diff --git a/addon/chrome/content/preferences.xhtml b/addon/chrome/content/preferences.xhtml
index 019e127..fe198d0 100644
--- a/addon/chrome/content/preferences.xhtml
+++ b/addon/chrome/content/preferences.xhtml
@@ -8,25 +8,25 @@
-
-
+
-
+ preference="__prefsPrefix__.autobookmark" native="true" />
+
+ 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/package.json b/package.json
index e7de8c1..fc1018d 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "jasminum",
- "version": "1.0.0-4",
+ "version": "1.0.0-7",
"description": "一个简单的 Zotero 中文插件",
"config": {
"addonName": "Jasminum",
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 ab44e0e..d94e547 100644
--- a/src/hooks.ts
+++ b/src/hooks.ts
@@ -7,6 +7,8 @@ import { config } from "../package.json";
import { 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([
@@ -18,7 +20,7 @@ async function onStartup() {
ztoolkit.ProgressWindow.setIconURI(
"default",
`chrome://${config.addonRef}/content/icons/icon.png`
- );
+ );
BasicExampleFactory.registerPrefs();
@@ -48,6 +50,9 @@ async function onStartup() {
displayMenuitem,
false
);
+
+ // Migrate Prefs from Zotero 6 to 7
+ migratePrefs();
}
function onShutdown(): void {
@@ -79,6 +84,29 @@ async function onNotify(
}
}
+
+// Run this when addon is first run
+// Keep preferences startswith extensions.jasminum
+function migratePrefs() {
+ ztoolkit.log("start to migrate");
+ if ( getPref("firstrun") != false ) {
+ const extensionBranch = Services.prefs.getBranch("extensions");
+ const prefs = extensionBranch.getChildList("", {});
+ const jasminmPrefs = prefs.filter((p: string) => p.includes(".zotero.jasminum."))
+ jasminmPrefs.forEach((ele: string) => {
+ ztoolkit.log("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);
+ }
+ });
+ setPref("firstrun", false);
+ }
+}
+
/**
* This function is just an example of dispatcher for Preference UI events.
* Any operations should be placed in a function to keep this funcion clear.
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..2d54050 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 (
@@ -98,11 +99,11 @@ export class BasicExampleFactory {
}
});
}
- // Set default language field
- if (Zotero.Prefs.get("jasminum.autolanguage")) {
- items = addedItems.filter((i) => i.isRegularItem());
- manualSetLanguage(items);
- }
+ // // Set default language field
+ // if (getPref("autolanguage")) {
+ // items = addedItems.filter((i) => i.isRegularItem());
+ // manualSetLanguage(items);
+ // }
}
@example
diff --git a/src/modules/preferenceScript.ts b/src/modules/preferenceScript.ts
index 3521005..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,8 +113,8 @@ async function insertTable(): Promise {
}
async function updateTranslatorData(refresh = true): Promise {
- const translatorUrl = Zotero.Prefs.get("jasminum.translatorurl");
- const url = translatorUrl + "/data/translators.json";
+ 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");
let contents;
@@ -150,12 +153,13 @@ 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/translators/${label}`;
- const url = Zotero.Prefs.get("jasminum.translatorurl") + "/" + filename;
+ // let url = `https://oss.wwang.de/translators_CN/${label}`;
+ 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
@@ -171,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();
@@ -189,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!");
}
@@ -245,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);
}
diff --git a/update.json b/update.json
index 8b2ab82..31b76dd 100644
--- a/update.json
+++ b/update.json
@@ -3,7 +3,7 @@
"jasminum@linxzh.com": {
"updates": [
{
- "version": "1.0.0-4",
+ "version": "1.0.0-7",
"update_link": "https://github.com/l0o0/jasminum/releases/latest/download/jasminum.xpi",
"applications": {
"zotero": {