-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbootstrap.js
More file actions
141 lines (120 loc) · 4.6 KB
/
Copy pathbootstrap.js
File metadata and controls
141 lines (120 loc) · 4.6 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
/**
* ZotSeek - Semantic Search for Zotero - Bootstrap
* Based on Zotero's official Make It Red example and BetterNotes plugin.
*/
var chromeHandle;
function install(data, reason) {}
async function startup({ id, version, resourceURI, rootURI }, reason) {
Zotero.debug("[ZotSeek Bootstrap] Waiting for initialization...");
await Zotero.initializationPromise;
Zotero.debug("[ZotSeek Bootstrap] Zotero initialized");
// Register chrome content and locale
Zotero.debug("[ZotSeek Bootstrap] Registering chrome content...");
var aomStartup = Components.classes[
"@mozilla.org/addons/addon-manager-startup;1"
].getService(Components.interfaces.amIAddonManagerStartup);
var manifestURI = Services.io.newURI(rootURI + "manifest.json");
chromeHandle = aomStartup.registerChrome(manifestURI, [
["content", "zotseek", rootURI + "content/"],
["locale", "zotseek", "en-US", rootURI + "locale/en-US/"],
["locale", "zotseek", "zh-CN", rootURI + "locale/zh-CN/"],
]);
Zotero.debug("[ZotSeek Bootstrap] Chrome content and locale registered");
// Create context for the plugin script
// _globalThis allows the script to access this context
const ctx = {
rootURI,
Zotero,
// Provide document for fake browser environment
document: Zotero.getMainWindow()?.document,
};
ctx._globalThis = ctx;
// Load the main script
Zotero.debug("[ZotSeek Bootstrap] Loading main script...");
try {
Services.scriptloader.loadSubScript(
`${rootURI}content/scripts/index.js`,
ctx
);
Zotero.debug("[ZotSeek Bootstrap] Main script loaded");
} catch (e) {
Zotero.debug("[ZotSeek Bootstrap] ERROR loading script: " + e);
Zotero.logError(e);
return;
}
// The script attaches itself to Zotero.ZotSeek
if (Zotero.ZotSeek) {
Zotero.debug("[ZotSeek Bootstrap] Calling onStartup...");
Zotero.ZotSeek.setInfo({ id, version, rootURI });
await Zotero.ZotSeek.hooks.onStartup();
Zotero.debug("[ZotSeek Bootstrap] Startup complete");
} else {
Zotero.debug("[ZotSeek Bootstrap] ERROR: Zotero.ZotSeek not found!");
}
}
function onMainWindowLoad({ window: win }) {
Zotero.ZotSeek?.hooks.onMainWindowLoad(win);
}
function onMainWindowUnload({ window: win }) {
Zotero.ZotSeek?.hooks.onMainWindowUnload(win);
}
function shutdown({ id, version, resourceURI, rootURI }, reason) {
if (reason === APP_SHUTDOWN) {
return;
}
Zotero.ZotSeek?.hooks.onShutdown();
if (chromeHandle) {
chromeHandle.destruct();
chromeHandle = null;
}
}
/**
* Uninstall cleanup - removes the ZotSeek database file
* This ensures no orphaned data remains after plugin removal
*
* Only deletes on true uninstall (ADDON_UNINSTALL = 6), not on upgrade/reload
*/
async function uninstall(data, reason) {
Zotero.debug("[ZotSeek Bootstrap] Uninstall called with reason: " + reason);
// ADDON_UNINSTALL = 6, only delete database on true uninstall
// Don't delete on ADDON_UPGRADE (7), ADDON_DOWNGRADE (8), or during dev reloads
if (reason !== 6) {
Zotero.debug("[ZotSeek Bootstrap] Skipping cleanup (not a true uninstall)");
return;
}
Zotero.debug("[ZotSeek Bootstrap] Performing uninstall cleanup...");
try {
// Delete the ZotSeek database file
const dbPath = PathUtils.join(Zotero.DataDirectory.dir, "zotseek.sqlite");
Zotero.debug("[ZotSeek Bootstrap] Deleting database: " + dbPath);
// Try to detach database first if Zotero.DB is available
if (Zotero.DB) {
try {
await Zotero.DB.queryAsync("DETACH DATABASE zotseek");
Zotero.debug("[ZotSeek Bootstrap] Database detached");
} catch (e) {
// Database may not be attached, that's fine
Zotero.debug("[ZotSeek Bootstrap] Database not attached (ok): " + e);
}
}
// Delete the database file
await IOUtils.remove(dbPath, { ignoreAbsent: true });
Zotero.debug("[ZotSeek Bootstrap] Database file deleted");
// Also delete any related files (journal, wal, shm)
await IOUtils.remove(dbPath + "-journal", { ignoreAbsent: true });
await IOUtils.remove(dbPath + "-wal", { ignoreAbsent: true });
await IOUtils.remove(dbPath + "-shm", { ignoreAbsent: true });
// Clear preferences
const prefBranch = Services.prefs.getBranch("extensions.zotero.zotseek.");
try {
prefBranch.deleteBranch("");
Zotero.debug("[ZotSeek Bootstrap] Preferences cleared");
} catch (e) {
Zotero.debug("[ZotSeek Bootstrap] Could not clear preferences: " + e);
}
Zotero.debug("[ZotSeek Bootstrap] Uninstall cleanup complete");
} catch (e) {
Zotero.debug("[ZotSeek Bootstrap] Uninstall error: " + e);
Zotero.logError(e);
}
}