Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: night for standalone reader window #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions content/zotero-night.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -348,6 +353,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
Expand Down Expand Up @@ -495,6 +550,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')
}
}
Expand Down