diff --git a/addon/chrome/content/preferences.xhtml b/addon/chrome/content/preferences.xhtml
index 019e127..cede2f2 100644
--- a/addon/chrome/content/preferences.xhtml
+++ b/addon/chrome/content/preferences.xhtml
@@ -8,19 +8,19 @@
-
-
+
+ preference="__prefsPrefix__.autobookmark" native="true" />
+ preference="__prefsPrefix__.autolanguage" native="true" />
-
+
@@ -35,7 +35,7 @@
-
@@ -54,7 +54,7 @@
-
+
@@ -76,7 +76,7 @@
+ preference="__prefsPrefix__.pdftkpath">
@@ -90,17 +90,17 @@
+ preference="__prefsPrefix__.ennamesplit" native="true" />
-
+
@@ -111,7 +111,7 @@
-
+
@@ -120,7 +120,7 @@
+ preference="__prefsPrefix__.dateformatterfill" native="true" />
@@ -151,6 +151,9 @@
+
+
+
diff --git a/addon/locale/zh-CN/addon.ftl b/addon/locale/zh-CN/addon.ftl
index dcc540d..1d044ff 100644
--- a/addon/locale/zh-CN/addon.ftl
+++ b/addon/locale/zh-CN/addon.ftl
@@ -27,4 +27,6 @@ reference-trans-fail = 知网引文导出结果中未发现可用信息
unmatched-itemtype-fail = { $itemType } 类型条目不需要抓取
nonchinese-item = 非中文条目
cssci-success = { $title }, 引用数: { $cite }, 期刊类型: { $cssci }
-url-missing = 缺失条目 URL 信息
\ No newline at end of file
+url-missing = 缺失条目 URL
+translator-download-success = { $filename }转换器下载成功
+translator-download-fail = { $filename }转换器下载失败
\ No newline at end of file
diff --git a/addon/locale/zh-CN/preferences.ftl b/addon/locale/zh-CN/preferences.ftl
index 54a90b0..fad05d8 100644
--- a/addon/locale/zh-CN/preferences.ftl
+++ b/addon/locale/zh-CN/preferences.ftl
@@ -67,7 +67,6 @@ translator-tab-column1 = 名称
translator-tab-column2 = 本地更新时间
translator-tab-column3 = 最新更新时间
translator-tab-column4 = 操作
-refresh-label = 刷新
updateall-label = 更新全部
refreshInfo-label = 获取TranslatorCN更新信息
# update-translator = 更新之后还需要在浏览器Zotero Connector中更新转换器信息
diff --git a/src/addon.ts b/src/addon.ts
index 9ccfb2b..9b53c84 100644
--- a/src/addon.ts
+++ b/src/addon.ts
@@ -14,8 +14,8 @@ class Addon {
};
prefs?: {
window: Window;
- columns: Array;
- rows: Array<{ [dataKey: string]: string }>;
+ // columns: Array;
+ // rows: Array<{ [dataKey: string]: string }>;
};
dialog?: DialogHelper;
cookiebox: any;
diff --git a/src/hooks.ts b/src/hooks.ts
index 44b964f..afefdbc 100644
--- a/src/hooks.ts
+++ b/src/hooks.ts
@@ -8,6 +8,7 @@ import { getString, initLocale } from "./utils/locale";
import { registerPrefsScripts } from "./modules/preferenceScript";
import { displayMenuitem } from "../src/modules/ui";
import { showPop } from "./utils/window";
+import { clearPref, getPref, setPref } from "./utils/prefs";
async function onStartup() {
await Promise.all([
@@ -19,24 +20,7 @@ async function onStartup() {
ztoolkit.ProgressWindow.setIconURI(
"default",
`chrome://${config.addonRef}/content/icons/icon.png`
- );
-
- const popupWin = new ztoolkit.ProgressWindow(config.addonName, {
- closeOnClick: true,
- closeTime: -1,
- })
- .createLine({
- text: "startup-begin",
- type: "default",
- progress: 0,
- })
- .show();
-
- popupWin.changeLine({
- progress: 30,
- text: `[30%] startup-begin`,
- });
-
+ );
BasicExampleFactory.registerPrefs();
@@ -102,20 +86,24 @@ async function onNotify(
// Run this when addon is first run
-// Keep preferences startswith extensions.zotero.jasminum
+// Keep preferences startswith extensions.jasminum
function migratePrefs() {
ztoolkit.log("start to migrate");
- if ( Zotero.Prefs.get("jasminum.firstrun") != false ) {
+ if ( getPref("firstrun") != false ) {
const extensionBranch = Services.prefs.getBranch("extensions");
const prefs = extensionBranch.getChildList("", {});
- const jasminmPrefs = prefs.filter((p: string) => p.includes(".jasminum."))
- const shortJasminumPrefs = jasminmPrefs.filter( (p: string) => p.startsWith(".jasminum."));
- shortJasminumPrefs.array.forEach((ele: string) => {
+ const jasminmPrefs = prefs.filter((p: string) => p.includes(".zotero.jasminum."))
+ jasminmPrefs.forEach((ele: string) => {
ztoolkit.log("extensions" + ele);
- Zotero.Prefs.clear("extensions" + ele, true);
- showPop("extensions" + ele);
+ const longPrefName = ele.replace(/^\.zotero\./, '');
+ const shortPrefName = longPrefName.split(".")[1];
+ const prefValue = Zotero.Prefs.get(longPrefName);
+ if (prefValue != undefined && getPref(shortPrefName) == undefined) {
+ setPref(shortPrefName, prefValue);
+ Zotero.Prefs.clear(longPrefName);
+ }
});
- Zotero.Prefs.set("jasminum.firstrun", false);
+ setPref("firstrun", false);
}
}
diff --git a/src/modules/bookmark.ts b/src/modules/bookmark.ts
index 8ea96cf..f1458d9 100644
--- a/src/modules/bookmark.ts
+++ b/src/modules/bookmark.ts
@@ -1,11 +1,12 @@
import { config } from "../../package.json";
import { getHTMLDoc } from "../utils/http";
import { getString } from "../utils/locale";
+import { getPref } from "../utils/prefs";
import { showPop } from "../utils/window";
import { searchCNKI } from "./cnki";
async function checkPDFtkPath() {
- const pdftkpath = Zotero.Prefs.get("jasminum.pdftkpath") as string;
+ const pdftkpath = getPref("pdftkpath") as string;
let pdftk = "";
if (Zotero.isWin) {
pdftk = OS.Path.join(pdftkpath, "pdftk.exe");
@@ -113,7 +114,7 @@ async function addBookmark(item: Zotero.Item, bookmark: string) {
await OS.File.writeAtomic(cacheFile.path, array, {
tmpPath: cacheFile.path + ".tmp",
});
- let pdftk = Zotero.Prefs.get("jasminum.pdftkpath") as string;
+ let pdftk = getPref("pdftkpath") as string;
if (Zotero.isWin) {
pdftk = OS.Path.join(pdftk, "pdftk.exe");
} else {
diff --git a/src/modules/cnki.ts b/src/modules/cnki.ts
index 3807fe5..8c3d9a9 100644
--- a/src/modules/cnki.ts
+++ b/src/modules/cnki.ts
@@ -1,6 +1,7 @@
import { config } from "../../package.json";
import { getHTMLDoc, getHTMLText, string2HTML } from "../utils/http";
import { getString } from "../utils/locale";
+import { getPref } from "../utils/prefs";
import { getItems, isCNKIPDF } from "../utils/tools";
import { showPop } from "../utils/window";
import { addBookmarkItem } from "./bookmark";
@@ -500,7 +501,7 @@ export async function fixItem(newItems: Zotero.Item[], targetData: any) {
Zotero.Items.erase(newItem.getNotes());
}
// 是否处理中文姓名. For Chinese name
- if (Zotero.Prefs.get("jasminum.zhnamesplit")) {
+ if (getPref("zhnamesplit")) {
creators = newItem.getCreators() as MyCreator[];
for (let i = 0; i < creators.length; i++) {
const creator = creators[i];
@@ -678,7 +679,7 @@ export async function searchCNKIMetadata(items: Zotero.Item[]) {
item.parentID = newItem.id;
// Use Zotfile to rename file
if (
- Zotero.Prefs.get("jasminum.rename") &&
+ getPref("rename") &&
typeof Zotero.ZotFile != "undefined"
) {
Zotero.ZotFile.renameSelectedAttachments();
@@ -687,7 +688,7 @@ export async function searchCNKIMetadata(items: Zotero.Item[]) {
await item.saveTx();
await newItem.saveTx();
// Add bookmark after PDF attaching to new item
- if (Zotero.Prefs.get("jasminum.autobookmark") && isCNKIPDF(item)) {
+ if (getPref("autobookmark") && isCNKIPDF(item)) {
await addBookmarkItem(item);
}
} else {
diff --git a/src/modules/jasminum.ts b/src/modules/jasminum.ts
index 8d7e5bb..f81b76f 100644
--- a/src/modules/jasminum.ts
+++ b/src/modules/jasminum.ts
@@ -1,5 +1,6 @@
import { config } from "../../package.json";
import { getString } from "../utils/locale";
+import { getPref } from "../utils/prefs";
import { addBookmarkItem } from "./bookmark";
import {
searchCNKIMetadata,
@@ -72,20 +73,20 @@ export class BasicExampleFactory {
@example
static itemAddedNotifier(addedItems: Zotero.Item[]) {
let items: Zotero.Item[];
- if (Zotero.Prefs.get("jasminum.autoupdate")) {
+ if (getPref("autoupdate")) {
items = addedItems.filter((i) => isCNKIFile(i));
ztoolkit.log(`add ${items.length} items`);
searchCNKIMetadata(items);
}
// Split or merge name
- if (!Zotero.Prefs.get("jasminum.zhnamesplit")) {
+ if (!getPref("zhnamesplit")) {
items = addedItems.filter((i) =>
addon.data.CNDB.includes(i.getField("libraryCatalog") as string)
);
concatName(items);
}
// Add bookmark after new PDF is attached.
- if (Zotero.Prefs.get("jasminum.autobookmark")) {
+ if (getPref("autobookmark")) {
addedItems.forEach((i) => {
const parentItem = i.parentItem;
if (
@@ -99,7 +100,7 @@ export class BasicExampleFactory {
});
}
// Set default language field
- if (Zotero.Prefs.get("jasminum.autolanguage")) {
+ if (getPref("autolanguage")) {
items = addedItems.filter((i) => i.isRegularItem());
manualSetLanguage(items);
}
diff --git a/src/modules/preferenceScript.ts b/src/modules/preferenceScript.ts
index dcd1bc3..8f0e460 100644
--- a/src/modules/preferenceScript.ts
+++ b/src/modules/preferenceScript.ts
@@ -1,10 +1,10 @@
import { FilePickerHelper } from "zotero-plugin-toolkit/dist/helpers/filePicker";
-import { setPref } from "../utils/prefs";
-import { config } from "../../package.json";
+import { getPref, setPref } from "../utils/prefs";
import { showPop } from "../utils/window";
import { getString } from "../utils/locale";
export async function checkPath(pathvalue: string): Promise {
+ if (!pathvalue) return;
let pdftk = "";
if (ztoolkit.getGlobal('Zotero').isWin) {
pdftk = OS.Path.join(pathvalue, "pdftk.exe");
@@ -45,7 +45,10 @@ async function getLastUpdateFromFile(filename: string): Promise {
};
async function insertTable(): Promise {
+ ztoolkit.log("********** insert Table");
const data = await updateTranslatorData(true);
+ ztoolkit.log("********** insert Table");
+ ztoolkit.log(data);
if (data) {
ztoolkit.log("get translator data ok");
(addon.data
@@ -110,7 +113,7 @@ async function insertTable(): Promise {
}
async function updateTranslatorData(refresh = true): Promise {
- const baseUrl = Zotero.Prefs.get("jasminum.translatorurl") ? Zotero.Prefs.get("jasminum.translatorurl") : "https://oss.wwang.de/translators_CN";
+ const baseUrl = getPref("translatorurl") ? getPref("translatorurl") : "https://oss.wwang.de/translators_CN";
const url = baseUrl + "/data/translators.json";
const cacheFile = ztoolkit.getGlobal("Zotero").getTempDirectory();
cacheFile.append("translator.json");
@@ -151,12 +154,12 @@ async function downloadTranslator(filename: string): Promise {
// var url = `https://gitee.com/l0o0/translators_CN/raw/master/translators/${label}`;
// var url = `https://gitcode.net/goonback/translators_CN/-/raw/master/translators/${label}`;
// let url = `https://oss.wwang.de/translators_CN/${label}`;
- const baseUrl = Zotero.Prefs.get("jasminum.translatorurl") ? Zotero.Prefs.get("jasminum.translatorurl") : "https://oss.wwang.de/translators_CN";
+ const baseUrl = getPref("translatorurl") ? getPref("translatorurl") : "https://oss.wwang.de/translators_CN";
const url = baseUrl + "/" + filename;
ztoolkit.log(url);
try {
const contents = await ztoolkit.getGlobal("Zotero").File.getContentsFromURL(url);
- const desPath = OS.Path.join(
+ const desPath = PathUtils.join(
ztoolkit.getGlobal("Zotero").Prefs.get("dataDir") as string,
"translators",
filename
@@ -172,15 +175,19 @@ async function downloadTranslator(filename: string): Promise {
}
}
+
+export async function downloadAll() {
+ const data = await updateTranslatorData(false);
+ Object.keys(data).forEach(async (label) => await downloadTranslator(label));
+}
+
export async function registerPrefsScripts(_window: Window) {
// This function is called when the prefs window is opened
// See addon/chrome/content/preferences.xul onpaneload
- const data = await updateTranslatorData(true);
- const rows = Object.keys(data).map((e: any) => { return { name: (data[e] as any).label, "local": "xx", "remote": (data[e] as any).lastUpdated, "download": "点击下载" } });
- if (addon.data.prefs) {
- addon.data.prefs.window = _window;
+ if (!addon.data.prefs) {
+ addon.data.prefs = { window: _window}
} else {
- addon.data.prefs = { window: _window, columns: [], rows: [] }
+ addon.data.prefs.window = _window;
}
updatePrefsUI();
bindPrefEvents();
@@ -190,13 +197,17 @@ async function updatePrefsUI() {
// You can initialize some UI elements on prefs window
// with addon.data.prefs.window.document
// Or bind some events to the elements
+ ztoolkit.log("***** update UI");
const renderLock = ztoolkit.getGlobal("Zotero").Promise.defer();
+
if (addon.data.prefs?.window == undefined) return;
-
+ ztoolkit.log("************* 111");
// Update pdftk check icon
await checkPath(ztoolkit.getGlobal("Zotero").Prefs.get("jasminum.pdftkpath") as string);
+ ztoolkit.log("************* 111");
// Update translator table
await insertTable();
+ ztoolkit.log("************* 111");
await renderLock.promise;
ztoolkit.log("Preference table rendered!");
}
@@ -246,4 +257,13 @@ function bindPrefEvents() {
await checkPath(f);
}
});
+
+ addon.data
+ .prefs!.window.document.querySelector(
+ "#download-all-translators"
+ )
+ ?.addEventListener("click", async (e) => {
+ ztoolkit.log("download all translators");
+ await downloadAll();
+ });
}
diff --git a/src/modules/tools.ts b/src/modules/tools.ts
index 17eb94a..4693d5b 100644
--- a/src/modules/tools.ts
+++ b/src/modules/tools.ts
@@ -1,9 +1,10 @@
+import { getPref } from "../utils/prefs";
import { getItems } from "../utils/tools";
// import { guess } from "./nlp";
// Functions used in menu
export async function concatName(items: Zotero.Item[]) {
- const isSplitEnName = Zotero.Prefs.get("jasminum.ennamesplit");
+ const isSplitEnName = getPref("ennamesplit");
for (const item of items) {
const creators = item.getCreators();
for (let i = 0; i < creators.length; i++) {
@@ -38,7 +39,7 @@ export async function concatNameMenu(type: "items" | "collection") {
}
export async function splitName(items: Zotero.Item[]) {
- const isSplitEnName = Zotero.Prefs.get("jasminum.ennamesplit");
+ const isSplitEnName = getPref("ennamesplit");
for (const item of items) {
const creators = item.getCreators();
for (let i = 0; i < creators.length; i++) {
diff --git a/src/utils/cookiebox.ts b/src/utils/cookiebox.ts
index ed56115..3302424 100644
--- a/src/utils/cookiebox.ts
+++ b/src/utils/cookiebox.ts
@@ -1,3 +1,5 @@
+import { getPref } from "./prefs";
+
export class MyCookieSandbox {
public searchCookieBox: any;
public attachmentCookieBox: any;
@@ -23,7 +25,7 @@ export class MyCookieSandbox {
}
setAttachmentCookieBox() {
- const cookieData = Zotero.Prefs.get("jasminum.cnki.attachment.cookie") as string;
+ const cookieData = getPref("cnkiAttachmentCookie") as string;
return new Zotero.CookieSandbox(null, this.baseUrl, cookieData, this.userAgent);
}