From 1e12c84ba62e744d28a5cae93f45acbc667e666d Mon Sep 17 00:00:00 2001 From: windingwind <33902321+windingwind@users.noreply.github.com> Date: Sat, 29 Jul 2023 11:12:20 +0800 Subject: [PATCH] update: main window hooks --- addon/bootstrap.js | 85 +++++----------------------------------------- 1 file changed, 9 insertions(+), 76 deletions(-) diff --git a/addon/bootstrap.js b/addon/bootstrap.js index d70a135..87f828d 100644 --- a/addon/bootstrap.js +++ b/addon/bootstrap.js @@ -5,86 +5,11 @@ * [2] https://www.zotero.org/support/dev/zotero_7_for_developers */ -if (typeof Zotero == "undefined") { - var Zotero; -} - var chromeHandle; -var windowListener; - -// In Zotero 6, bootstrap methods are called before Zotero is initialized, and using include.js -// to get the Zotero XPCOM service would risk breaking Zotero startup. Instead, wait for the main -// Zotero window to open and get the Zotero object from there. -// -// In Zotero 7, bootstrap methods are not called until Zotero is initialized, and the 'Zotero' is -// automatically made available. -async function waitForZotero() { - await new Promise(async (resolve) => { - if (typeof Zotero != "undefined") { - resolve(); - } - - const { Services } = ChromeUtils.import( - "resource://gre/modules/Services.jsm", - ); - const windows = Services.wm.getEnumerator("navigator:browser"); - let found = false; - while (windows.hasMoreElements()) { - let win = windows.getNext(); - if (win.Zotero) { - Zotero = win.Zotero; - found = true; - resolve(); - break; - } - } - windowListener = { - onOpenWindow: function (aWindow) { - // Wait for the window to finish loading - const domWindow = aWindow - .QueryInterface(Ci.nsIInterfaceRequestor) - .getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow); - domWindow.addEventListener( - "load", - async function () { - domWindow.removeEventListener("load", arguments.callee, false); - if (!found && domWindow.Zotero) { - Zotero = domWindow.Zotero; - resolve(); - } else if ( - domWindow.location.href === - "chrome://zotero/content/zoteroPane.xhtml" - ) { - // Call the hook for the main window load event - // Note that this is not called the first time the window is opened - // (when Zotero is initialized), but only when the window is re-opened - // after being closed - await Zotero.__addonInstance__?.hooks.onMainWindowLoad(domWindow); - } - }, - false, - ); - }, - onCloseWindow: function (aWindow) { - const domWindow = aWindow - .QueryInterface(Ci.nsIInterfaceRequestor) - .getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow); - if ( - domWindow.location.href === "chrome://zotero/content/zoteroPane.xhtml" - ) { - Zotero.__addonInstance__?.hooks.onMainWindowUnload(domWindow); - } - }, - }; - Services.wm.addListener(windowListener); - }); -} - function install(data, reason) {} async function startup({ id, version, resourceURI, rootURI }, reason) { - await waitForZotero(); await Zotero.initializationPromise; // String 'rootURI' introduced in Zotero 7 @@ -117,6 +42,14 @@ async function startup({ id, version, resourceURI, rootURI }, reason) { ); } +async function onMainWindowLoad(window) { + Zotero.__addonInstance__?.hooks.onMainWindowLoad(window); +} + +async function onMainWindowUnload(window) { + Zotero.__addonInstance__?.hooks.onMainWindowUnload(window); +} + function shutdown({ id, version, resourceURI, rootURI }, reason) { if (reason === APP_SHUTDOWN) { return; @@ -128,7 +61,7 @@ function shutdown({ id, version, resourceURI, rootURI }, reason) { Components.interfaces.nsISupports, ).wrappedJSObject; } - Zotero.__addonInstance__.hooks.onShutdown(); + Zotero.__addonInstance__?.hooks.onShutdown(); Cc["@mozilla.org/intl/stringbundle;1"] .getService(Components.interfaces.nsIStringBundleService)