From 84beaab5fd499814d42c6141a8833f5714b93923 Mon Sep 17 00:00:00 2001 From: xiangyu <3170102889@zju.edu.cn> Date: Thu, 5 Jan 2023 20:28:03 +0800 Subject: [PATCH] add: notify hook example --- addon/chrome/content/preferences.xhtml | 2 +- src/hooks.ts | 48 ++++++++++++++++++++------ src/modules/examples.ts | 19 ++++------ 3 files changed, 46 insertions(+), 23 deletions(-) diff --git a/addon/chrome/content/preferences.xhtml b/addon/chrome/content/preferences.xhtml index c928e36..5c0e4a0 100644 --- a/addon/chrome/content/preferences.xhtml +++ b/addon/chrome/content/preferences.xhtml @@ -1,6 +1,6 @@ diff --git a/src/hooks.ts b/src/hooks.ts index 57c4f1a..ead45cc 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -10,20 +10,20 @@ import { registerPrefsScripts } from "./modules/preferenceScript"; async function onStartup() { initLocale(); - const w = showProgressWindow( + const progWin = showProgressWindow( config.addonName, getString("startup.begin"), "default", -1 ); - changeProgressWindowLine(w, { newProgress: 0 }); + changeProgressWindowLine(progWin, { newProgress: 0 }); BasicExampleFactory.registerPrefs(); BasicExampleFactory.registerNotifier(); await Zotero.Promise.delay(1000); - changeProgressWindowLine(w, { + changeProgressWindowLine(progWin, { newProgress: 30, newText: `[30%] ${getString("startup.begin")}`, }); @@ -47,11 +47,11 @@ async function onStartup() { await UIExampleFactory.registerReaderTabPanel(); await Zotero.Promise.delay(1000); - changeProgressWindowLine(w, { + changeProgressWindowLine(progWin, { newProgress: 100, newText: `[100%] ${getString("startup.finish")}`, }); - w.startCloseTimer(5000); + progWin.startCloseTimer(5000); } function onShutdown(): void { @@ -63,14 +63,37 @@ function onShutdown(): void { } /** - * This function is just a dispatcher for UI events. - * Any operations should be placed in a function to keep this funcion clear + * This function is just an example of dispatcher for Notify events. + * Any operations should be placed in a function to keep this funcion clear. + */ +async function onNotify( + event: string, + type: string, + ids: Array, + extraData: { [key: string]: any } +) { + // You can add your code to the corresponding notify type + ztoolkit.Tool.log("notify", event, type, ids, extraData); + if ( + event == "select" && + type == "tab" && + extraData[ids[0]].type == "reader" + ) { + BasicExampleFactory.exampleNotifierCallback(); + } else { + return; + } +} + +/** + * 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. * @param type event type * @param data event data */ -function onCustomEvent(type: string, data: { [key: string]: any }) { +async function onPrefsEvent(type: string, data: { [key: string]: any }) { switch (type) { - case "prefLoad": + case "load": registerPrefsScripts(data.window); break; default: @@ -78,8 +101,13 @@ function onCustomEvent(type: string, data: { [key: string]: any }) { } } +// 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. + export default { onStartup, onShutdown, - onCustomEvent, + onNotify, + onPrefsEvent, }; diff --git a/src/modules/examples.ts b/src/modules/examples.ts index 0dd8a2b..d5de070 100644 --- a/src/modules/examples.ts +++ b/src/modules/examples.ts @@ -1,5 +1,6 @@ import { config } from "../../package.json"; import { getString } from "./locale"; +import { showProgressWindow } from "./progressWindow"; function example( target: any, @@ -38,18 +39,7 @@ export class BasicExampleFactory { this.unregisterNotifier(notifierID); return; } - ztoolkit.Tool.log("notify", event, type, ids, extraData); - // You can add your code to the corresponding notify type - if ( - event == "select" && - type == "tab" && - extraData[ids[0]].type == "reader" - ) { - // Select a reader tab - } - if (event == "add" && type == "item") { - // Add an item - } + addon.hooks.onNotify(event, type, ids, extraData); }, }; @@ -70,6 +60,11 @@ export class BasicExampleFactory { ); } + @example + static exampleNotifierCallback() { + showProgressWindow(config.addonName, "Open Tab Detected!", "success"); + } + @example private static unregisterNotifier(notifierID: string) { Zotero.Notifier.unregisterObserver(notifierID);