11/* eslint-disable prefer-arrow/prefer-arrow-functions, no-var, @typescript-eslint/no-unused-vars, no-caller */
22
3- declare const dump : ( msg : string ) => void
3+ declare const Zotero : any
4+ declare const Services : any
45declare const Components : any
5- declare const ChromeUtils : any
66
7- var Services : any
8-
9- if ( typeof Zotero == 'undefined' ) {
10- var Zotero
11- }
12-
13- function log ( msg ) {
7+ function log ( msg : string ) : void {
148 Zotero . debug ( `EdTechHub: (bootstrap) ${ msg } ` )
159}
1610
17- // In Zotero 6, bootstrap methods are called before Zotero is initialized, and using include.js
18- // to get the Zotero XPCOM service would risk breaking Zotero startup. Instead, wait for the main
19- // Zotero window to open and get the Zotero object from there.
20- //
21- // In Zotero 7, bootstrap methods are not called until Zotero is initialized, and the 'Zotero' is
22- // automatically made available.
23- async function waitForZotero ( ) {
24- if ( typeof Zotero != 'undefined' ) {
25- await Zotero . initializationPromise
26- return
27- }
28-
29- // eslint-disable-next-line @typescript-eslint/no-shadow
30- Services = ChromeUtils . import ( 'resource://gre/modules/Services.jsm' ) . Services
31- var windows = Services . wm . getEnumerator ( 'navigator:browser' )
32- var found = false
33- while ( windows . hasMoreElements ( ) ) {
34- const win = windows . getNext ( )
35- if ( win . Zotero ) {
36- Zotero = win . Zotero
37- found = true
38- break
39- }
40- }
41- if ( ! found ) {
42- await new Promise ( resolve => {
43- var listener = {
44- onOpenWindow ( aWindow ) {
45- // Wait for the window to finish loading
46- const domWindow = aWindow . QueryInterface ( Components . interfaces . nsIInterfaceRequestor )
47- . getInterface ( Components . interfaces . nsIDOMWindowInternal || Components . interfaces . nsIDOMWindow )
48- domWindow . addEventListener ( 'load' , function ( ) {
49- domWindow . removeEventListener ( 'load' , arguments . callee , false )
50- if ( domWindow . Zotero ) {
51- Services . wm . removeListener ( listener )
52- Zotero = domWindow . Zotero
53- resolve ( undefined )
54- }
55- } , false )
56- } ,
57- }
58- Services . wm . addListener ( listener )
59- } )
60- }
11+ async function waitForZotero ( ) : Promise < void > {
6112 await Zotero . initializationPromise
6213}
6314
6415
65- // Loads default preferences from prefs.js in Zotero 6
66- function setDefaultPrefs ( rootURI ) {
67- var branch = Services . prefs . getDefaultBranch ( '' )
68- var obj = {
69- pref ( pref , value ) {
70- switch ( typeof value ) {
71- case 'boolean' :
72- branch . setBoolPref ( pref , value )
73- break
74- case 'string' :
75- branch . setStringPref ( pref , value )
76- break
77- case 'number' :
78- branch . setIntPref ( pref , value )
79- break
80- default :
81- Zotero . logError ( `Invalid type '${ typeof ( value ) } ' for pref '${ pref } '` )
82- }
83- } ,
84- }
85- Services . scriptloader . loadSubScript ( `${ rootURI } prefs.js` , obj )
86- }
87-
88-
8916export async function install ( ) : Promise < void > {
9017 await waitForZotero ( )
9118 log ( 'Installed' )
9219}
9320
94- let chromeHandle
21+ let chromeHandle : any
9522export async function startup ( { id, version, resourceURI, rootURI = resourceURI . spec } ) : Promise < void > {
9623 await waitForZotero ( )
9724
9825 try {
9926 log ( `Starting ${ rootURI } ` )
10027
101- if ( Zotero . platformMajorVersion >= 102 ) { // eslint-disable-line @typescript-eslint/no-magic-numbers
102- const aomStartup = Components . classes [ '@mozilla.org/addons/addon-manager-startup;1' ] . getService ( Components . interfaces . amIAddonManagerStartup )
103- const manifestURI = Services . io . newURI ( `${ rootURI } manifest.json` )
104- chromeHandle = aomStartup . registerChrome ( manifestURI , [
105- [ 'content' , 'zotero-edtechhub' , 'content/' ] ,
106- [ 'locale' , 'zotero-edtechhub' , 'en-US' , 'locale/en-US/' ] ,
107- ] )
108- }
109-
110- // 'Services' may not be available in Zotero 6
111- if ( typeof Services == 'undefined' ) {
112- // eslint-disable-next-line @typescript-eslint/no-shadow
113- Services = ChromeUtils . import ( 'resource://gre/modules/Services.jsm' ) . Services
114- }
115-
116- // Read prefs from prefs.js when the plugin in Zotero 6
117- /*
118- if (Zotero.platformMajorVersion < 102) { // eslint-disable-line @typescript -eslint/no-magic-numbers
119- setDefaultPrefs(rootURI)
120- }
121- */
28+ const aomStartup = Components . classes [ '@mozilla.org/addons/addon-manager-startup;1' ] . getService ( Components . interfaces . amIAddonManagerStartup )
29+ const manifestURI = Services . io . newURI ( `${ rootURI } manifest.json` )
30+ chromeHandle = aomStartup . registerChrome ( manifestURI , [
31+ [ 'content' , 'zotero-edtechhub' , 'content/' ] ,
32+ [ 'locale' , 'zotero-edtechhub' , 'en-US' , 'locale/en-US/' ] ,
33+ ] )
12234
12335 log ( 'loading lib' )
12436 Services . scriptloader . loadSubScript ( `${ rootURI } lib.js` , { Zotero } )
@@ -130,6 +42,19 @@ export async function startup({ id, version, resourceURI, rootURI = resourceURI.
13042 }
13143}
13244
45+ // Window hooks for Zotero 7+
46+ export function onMainWindowLoad ( { window } : { window : Window } ) : void {
47+ log ( 'Main window loaded' )
48+ if ( Zotero . EdTechHub ) {
49+ Zotero . EdTechHub . ui ( window )
50+ }
51+ }
52+
53+ export function onMainWindowUnload ( { window } : { window : Window } ) : void {
54+ log ( 'Main window unloading' )
55+ // Cleanup is handled in shutdown
56+ }
57+
13358export function shutdown ( ) : void {
13459 log ( 'Shutting down' )
13560
@@ -150,11 +75,5 @@ export function shutdown(): void {
15075}
15176
15277export function uninstall ( ) : void {
153- // `Zotero` object isn't available in `uninstall()` in Zotero 6, so log manually
154- if ( typeof Zotero == 'undefined' ) {
155- dump ( 'EdTechHub: Uninstalled\n\n' )
156- return
157- }
158-
15978 log ( 'Uninstalled' )
16079}
0 commit comments