-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
140 lines (124 loc) · 5.18 KB
/
background.js
File metadata and controls
140 lines (124 loc) · 5.18 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
var EXPORTED_SYMBOLS = ["shelfAPI"];
var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
var { ExtensionCommon } = ChromeUtils.import("resource://gre/modules/ExtensionCommon.jsm");
var shelfAPI = class extends ExtensionCommon.ExtensionAPI {
getAPI(context) {
// Global ZotShelf object will be initialized when the main script loads
if (typeof ZotShelf === 'undefined') {
// Initialize a placeholder that will be replaced by the main script
this.ZotShelf = {
_initialized: false,
_pendingCalls: []
};
}
return {
shelf: {
async init() {
try {
Services.console.logStringMessage("ZotShelf API: init() called");
// Wait for Zotero to be ready
if (!Zotero || !Zotero.initialized) {
Services.console.logStringMessage("ZotShelf API: Waiting for Zotero to initialize...");
await new Promise(resolve => {
const checkZotero = () => {
if (Zotero && Zotero.initialized) {
resolve();
} else {
setTimeout(checkZotero, 100);
}
};
checkZotero();
});
}
// Load the main ZotShelf script into Zotero's context
const scriptURL = context.extension.getURL("content/zotshelf.js");
Services.console.logStringMessage("ZotShelf API: Loading main script from " + scriptURL);
// Create a script element and inject it
const doc = Zotero.getMainWindow().document;
const script = doc.createElement('script');
script.type = 'module';
script.src = scriptURL;
// Add extension context to global scope for the script
Zotero.getMainWindow().ZotShelfExtension = context.extension;
return new Promise((resolve, reject) => {
script.onload = () => {
Services.console.logStringMessage("ZotShelf API: Main script loaded successfully");
resolve(true);
};
script.onerror = (error) => {
Services.console.logStringMessage("ZotShelf API: Error loading main script: " + error);
reject(error);
};
doc.head.appendChild(script);
});
} catch (e) {
Services.console.logStringMessage("ZotShelf API: Error in init: " + e);
throw e;
}
},
async getEpubItems(collectionID) {
try {
// Ensure ZotShelf is available in the main window
const mainWindow = Zotero.getMainWindow();
if (mainWindow.ZotShelf && mainWindow.ZotShelf.getEpubItems) {
return await mainWindow.ZotShelf.getEpubItems(collectionID);
}
Services.console.logStringMessage("ZotShelf API: ZotShelf not available in main window");
return [];
} catch (e) {
Services.console.logStringMessage("ZotShelf API: Error in getEpubItems: " + e);
return [];
}
},
async openEpubItem(itemID) {
try {
const mainWindow = Zotero.getMainWindow();
if (mainWindow.ZotShelf && mainWindow.ZotShelf.openEpubItem) {
return await mainWindow.ZotShelf.openEpubItem(itemID);
}
return false;
} catch (e) {
Services.console.logStringMessage("ZotShelf API: Error in openEpubItem: " + e);
return false;
}
},
getCachedCover(itemID) {
try {
const mainWindow = Zotero.getMainWindow();
if (mainWindow.ZotShelf && mainWindow.ZotShelf.getCachedCover) {
return mainWindow.ZotShelf.getCachedCover(itemID);
}
return null;
} catch (e) {
Services.console.logStringMessage("ZotShelf API: Error in getCachedCover: " + e);
return null;
}
},
cacheCover(itemID, coverData) {
try {
const mainWindow = Zotero.getMainWindow();
if (mainWindow.ZotShelf && mainWindow.ZotShelf.cacheCover) {
return mainWindow.ZotShelf.cacheCover(itemID, coverData);
}
return false;
} catch (e) {
Services.console.logStringMessage("ZotShelf API: Error in cacheCover: " + e);
return false;
}
},
setCollection(collectionID) {
try {
const mainWindow = Zotero.getMainWindow();
if (mainWindow.ZotShelf && mainWindow.ZotShelf.setCollection) {
return mainWindow.ZotShelf.setCollection(collectionID);
}
return false;
} catch (e) {
Services.console.logStringMessage("ZotShelf API: Error in setCollection: " + e);
return false;
}
}
}
};
}
};