Skip to content

Commit

Permalink
Add pdftk check before adding bookmark. Update query title in functio…
Browse files Browse the repository at this point in the history
…n splieFilename. Fix #2 issue mix VO and IS.
  • Loading branch information
l0o0 committed Aug 6, 2020
1 parent d8ffc10 commit 606fa01
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
33 changes: 30 additions & 3 deletions chrome/content/scripts/jasminum.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ Zotero.Jasminum = {

splitFilename: function (filename) {
// Make query parameters from filename
var patent = Zotero.Prefs.get("jasminum.namepatent");
var patentArr = patent.split("_");
var prefix = filename.substr(0, filename.length - 4);

var prefix = prefix.replace("_省略_", ""); // Long title contains _省略_
var author = "";
var title = "";
// Remove year string
Expand All @@ -155,9 +156,9 @@ Zotero.Jasminum = {
console.log(authorIdx);
author = prefixArr[authorIdx];
prefixArr.splice(authorIdx, 1);
title = prefixArr.join("");
title = prefixArr.join(" ");
} else {
title = prefixArr.join("");
title = prefixArr.join(" ");
}

return {
Expand Down Expand Up @@ -362,6 +363,7 @@ Zotero.Jasminum = {
if (!authors[authors.length - 1].trim()) authors.pop();
return tag + " " + authors.join("\n" + tag + " ");
});
var data = data.replace(/vo (\d+)\n/, "VO $1\n"); // Divide VO and IS to different line.
targetUrl = `https://kns.cnki.net/KCMS/detail/detail.aspx?dbcode=${targetID.dbcode}&dbname=${targetID.dbname}&filename=${targetID.filename}&v=`;
Zotero.debug(data);
return [data, targetUrl];
Expand Down Expand Up @@ -444,6 +446,7 @@ Zotero.Jasminum = {
var libraryID = item.libraryID;
if (!Zotero.Jasminum.checkItem(item)) return; // TODO Need notify
var fileData = Zotero.Jasminum.splitFilename(item.getFilename());
Zotero.debug(fileData);
var searchPrepareOut = await Zotero.Jasminum.searchPrepare(fileData);
Zotero.debug("searchPrepareOut");
Zotero.debug(searchPrepareOut);
Expand Down Expand Up @@ -661,10 +664,34 @@ Zotero.Jasminum = {

addBookmarkItem: async function () {
var item = ZoteroPane.getSelectedItems()[0];
if (!(await Zotero.Jasminum.checkPath())) {
alert(
"Can't find PDFtk Server execute file. Please install PDFtk Server and choose the folder in the Jasminum preference window."
);
return false;
}
var bookmark = await Zotero.Jasminum.getBookmark(item);
await Zotero.Jasminum.addBookmark(item, bookmark);
},

checkPath: async function () {
Zotero.debug("** Jasminum check path.");
var pdftkpath = Zotero.Prefs.get("jasminum.pdftkpath");
Zotero.debug(pdftkpath);
var pdftk = "";
if (Zotero.isWin) {
Zotero.debug("1");
pdftk = OS.Path.join(pdftkpath, "pdftk.exe");
} else {
Zotero.debug("2");
pdftk = OS.Path.join(pdftkpath, "pdftk");
}
Zotero.debug(pdftk);
var fileExist = await OS.File.exists(pdftk);
Zotero.debug(fileExist);
return fileExist;
},

checkItemName: function (item) {
return item.isRegularItem() && item.isTopLevelItem();
},
Expand Down
22 changes: 2 additions & 20 deletions chrome/content/scripts/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ initPref = async function () {
Components.utils.import("resource://gre/modules/osfile.jsm");
var jasminum_pdftk_path = Zotero.Prefs.get("jasminum.pdftkpath");
document.getElementById("jasminum-pdftk-path").value = jasminum_pdftk_path;
var fileExist = await checkPath();
var fileExist = await Zotero.Jasminum.checkPath();
pathCheckIcon(fileExist);
};

Expand All @@ -27,28 +27,10 @@ choosePath = async function () {
Zotero.debug("** Jasminum " + fp.file.path);
Zotero.Prefs.set("jasminum.pdftkpath", fp.file.path);
document.getElementById("jasminum-pdftk-path").value = fp.file.path;
var fileExist = await checkPath();
var fileExist = await Zotero.Jasminum.checkPath();
pathCheckIcon(fileExist);
};

checkPath = async function () {
Zotero.debug("** Jasminum check path.");
var pdftkpath = Zotero.Prefs.get("jasminum.pdftkpath");
Zotero.debug(pdftkpath);
var pdftk = "";
if (Zotero.isWin) {
Zotero.debug("1");
pdftk = OS.Path.join(pdftkpath, "pdftk.exe");
} else {
Zotero.debug("2");
pdftk = OS.Path.join(pdftkpath, "pdftk");
}
Zotero.debug(pdftk);
var fileExist = await OS.File.exists(pdftk);
Zotero.debug(fileExist);
return fileExist;
};

pathCheckIcon = function (fileExist) {
document.getElementById("path-accept").hidden = !fileExist;
document.getElementById("path-error").hidden = fileExist;
Expand Down
2 changes: 1 addition & 1 deletion install.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Description about="urn:mozilla:install-manifest">
<em:id>jasminum@linxzh.com</em:id>
<em:name>Jasminum</em:name>
<em:version>0.0.2</em:version>
<em:version>0.0.3</em:version>
<em:type>2</em:type>
<em:iconURL>chrome://jasminum/skin/icon.png</em:iconURL>
<em:multiprocessCompatible>true</em:multiprocessCompatible>
Expand Down

0 comments on commit 606fa01

Please sign in to comment.