-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbootstrap.js
356 lines (312 loc) · 9.81 KB
/
bootstrap.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
if (typeof Zotero == 'undefined') {
var Zotero;
}
var Zotero_Tabs;
var Zotero_File_Exporter;
var Zotero_File_Interface;
var ZoteroPane_Local;
var Zotero_File_Interface;
var FilePicker;
var fflate;
var ZeNotes;
var setImmediate = function(){};
var Menu;
var Data;
var Database;
var DBPrefs;
var Ui;
var Format;
var Utils
var Filter;
var Image;
var Ai;
var Io;
var CryptoJS;
var Actions;
var Annotations;
var Languages;
var Blob;
var URLSearchParams;
const ANNOTATION = 1;
const ANNOTATION_LABEL = "annotation";
const ATTACHMENT = 3;
const ATTACHMENT_LABEL = "attachment";
const NOTE = 28;
const NOTE_LABEL = "note";
let mainWindowListener;
function log(msg) {
Zotero.debug("ZeNotes: " + msg);
}
function alert(msg)
{
return Zotero.getMainWindow().alert(msg);
}
function confirm(msg)
{
return Zotero.getMainWindow().confirm(msg);
}
function prompt(message)
{
return Zotero.getMainWindow().prompt(msg);
}
function setInterval(func, delay)
{
return Zotero.getMainWindow().setInterval(func, delay);
}
function setTimeout(func, delay)
{
return Zotero.getMainWindow().setTimeout(func, delay);
}
function clearTimeout(timoutid)
{
return Zotero.getMainWindow().clearTimeout(timoutid);
}
function clearInterval(intervalid)
{
return Zotero.getMainWindow().clearInterval(intervalid);
}
// 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() {
if (typeof Zotero != 'undefined') {
await Zotero.initializationPromise;
return;
}
var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
var windows = Services.wm.getEnumerator('navigator:browser');
var found = false;
while (windows.hasMoreElements()) {
let win = windows.getNext();
if (win.Zotero) {
Zotero = win.Zotero;
found = true;
break;
}
}
if (!found) {
await new Promise((resolve) => {
var listener = {
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 (domWindow.Zotero) {
Services.wm.removeListener(listener);
Zotero = domWindow.Zotero;
resolve();
}
}, false);
}
};
Services.wm.addListener(listener);
});
}
await Zotero.initializationPromise;
}
// Adds main window open/close listeners in Zotero 6
function listenForMainWindowEvents() {
mainWindowListener = {
onOpenWindow: function (aWindow) {
Zotero.debug('windowListener:onOpenWindow');
let domWindow = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
async function onload() {
domWindow.removeEventListener("load", onload, false);
if (domWindow.location.href !== "chrome://zotero/content/standalone/standalone.xul") {
return;
}
onMainWindowLoad({ window: domWindow });
}
domWindow.addEventListener("load", onload, false);
},
onCloseWindow: async function (aWindow) {
let domWindow = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
if (domWindow.location.href !== "chrome://zotero/content/standalone/standalone.xul") {
return;
}
onMainWindowUnload({ window: domWindow });
},
};
Services.wm.addListener(mainWindowListener);
}
function removeMainWindowListener() {
if (mainWindowListener) {
Services.wm.removeListener(mainWindowListener);
}
}
// Loads default preferences from prefs.js in Zotero 6
function setDefaultPrefs(rootURI) {
var branch = Services.prefs.getDefaultBranch("");
var obj = {
pref(pref, value) {
switch (typeof value) {
case 'boolean':
branch.setBoolPref(pref, value);
break;
case 'string':
branch.setStringPref(pref, value);
break;
case 'number':
branch.setIntPref(pref, value);
break;
default:
Zotero.logError(`Invalid type '${typeof(value)}' for pref '${pref}'`);
}
}
};
Services.scriptloader.loadSubScript(rootURI + "prefs.js", obj);
}
function registerchrome(rootURI){
var aomStartup = Cc["@mozilla.org/addons/addon-manager-startup;1"].getService(Ci.amIAddonManagerStartup);
var manifestURI = Services.io.newURI(rootURI + "manifest.json");
chromeHandle = aomStartup.registerChrome(manifestURI, [
["content", "zenotes", "content/"],
["locale", "zenotes", "en-US", "chrome/locale/en-US"],
]);
}
function initPreferences(rootURI) {
if(Zotero.platformMajorVersion >= 102) {
registerchrome(rootURI);
Zotero.PreferencePanes.register({
pluginID: 'zenotes@alefa.net',
id: 'zenotes@alefa.net',
stylesheets: [
rootURI + 'content/settings/preferences.css',
rootURI + 'content/settings/preferences7.css',
rootURI + 'content/lib/fontawesome/6.1.1/css/all.min.css',
],
src: rootURI + 'content/settings/preferences.xhtml',
scripts: [
rootURI + 'content/settings/zntable.js',
rootURI + 'content/settings/preferences.js',
rootURI + 'content/settings/preferences7.js',
],
image: rootURI+"/content/images/zenotes-settings.png"
});
}
}
async function install() {
await waitForZotero();
log("Installed ZeNotes");
}
async function startup({ id, version, resourceURI, rootURI = resourceURI.spec }) {
await waitForZotero();
log("Starting Ze-Notes");
initPreferences(rootURI);
Zotero.createXULElement = function(doc, tag) {
let XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
return doc.createElementNS(XUL_NS, tag);
}
// 'Services' may not be available in Zotero 6
if (typeof Services == 'undefined') {
var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
}
if (Zotero.platformMajorVersion < 102) {
// Listen for window load/unload events in Zotero 6, since onMainWindowLoad/Unload don't
// get called
listenForMainWindowEvents();
// Read prefs from prefs.js in Zotero 6
/*
Does not work on Zotero 6 on certain machines
*/
// setDefaultPrefs(rootURI);
}
URLSearchParams = Zotero.getMainWindow().URLSearchParams;
Blob = Zotero.getMainWindow().Blob;
Response = Zotero.getMainWindow().Response;
Services.scriptloader.loadSubScript(rootURI + 'lib/fflate/index.js');
Services.scriptloader.loadSubScript(rootURI + 'lib/CryptoJS 3.1.2/aes.js');
Services.scriptloader.loadSubScript(rootURI + 'core/filepicker6.js');
Services.scriptloader.loadSubScript(rootURI + 'core/settings.js');
Services.scriptloader.loadSubScript(rootURI + 'core/dropbox.js');
Services.scriptloader.loadSubScript(rootURI + 'core/zenotes.js');
Services.scriptloader.loadSubScript(rootURI + 'core/prefs.js');
Services.scriptloader.loadSubScript(rootURI + 'core/database.js');
Services.scriptloader.loadSubScript(rootURI + 'core/dbprefs.js');
Services.scriptloader.loadSubScript(rootURI + 'core/image.js');
Services.scriptloader.loadSubScript(rootURI + 'core/filter.js');
Services.scriptloader.loadSubScript(rootURI + 'core/utils.js');
Services.scriptloader.loadSubScript(rootURI + 'core/ui.js');
Services.scriptloader.loadSubScript(rootURI + 'core/nsync.js');
Services.scriptloader.loadSubScript(rootURI + 'core/menu.js');
Services.scriptloader.loadSubScript(rootURI + 'core/data.js');
Services.scriptloader.loadSubScript(rootURI + 'core/format.js');
Services.scriptloader.loadSubScript(rootURI + 'core/ai.js');
Services.scriptloader.loadSubScript(rootURI + 'core/io.js');
Services.scriptloader.loadSubScript(rootURI + 'core/os.js');
Services.scriptloader.loadSubScript(rootURI + 'core/ocr.js');
Services.scriptloader.loadSubScript(rootURI + 'content/notes/actions.js');
Services.scriptloader.loadSubScript(rootURI + 'core/annotations.js');
Services.scriptloader.loadSubScript(rootURI + 'core/languages.js');
ZeNotes.init({ id, version, rootURI });
Zotero.ZeNotes = ZeNotes;
ZeNotes.addToAllWindows();
Zotero_Tabs = Zotero.getMainWindow().Zotero_Tabs;
Zotero_File_Exporter = Zotero.getMainWindow().Zotero_File_Exporter;
Zotero_File_Interface = Zotero.getMainWindow().Zotero_File_Interface;
ZoteroPane_Local = Zotero.getMainWindow().ZoteroPane_Local;
Zotero_File_Interface = Zotero.getMainWindow().Zotero_File_Interface;
FilePicker = Zotero.getMainWindow().FilePicker;
Menu.addToAllWindows();
ZeNotes.Prefs = Prefs;
ZeNotes.Database = Database;
ZeNotes.DBPrefs = DBPrefs;
await Database.init();
await DBPrefs.init();
ZeNotes.Ui = Ui;
ZeNotes.Menu = Menu;
ZeNotes.Dropbox = Dropbox;
ZeNotes.NSync = NSync;
ZeNotes.Utils = Utils;
ZeNotes.Filter = Filter;
ZeNotes.Image = Image;
ZeNotes.Ai = Ai;
ZeNotes.Io = Io;
ZeNotes.CryptoJS = CryptoJS;
ZeNotes.Actions = Actions;
ZeNotes.Annotations = Annotations;
ZeNotes.Languages = Languages;
ZeNotes.Data = Data;
ZeNotes.Settings = Settings;
ZeNotes.Format = Format;
ZeNotes.Ocr = Ocr;
ZeNotes.Os = Os;
Annotations.initmenu();
Settings.inject();
await ZeNotes.main();
}
function onMainWindowLoad({ window }) {
ZeNotes.addToWindow(window);
}
function onMainWindowUnload({window}) {
ZeNotes.removeFromWindow(window);
}
function shutdown() {
Zotero.log("Shutting down 1.2");
ZeNotes.Database.close();
ZeNotes.DBPrefs.close();
if (Zotero.platformMajorVersion < 102) {
removeMainWindowListener();
}
else
{
chromeHandle.destruct();
}
ZeNotes.removeFromAllWindows();
ZeNotes = undefined;
}
function uninstall() {
// `Zotero` object isn't available in `uninstall()` in Zotero 6, so log manually
if (typeof Zotero == 'undefined') {
dump("ZeNotes: Uninstalled\n\n");
return;
}
log("Uninstalled 1.2");
}