Skip to content

Commit

Permalink
fix: Zotero6与7配置设置异常造成的问题. fixed l0o0#197
Browse files Browse the repository at this point in the history
  • Loading branch information
l0o0 committed Sep 9, 2023
1 parent 8a06c74 commit 0f189a8
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 67 deletions.
31 changes: 17 additions & 14 deletions addon/chrome/content/preferences.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
<label>
<html:h2 data-l10n-id="chinese-caption"></html:h2>
</label>
<checkbox id="zotero-prefpane-jasminum.autoupdate" preference="extensions.zotero.jasminum.autoupdate"
<checkbox id="zotero-prefpane-jasminum.autoupdate" preference="__prefsPrefix__.autoupdate"
data-l10n-id="autoupdate-label" native="true" />
<checkbox id="jasminum.zhnamesplit" data-l10n-id="zhnamesplit-label"
preference="extensions.zotero.jasminum.zhnamesplit" native="true" />
<checkbox id="jasminum.rename" data-l10n-id="rename-label" preference="extensions.zotero.jasminum.rename"
preference="__prefsPrefix__.zhnamesplit" native="true" />
<checkbox id="jasminum.rename" data-l10n-id="rename-label" preference="__prefsPrefix__.rename"
native="true" />
<checkbox id="jasminum.autobookmark" data-l10n-id="autobookmark-label"
preference="extensions.zotero.jasminum.autobookmark" native="true" />
preference="__prefsPrefix__.autobookmark" native="true" />
<hbox>
<checkbox id="jasminum.autolanguage" data-l10n-id="autolanguage-label"
preference="extensions.zotero.jasminum.autolanguage" native="true" />
preference="__prefsPrefix__.autolanguage" native="true" />
<label data-l10n-id="autolanguagefunc-label" />
<menulist id="jasminum.autolanguagefunc" preference="extensions.zotero.jasminum.autolanguagefunc" native="true">
<menulist id="jasminum.autolanguagefunc" preference="__prefsPrefix__.autolanguagefunc" native="true">
<menupopup>
<menuitem data-l10n-id="autolanguage-auto" value="Auto" />
<menuitem data-l10n-id="autolanguage-manual" value="Manual" />
Expand All @@ -35,7 +35,7 @@
<vbox flex="1">
<hbox>
<label class='jasminum-label' data-l10n-id="namepatent-desc-info"></label>
<menulist editable="true" id="jasminum.namepatent" preference="extensions.zotero.jasminum.namepatent"
<menulist editable="true" id="jasminum.namepatent" preference="__prefsPrefix__.namepatent"
native="true">
<menupopup>
<menuitem label="{%t}_{%g}" value="{%t}_{%g}" data-l10n-id="namepatent-desc-tg" />
Expand All @@ -54,7 +54,7 @@
<vbox>
<hbox>
<label class="jasminum-label" data-l10n-id="attachment-type-info"></label>
<menulist id="jasminum.attachment" preference="extensions.zotero.jasminum.attachmenttype" native="true">
<menulist id="jasminum.attachment" preference="__prefsPrefix__.attachmenttype" native="true">
<menupopup>
<menuitem label="pdf" value="pdf" />
<menuitem label="caj" value="caj" />
Expand All @@ -76,7 +76,7 @@
</description>
<hbox style="display: flex; align-items: center;">
<html:input style="flex: 1;" type="text" id="jasminum-pdftk-path"
preference="extensions.zotero.jasminum.pdftkpath"></html:input>
preference="__prefsPrefix__.pdftkpath"></html:input>
<button data-l10n-id="choose" id="choose-button" native="true" />
<image id="path-accept" src="chrome://jasminum/content/icons/accept.png" data-l10n-id="pdftk-path-check-ok"
hidden="true" />
Expand All @@ -90,17 +90,17 @@
</label>
<vbox flex="1">
<checkbox id="jasminum.ennamesplit" data-l10n-id="toolbox-ennamesplit-label"
preference="extensions.zotero.jasminum.ennamesplit" native="true" />
preference="__prefsPrefix__.ennamesplit" native="true" />
<!-- <hbox>
<label class="jasminum-label" data-l10n-id="toolbox-bacthsetlanguage-info"></label>
<html:input type="text" id="jasminum.languagelist" preference="extensions.zotero.jasminum.languagelist">
<html:input type="text" id="jasminum.languagelist" preference="__prefsPrefix__.languagelist">
</html:input>
<image src="chrome://jasminum/content/icons/information.png" style="margin-top: 5px; margin-bottom: 5px"
data-l10n-id="toolbox-bacthsetlanguage-desc" />
</hbox> -->
<hbox>
<label class="jasminum-label" data-l10n-id="toolbox-manualsetlanguage-info"></label>
<menulist flex="0.5" id="jasminum.language" preference="extensions.zotero.jasminum.language" native="true">
<menulist flex="0.5" id="jasminum.language" preference="__prefsPrefix__.language" native="true">
<menupopup>
<menuitem label="zh-CN" value="zh-CN" />
<menuitem label="en-US" value="en-US" />
Expand All @@ -111,7 +111,7 @@
</hbox>
<hbox>
<label class="jasminum-label" data-l10n-id="toolbox-dateformatter-info"></label>
<menulist id="jasminum.dateformatter" preference="extensions.zotero.jasminum.dateformatter" native="true">
<menulist id="jasminum.dateformatter" preference="__prefsPrefix__.dateformatter" native="true">
<menupopup>
<menuitem label="ISO" value="ISO" tooltiptext="toolbox.dateformatter.iso;" />
<menuitem label="Short" value="short" tooltiptext="toolbox.dateformatter.short;" />
Expand All @@ -120,7 +120,7 @@
</menupopup>
</menulist>
<checkbox id="jasminum.dateformatterfill" data-l10n-id="toolbox-dateformatterfill"
preference="extensions.zotero.jasminum.dateformatterfill" native="true" />
preference="__prefsPrefix__.dateformatterfill" native="true" />
</hbox>
</vbox>
</groupbox>
Expand Down Expand Up @@ -151,6 +151,9 @@
</html:table>
</html:div>
</hbox>
<hbox>
<button data-l10n-id="updateall-label" id="download-all-translators" native="true" flex="0.5"/>
</hbox>
</groupbox>
</vbox>
<vbox>
Expand Down
4 changes: 3 additions & 1 deletion addon/locale/zh-CN/addon.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ reference-trans-fail = 知网引文导出结果中未发现可用信息
unmatched-itemtype-fail = { $itemType } 类型条目不需要抓取
nonchinese-item = 非中文条目
cssci-success = { $title }, 引用数: { $cite }, 期刊类型: { $cssci }
url-missing = 缺失条目 URL 信息
url-missing = 缺失条目 URL
translator-download-success = { $filename }转换器下载成功
translator-download-fail = { $filename }转换器下载失败
1 change: 0 additions & 1 deletion addon/locale/zh-CN/preferences.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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中更新转换器信息
Expand Down
4 changes: 2 additions & 2 deletions src/addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Addon {
};
prefs?: {
window: Window;
columns: Array<ColumnOptions>;
rows: Array<{ [dataKey: string]: string }>;
// columns: Array<ColumnOptions>;
// rows: Array<{ [dataKey: string]: string }>;
};
dialog?: DialogHelper;
cookiebox: any;
Expand Down
40 changes: 14 additions & 26 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand 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();

Expand Down Expand Up @@ -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);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/modules/bookmark.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions src/modules/cnki.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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();
Expand All @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions src/modules/jasminum.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 (
Expand All @@ -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);
}
Expand Down
Loading

0 comments on commit 0f189a8

Please sign in to comment.