From 27f171e30f788271e4709e002260100f3c641a64 Mon Sep 17 00:00:00 2001 From: windingwind <33902321+windingwind@users.noreply.github.com> Date: Sun, 27 Aug 2023 11:43:58 +0800 Subject: [PATCH] fix: preview bug with z7 beta 28+ fix: #120 fix: #117 fix: #115 fix: #113 fix: #110 --- addon/bootstrap.js | 85 +++------------------------- addon/chrome/content/previewPDF.html | 75 ++++++++++++------------ src/addon.ts | 46 ++------------- src/hooks.ts | 25 +++++--- src/index.ts | 21 ++++--- src/modules/split.ts | 9 ++- src/utils/ztoolkit.ts | 50 ++++++++++++++++ typings/global.d.ts | 12 +++- 8 files changed, 143 insertions(+), 180 deletions(-) create mode 100644 src/utils/ztoolkit.ts diff --git a/addon/bootstrap.js b/addon/bootstrap.js index 00e7758..0bbfc06 100644 --- a/addon/bootstrap.js +++ b/addon/bootstrap.js @@ -5,80 +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 - let domWindow = aWindow - .QueryInterface(Ci.nsIInterfaceRequestor) - .getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow); - domWindow.addEventListener( - "load", - 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" - ) { - Zotero.__addonInstance__?.hooks.onMainWindowLoad(domWindow); - domWindow.addEventListener("unload", function () { - domWindow.removeEventListener( - "unload", - arguments.callee, - false, - ); - Zotero.__addonInstance__?.hooks.onMainWindowUnload(domWindow); - }); - } - }, - false, - ); - }, - }; - 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 @@ -111,18 +42,20 @@ async function startup({ id, version, resourceURI, rootURI }, reason) { ); } +async function onMainWindowLoad({ window }, reason) { + Zotero.__addonInstance__?.hooks.onMainWindowLoad(window); +} + +async function onMainWindowUnload({ window }, reason) { + Zotero.__addonInstance__?.hooks.onMainWindowUnload(window); +} + function shutdown({ id, version, resourceURI, rootURI }, reason) { if (reason === APP_SHUTDOWN) { return; } - Services.wm.removeListener(windowListener); - if (typeof Zotero === "undefined") { - Zotero = Components.classes["@zotero.org/Zotero;1"].getService( - Components.interfaces.nsISupports, - ).wrappedJSObject; - } - Zotero.__addonInstance__.hooks.onShutdown(); + Zotero.__addonInstance__?.hooks.onShutdown(); Cc["@mozilla.org/intl/stringbundle;1"] .getService(Components.interfaces.nsIStringBundleService) diff --git a/addon/chrome/content/previewPDF.html b/addon/chrome/content/previewPDF.html index 013cbcd..8f14bdf 100644 --- a/addon/chrome/content/previewPDF.html +++ b/addon/chrome/content/previewPDF.html @@ -9,6 +9,10 @@ - + + - +