-
Notifications
You must be signed in to change notification settings - Fork 7
/
bootstrap.ts
87 lines (75 loc) · 2.69 KB
/
bootstrap.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* eslint-disable @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/ban-types, prefer-rest-params, @typescript-eslint/no-unnecessary-type-assertion, no-throw-literal, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function */
declare const Components: any
declare var Services: any // eslint-disable-line no-var
declare const dump: (msg: string) => void
var Zotero // eslint-disable-line no-var
var notifier // eslint-disable-line no-var
var ChromeUtils // eslint-disable-line no-var
Components.utils.importGlobalProperties(['fetch'])
Components.utils.import('resource://gre/modules/Services.jsm')
function debug(msg) {
msg = `PMCID: (bootstrap) ${msg}`
if (Zotero) {
Zotero.debug(msg)
}
else {
dump(`${msg}\n`)
}
}
const classname = 'fetch-pmcid'
async function waitForZotero() {
debug('waitForZotero')
if (typeof Zotero != 'undefined') {
await Zotero.initializationPromise
return
}
if (typeof Services == 'undefined') {
({ Services } = ChromeUtils.import('resource://gre/modules/Services.jsm'))
}
const windows = Services.wm.getEnumerator('navigator:browser')
let found = false
while (windows.hasMoreElements()) {
const win = windows.getNext()
if (win.Zotero) {
Zotero = win.Zotero
found = true
break
}
}
if (!found) {
await new Promise(resolve => {
const listener = {
onOpenWindow: aWindow => {
// Wait for the window to finish loading
const domWindow = aWindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIDOMWindowInternal || Components.interfaces.nsIDOMWindow)
domWindow.addEventListener('load', () => {
domWindow.removeEventListener('load', arguments.callee, false) // eslint-disable-line no-caller
if (domWindow.Zotero) {
Services.wm.removeListener(listener)
Zotero = domWindow.Zotero
resolve(undefined)
}
}, false)
},
}
Services.wm.addListener(listener)
})
}
await Zotero.initializationPromise
}
// --- //
export function install(_data, _reason) { }
export async function startup({ resourceURI, rootURI = resourceURI.spec }) {
debug('waiting for zotero')
await waitForZotero()
debug('zotero loaded, loading lib')
Services.scriptloader.loadSubScript(`${rootURI}lib.js`, { Zotero })
debug('zotero loaded, lib loaded')
Zotero.PMCIDFetcher.startup()
debug('lib started')
}
export function shutdown(_data, _reason) {
Zotero.PMCIDFetcher.shutdown()
}
export function uninstall(_data, _reason) { }