Skip to content

Commit

Permalink
Add item menu callback. Execute local js or cmd.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bowen-0x00 committed Nov 29, 2023
1 parent 67157b2 commit 425be18
Show file tree
Hide file tree
Showing 19 changed files with 116 additions and 1,188 deletions.
Binary file removed addon/chrome/content/icons/favicon.png
Binary file not shown.
14 changes: 14 additions & 0 deletions addon/chrome/content/icons/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed addon/chrome/content/icons/[email protected]
Binary file not shown.
14 changes: 14 additions & 0 deletions addon/chrome/content/icons/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 5 additions & 31 deletions addon/chrome/content/preferences.xhtml
Original file line number Diff line number Diff line change
@@ -1,36 +1,10 @@
<linkset>
<html:link rel="localization" href="__addonRef__-preferences.ftl" />
</linkset>
<vbox
id="zotero-prefpane-__addonRef__"
onload="Zotero.__addonInstance__.hooks.onPrefsEvent('load', {window})"
>
<vbox id="zotero-prefpane-__addonRef__" onload="Zotero.__addonInstance__.hooks.onPrefsEvent('load', {window})">
<groupbox>
<label><html:h2 data-l10n-id="pref-title"></html:h2></label>
<checkbox
id="zotero-prefpane-__addonRef__-enable"
preference="extensions.zotero.__addonRef__.enable"
data-l10n-id="pref-enable"
/>
<hbox>
<html:label
for="zotero-prefpane-__addonRef__-input"
data-l10n-id="pref-input"
></html:label>
<html:input
type="text"
id="zotero-prefpane-__addonRef__-input"
preference="extensions.zotero.__addonRef__.input"
></html:input>
</hbox>
<hbox class="virtualized-table-container" flex="1" height="300px">
<html:div id="__addonRef__-table-container" />
</hbox>
<label>
<html:h2 data-l10n-id="pref-title"></html:h2>
</label>
</groupbox>
</vbox>
<vbox>
<html:label
data-l10n-id="pref-help"
data-l10n-args='{"time": "__buildTime__","name": "__addonName__","version":"__buildVersion__"}'
></html:label>
</vbox>
</vbox>
3 changes: 0 additions & 3 deletions addon/chrome/content/zoteroPane.css
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
.makeItRed {
background-color: tomato;
}
2 changes: 1 addition & 1 deletion addon/locale/en-US/addon.ftl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
startup-begin = Addon is loading
startup-finish = Addon is ready
menuitem-label = Addon Template: Helper Examples
menuitem-label = Action-cmd: menu action
menupopup-label = Addon Template: Menupopup
menuitem-submenulabel = Addon Template
menuitem-filemenulabel = Addon Template: File Menuitem
Expand Down
2 changes: 1 addition & 1 deletion addon/locale/en-US/preferences.ftl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pref-title = Addon Template Example
pref-title = See settings in Settings/Advanced/Config Editor
pref-enable =
.label = Enable
pref-input = Input
Expand Down
2 changes: 1 addition & 1 deletion addon/locale/zh-CN/addon.ftl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
startup-begin = 插件加载中
startup-finish = 插件已就绪
menuitem-label = 插件模板: 帮助工具样例
menuitem-label = Action-cmd: menu action
menupopup-label = 插件模板: 弹出菜单
menuitem-submenulabel = 插件模板:子菜单
menuitem-filemenulabel = 插件模板: 文件菜单
Expand Down
2 changes: 1 addition & 1 deletion addon/locale/zh-CN/preferences.ftl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pref-title = 插件模板设置示例
pref-title = 目前设置在"设置/高级/编辑器"下
pref-enable =
.label = 开启
pref-input = 输入
Expand Down
6 changes: 3 additions & 3 deletions addon/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"homepage_url": "__homepage__",
"author": "__author__",
"icons": {
"48": "chrome/content/icons/[email protected].png",
"96": "chrome/content/icons/favicon.png"
"48": "chrome/content/icons/[email protected].svg",
"96": "chrome/content/icons/favicon.svg"
},
"applications": {
"zotero": {
Expand All @@ -17,4 +17,4 @@
"strict_max_version": "7.0.*"
}
}
}
}
6 changes: 4 additions & 2 deletions addon/prefs.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-undef */
pref("__prefsPrefix__.enable", true);
pref("__prefsPrefix__.input", "This is input");
pref('__prefsPrefix__.menu.jsOrCmd', true)
pref('__prefsPrefix__.menu.jsPath', 'D:\\Project\\Python工具\\extract_pdf_image\\test.js')
pref('__prefsPrefix__.menu.cmd', 'D:\\Python310\\pythonw.exe')
pref('__prefsPrefix__.menu.args', '"D:\\Project\\Python工具\\extract_pdf_image\\extract_pdf_image.py"')
16 changes: 16 additions & 0 deletions src/actions/menu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { MenuTask } from "../tasks"
import { config } from "../../package.json"


export async function menuAction(task: MenuTask) {
const jsOrCmd = Zotero.Prefs.get(`${config.addonRef}.menu.jsOrCmd`) as boolean
const args = (Zotero.Prefs.get(`${config.addonRef}.menu.args`) as string).split(/\s*,\s*/).map(el => el.trim().replace(/^\"|\"$/g, "")).filter(el => el)
args.push(`"${JSON.stringify(task)}"`)
if (jsOrCmd) {
const content = await OS.File.read(Zotero.Prefs.get(`${config.addonRef}.menu.jsPath`) as string, { encoding: "utf-8" }) as string;
// new Function(content)(task)
eval(content)
} else {
await Zotero.Utilities.Internal.exec(Zotero.Prefs.get(`${config.addonRef}.menu.cmd`) as string, args)
}
}
3 changes: 0 additions & 3 deletions src/addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ class Addon {
};
prefs?: {
window: Window;
columns: Array<ColumnOptions>;
rows: Array<{ [dataKey: string]: string }>;
};
dialog?: DialogHelper;
};
// Lifecycle hooks
public hooks: typeof hooks;
Expand Down
113 changes: 12 additions & 101 deletions src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {
BasicExampleFactory,
HelperExampleFactory,
KeyExampleFactory,
PromptExampleFactory,
UIExampleFactory,
} from "./modules/examples";
import { config } from "../package.json";
import { getString, initLocale } from "./utils/locale";
import { registerPrefsScripts } from "./modules/preferenceScript";
import { createZToolkit } from "./utils/ztoolkit";
import { addMenuTask, MenuTask } from "./tasks"
import { menuAction } from "./actions/menu"

async function onStartup() {
Components.utils.import("resource://gre/modules/osfile.jsm");
await Promise.all([
Zotero.initializationPromise,
Zotero.unlockPromise,
Expand All @@ -20,77 +20,22 @@ async function onStartup() {

BasicExampleFactory.registerPrefs();

BasicExampleFactory.registerNotifier();

await onMainWindowLoad(window);
}

async function onMainWindowLoad(win: Window): Promise<void> {
// Create ztoolkit for every window
addon.data.ztoolkit = createZToolkit();

const popupWin = new ztoolkit.ProgressWindow(config.addonName, {
closeOnClick: true,
closeTime: -1,
})
.createLine({
text: getString("startup-begin"),
type: "default",
progress: 0,
})
.show();

KeyExampleFactory.registerShortcuts();

await Zotero.Promise.delay(1000);
popupWin.changeLine({
progress: 30,
text: `[30%] ${getString("startup-begin")}`,
});

UIExampleFactory.registerStyleSheet();

UIExampleFactory.registerRightClickMenuItem();

UIExampleFactory.registerRightClickMenuPopup();

UIExampleFactory.registerWindowMenuWithSeparator();

await UIExampleFactory.registerExtraColumn();

await UIExampleFactory.registerExtraColumnWithCustomCell();

await UIExampleFactory.registerCustomItemBoxRow();

UIExampleFactory.registerLibraryTabPanel();

await UIExampleFactory.registerReaderTabPanel();

PromptExampleFactory.registerNormalCommandExample();

PromptExampleFactory.registerAnonymousCommandExample();

PromptExampleFactory.registerConditionalCommandExample();

await Zotero.Promise.delay(1000);

popupWin.changeLine({
progress: 100,
text: `[100%] ${getString("startup-finish")}`,
});
popupWin.startCloseTimer(5000);

addon.hooks.onDialogEvents("dialogExample");
}

async function onMainWindowUnload(win: Window): Promise<void> {
ztoolkit.unregisterAll();
addon.data.dialog?.window?.close();
}

function onShutdown(): void {
ztoolkit.unregisterAll();
addon.data.dialog?.window?.close();
// Remove addon object
addon.data.alive = false;
delete Zotero[config.addonInstance];
Expand All @@ -107,16 +52,7 @@ async function onNotify(
extraData: { [key: string]: any },
) {
// You can add your code to the corresponding notify type
ztoolkit.log("notify", event, type, ids, extraData);
if (
event == "select" &&
type == "tab" &&
extraData[ids[0]].type == "reader"
) {
BasicExampleFactory.exampleNotifierCallback();
} else {
return;
}

}

/**
Expand All @@ -136,47 +72,21 @@ async function onPrefsEvent(type: string, data: { [key: string]: any }) {
}

function onShortcuts(type: string) {
switch (type) {
case "larger":
KeyExampleFactory.exampleShortcutLargerCallback();
break;
case "smaller":
KeyExampleFactory.exampleShortcutSmallerCallback();
break;
case "confliction":
KeyExampleFactory.exampleShortcutConflictingCallback();
break;
default:
break;
}

}

function onDialogEvents(type: string) {
switch (type) {
case "dialogExample":
HelperExampleFactory.dialogExample();
break;
case "clipboardExample":
HelperExampleFactory.clipboardExample();
break;
case "filePickerExample":
HelperExampleFactory.filePickerExample();
break;
case "progressWindowExample":
HelperExampleFactory.progressWindowExample();
break;
case "vtableExample":
HelperExampleFactory.vtableExample();
break;
default:
break;
}

}

// Add your hooks here. For element click, etc.
// Keep in mind hooks only do dispatch. Don't add code that does real jobs in hooks.
// Otherwise the code would be hard to read and maintian.

async function onMenuActionInBatch(tasks: Array<any>) {
for (const task of tasks) {
await menuAction(task);
}
}
export default {
onStartup,
onShutdown,
Expand All @@ -186,4 +96,5 @@ export default {
onPrefsEvent,
onShortcuts,
onDialogEvents,
onMenuActionInBatch
};
Loading

0 comments on commit 425be18

Please sign in to comment.