-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathbootstrap.ts
More file actions
141 lines (121 loc) · 4.08 KB
/
bootstrap.ts
File metadata and controls
141 lines (121 loc) · 4.08 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* eslint-disable prefer-arrow/prefer-arrow-functions, no-var, @typescript-eslint/no-unused-vars, no-caller, @typescript-eslint/explicit-module-boundary-types */
declare const ChromeUtils: any
declare const Cc: any
declare const Ci: any
declare const Services: any
declare function dump(msg: string): void
// Log immediately when file is loaded
dump('[Scite Zotero] bootstrap.js file loaded!\n')
if (typeof Zotero == 'undefined') {
var Zotero
}
function log(msg) {
msg = `[Scite Zotero] bootstrap: ${msg}`
dump(msg + '\n')
if (typeof Zotero !== 'undefined' && Zotero.logError) {
Zotero.logError(msg)
}
}
export function onMainWindowLoad({ window }) {
log('onMainWindowLoad')
window.MozXULElement.insertFTLIfNeeded('scite-zotero-plugin.ftl')
}
async function waitForZotero() {
if (typeof Zotero != 'undefined') {
await Zotero.initializationPromise
return
}
// Services should be available globally in Zotero 7+
var windows = Services.wm.getEnumerator('navigator:browser')
var found = false
while (windows.hasMoreElements()) {
const win = windows.getNext()
if (win.Zotero) {
Zotero = win.Zotero
found = true
break
}
}
if (!found) {
await new Promise(resolve => {
var listener = {
onOpenWindow(aWindow) {
// Wait for the window to finish loading
const domWindow = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow)
domWindow.addEventListener('load', function() {
domWindow.removeEventListener('load', arguments.callee, false)
if (domWindow.Zotero) {
Services.wm.removeListener(listener)
Zotero = domWindow.Zotero
resolve(undefined)
}
}, false)
},
}
Services.wm.addListener(listener)
})
}
await Zotero.initializationPromise
}
async function install() {
await waitForZotero()
log('Installed Scite Plugin')
}
let chromeHandle
async function startup({ id, version, resourceURI, rootURI = resourceURI?.spec }) {
dump(`[Scite Zotero] startup called, rootURI: ${rootURI}\n`)
try {
dump('[Scite Zotero] Waiting for Zotero...\n')
await waitForZotero()
Zotero.debug('[Scite Zotero] Zotero ready, starting plugin...')
Zotero.debug('[Scite Zotero] Getting addon manager startup...')
var aomStartup = Cc['@mozilla.org/addons/addon-manager-startup;1'].getService(Ci.amIAddonManagerStartup)
var manifestURI = Services.io.newURI(rootURI + 'client/manifest.json')
Zotero.debug('[Scite Zotero] Registering chrome...')
chromeHandle = aomStartup.registerChrome(manifestURI, [
[ 'content', 'scite-zotero-plugin', rootURI + 'content/' ],
[ 'locale', 'scite-zotero-plugin', 'en-US', rootURI + 'locale/en-US/' ],
])
Zotero.debug(`[Scite Zotero] Loading lib.js from ${rootURI}lib.js`)
Services.scriptloader.loadSubScript(`${rootURI}lib.js`)
Zotero.debug('[Scite Zotero] lib.js loaded, starting Scite...')
if (!Zotero.Scite) {
Zotero.debug('[Scite Zotero] ERROR: Zotero.Scite is not defined after loading lib.js!')
return
}
await Zotero.Scite.start(rootURI)
Zotero.debug('[Scite Zotero] Started successfully!')
const $window = Zotero.getMainWindow()
onMainWindowLoad({ window: $window })
}
catch (err) {
dump(`[Scite Zotero] Error during startup: ${err}\n`)
if (typeof Zotero !== 'undefined') {
Zotero.debug(`[Scite Zotero] Error during startup: ${err}`)
if (Zotero.logError) {
Zotero.logError('[Scite Zotero] Error during startup')
Zotero.logError(err)
}
}
}
}
function shutdown() {
log('Shutting down')
if (typeof Zotero !== 'undefined' && Zotero.Scite) {
if (typeof Zotero.Scite.unload === 'function') {
Zotero.Scite.unload().catch(err => {
if (Zotero.logError) Zotero.logError(err)
})
}
Zotero.Scite = undefined
}
if (chromeHandle) {
chromeHandle.destruct()
chromeHandle = null
}
}
function uninstall() {
log('Uninstalled')
}
export { install, startup, shutdown, uninstall }