From 4dd1b838279ac0f851ffc66b2e457c223f0cdc60 Mon Sep 17 00:00:00 2001 From: windingwind Date: Tue, 26 Apr 2022 16:30:07 +0800 Subject: [PATCH] feat: night for standalone reader window --- content/zotero-night.ts | 69 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/content/zotero-night.ts b/content/zotero-night.ts index f5deaea..dc43c65 100644 --- a/content/zotero-night.ts +++ b/content/zotero-night.ts @@ -13,6 +13,11 @@ import { debug } from './debug' declare const Zotero: any declare const ZoteroContextPane: any declare const Zotero_Tabs: any +declare class ReaderObj { + itemID: number + _iframeWindow: Window + _initPromise: any +} // declare const Components: any const monkey_patch_marker = 'NightMonkeyPatched' @@ -279,6 +284,56 @@ class Night { return name } + public getWindowReaders(): ReaderObj[] { + const windowReaders: ReaderObj[] = [] + const tabIDs = Zotero_Tabs._tabs.map((e) => e.id as string) + for (const reader of Zotero.Reader._readers) { + let flag = false + for (const tabID of tabIDs) { + if (reader.tabID === tabID) { + flag = true + break + } + } + if (!flag) { + windowReaders.push(reader as ReaderObj) + } + } + return windowReaders + } + + public async addEverythingForStandaloneWindowReaders() { + const readers: ReaderObj[] = this.getWindowReaders() + for (const reader of readers) { + await reader._initPromise + const tabWindow = reader._iframeWindow + debug(tabWindow) + debug('Added standalone window') + debug( + `Added standalone window readystate is ${tabWindow.document.readyState}` + ) + switch (tabWindow.document.readyState) { + // @ts-expect-error + case 'uninitialized': { + setTimeout(() => { + tabWindow.document.onreadystatechange = () => + debug('in readystatechange eventlistener:') + + debug( + `Added standalone windw readystate is ${tabWindow.document.readyState}` + ) + + this.addEverythingForTab(tabWindow) + return + }, 300) + } + case 'complete': { + this.addEverythingForTab(tabWindow) + } + } + } + } + private addEverythingForTab(tabWindow: Window) { const doc = tabWindow.document // if (doc.querySelector('#pageStyle')) return @@ -551,6 +606,20 @@ class Night { } Zotero.Notifier.registerObserver(notifierCallback, ['tab']) + const windowNotifierCallback = { + notify: async (event: string, type, ids: string[], extraData) => { + if ( + (event === 'close' && type === 'tab') || + (event === 'open' && type === 'file') + ) { + debug('zotero-night: open window event detected.') + debug('Trying to find window') + await this.addEverythingForStandaloneWindowReaders() + } + } + } + Zotero.Notifier.registerObserver(windowNotifierCallback, ['tab', 'file']) + this.strings = globals.document.getElementById('zotero-night-strings') } }