-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrap.js
289 lines (258 loc) · 9.99 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
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
/*
* Function to pick up Zotero and tinker with it.
*/
var Zotero;
var AbbrevsFilter;
var AbbrevsFilterFactory;
var AbbrevsService;
// Preferences
// From https://developer.mozilla.org/en-US/Add-ons/How_to_convert_an_overlay_extension_to_restartless
function getGenericPref(branch,prefName)
{
switch (branch.getPrefType(prefName))
{
default:
case 0: return undefined; // PREF_INVALID
case 32: return getCharPref(prefName); // PREF_STRING
case 64: return branch.getIntPref(prefName); // PREF_INT
case 128: return branch.getBoolPref(prefName); // PREF_BOOL
}
}
function setGenericPref(branch,prefName,prefValue)
{
switch (typeof prefValue)
{
case "string":
branch.setCharPref(prefName,prefValue);
return;
case "number":
branch.setIntPref(prefName,prefValue);
return;
case "boolean":
branch.setBoolPref(prefName,prefValue);
return;
}
}
function setDefaultPref(prefName,prefValue)
{
var defaultBranch = Services.prefs.getDefaultBranch(null);
setGenericPref(defaultBranch,prefName,prefValue);
}
const consoleService = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
function logMessage(msg) {
consoleService.logStringMessage("AFZ msg: " + msg);
}
// [JavaScript Error: "NS_NOINTERFACE: Component returned failure code: 0x80004002 (NS_NOINTERFACE) [nsIPrefBranch.setComplexValue]" {file: "resource://gre/modules/addons/XPIProvider.jsm -> file:///media/storage/src/JM/zotero-standalone-build/staging/Jurism_linux-x86_64/extensions/[email protected]/bootstrap.js" line: 58}]
var initializePlugin = async function() {
Zotero = Components.classes["@zotero.org/Zotero;1"]
.getService(Components.interfaces.nsISupports)
.wrappedJSObject;
if (AbbrevsService && AbbrevsFilterFactory) {
const registrar = Components.manager.QueryInterface(Components.utils.nsIComponentRegistrar);
registrar.unregisterFactory(AbbrevsService.prototype.classID,
AbbrevsFilterFactory);
}
// Set up preferences
Services.scriptloader.loadSubScript("chrome://abbrevs-filter/content/defaultprefs.js",
{pref:setDefaultPref} );
// Empty context for build
var buildContext = {
Zotero: Zotero
};
// Build and instantiate the component
var xpcomFiles = [
"component",
"utils",
"save",
"cache",
"getabbrev",
"initializers",
"attachers",
"import",
"export"
];
for (var i=0, ilen=xpcomFiles.length; i < ilen; i += 1) {
Services.scriptloader.loadSubScript("chrome://abbrevs-filter/content/xpcom/" + xpcomFiles[i] + ".js", buildContext);
}
AbbrevsService = function () {
this.wrappedJSObject = new buildContext.AbbrevsFilter();
};
// Define the service
AbbrevsService.prototype = {
classDescription: 'Juris-M Abbreviation Filter',
contractID: '@juris-m.github.io/abbrevs-filter;1',
classID: Components.ID("{e2731ad0-8426-11e0-9d78-0800200c5798}"),
service: true,
QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsISupports])
}
// Plugin factory
AbbrevsFilterFactory = Object.freeze({
createInstance: function(aOuter, aIID) {
if (aOuter) { throw Components.results.NS_ERROR_NO_AGGREGATION; }
return new AbbrevsService();
},
loadFactory: function (aLock) { /* unused */ },
QueryInterface: XPCOMUtils.generateQI([Components.utils.nsIFactory])
});
const registrar = Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar);
registrar.registerFactory(AbbrevsService.prototype.classID,
AbbrevsService.prototype.classDescription,
AbbrevsService.prototype.contractID,
AbbrevsFilterFactory);
AbbrevsFilter = Components.classes['@juris-m.github.io/abbrevs-filter;1'].getService(Components.interfaces.nsISupports).wrappedJSObject;
// logMessage("Begin initComponent");
await AbbrevsFilter.initComponent(Zotero);
// logMessage("End initComponent");
}.bind(this);
function domListener (event) {
var doc = event.target;
if (doc.getElementById("abbrevs-button")) return;
// To be executed when opening CSL Editor or one of the integration plugins
if (doc.getElementById('abbrevs-button')) return;
if (doc.getElementById('automaticJournalAbbreviations-checkbox')) {
var checkboxElem = doc.getElementById('automaticJournalAbbreviations-checkbox');
checkboxElem.setAttribute('label', 'Abbreviation Filter installed (overrides Medline abbrevs)');
checkboxElem.disabled = true;
var description = checkboxElem.nextSibling
description.innerHTML = 'To use the default Medline abbreviations, disable the Abbreviation Filter and restart Zotero.';
return;
} else if (doc.documentElement.getAttribute('id') === 'csl-edit') {
Zotero = Components.classes["@zotero.org/Zotero;1"].getService(Components.interfaces.nsISupports).wrappedJSObject;
var AbbrevsFilter = Components.classes['@juris-m.github.io/abbrevs-filter;1'].getService(Components.interfaces.nsISupports).wrappedJSObject;
AbbrevsFilter.initWindow(doc.defaultView, doc);
var hasEngine = false;
var refresh = doc.getElementById("preview-refresh-button");
var cslmenu = doc.getElementById("zotero-csl-list");
var csleditor = doc.getElementById("zotero-csl-editor");
var button = doc.createElement("button");
button.setAttribute("label", "Abbrevs.");
button.setAttribute("id","abbrevs-button");
button.setAttribute('disabled','true');
cslmenu.parentNode.insertBefore(button, null);
function attachStyleEngine () {
if (hasEngine) return;
var button = doc.getElementById('abbrevs-button');
var items = Zotero.getActiveZoteroPane().getSelectedItems();
if (items.length > 0) {
button.removeAttribute('disabled');
button.addEventListener("command", function() {
var io = {
style:csleditor.styleEngine,
AFZ: AbbrevsFilter
};
io.wrappedJSObject = io;
doc.defaultView.openDialog('chrome://abbrevs-filter/content/dialog.xul', 'AbbrevsFilterDialog', 'chrome,centerscreen,alwaysRaised,modal',io);
}, false);
hasEngine = true;
}
}
attachStyleEngine();
cslmenu.addEventListener("command", attachStyleEngine, false);
refresh.addEventListener("command", attachStyleEngine, false);
button.addEventListener("command", attachStyleEngine, false);
} else if (doc.getElementById("zotero-add-citation-dialog") || doc.getElementById("quick-format-search")) {
var stringBundle = Components.classes["@mozilla.org/intl/stringbundle;1"]
.getService(Components.interfaces.nsIStringBundleService)
.createBundle("chrome://abbrevs-filter/locale/overlay.properties")
var AbbrevsFilter = Components.classes['@juris-m.github.io/abbrevs-filter;1'].getService(Components.interfaces.nsISupports).wrappedJSObject;
AbbrevsFilter.initWindow(doc.defaultView, doc);
var io = doc.defaultView.arguments[0].wrappedJSObject;
io = {
style:io.style,
AFZ: AbbrevsFilter
}
io.wrappedJSObject = io;
function processorIsLoaded() {
if (io.wrappedJSObject.style.registry.citationreg.citationByIndex.length) {
return true;
} else {
return false;
}
}
function abbrevsPopup() {
doc.defaultView.openDialog('chrome://abbrevs-filter/content/dialog.xul',
'AbbrevsFilterDialog',
'chrome,centerscreen,alwaysRaised,modal',
io);
}
function makeButtonBox() {
var bx = doc.createElement("hbox");
var button = doc.createElement("button");
button.setAttribute("type", "button");
button.setAttribute("label", "Abbrevs.");
button.setAttribute("id", "abbrevs-button");
button.setAttribute("disabled", "true");
button.setAttribute("tooltiptext", "Close and refresh to enable Abbrevs");
bx.appendChild(button);
return bx;
}
function attachButton() {
var bx = makeButtonBox();
var dialog = doc.getElementById("zotero-add-citation-dialog");
if (dialog) {
var vbox = doc.getElementById("zotero-select-items-container");
dialog.insertBefore(bx, vbox);
} else {
var spinner = doc.getElementById("quick-format-spinner");
bx.setAttribute("style", "border:1px solid #ffffff;line-height:normal;display:inline-block;");
spinner.parentNode.insertBefore(bx, null);
}
if (processorIsLoaded()) {
var button = doc.getElementById("abbrevs-button");
button.removeAttribute("disabled");
button.removeAttribute("tooltiptext");
button.addEventListener("click", abbrevsPopup);
}
}
attachButton();
}
event.target.removeEventListener(event.type, arguments.callee);
}
var startupObserver = {
observe: function(subject, topic, data) {
initializePlugin();
},
register: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
observerService.addObserver(this, "final-ui-startup", false);
},
unregister: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
observerService.removeObserver(this, "final-ui-startup");
}
}
var popupObserver = {
observe: function(subject, topic, data) {
var target = subject.QueryInterface(Components.interfaces.nsIDOMWindow);
target.addEventListener("DOMContentLoaded", domListener, false);
},
register: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
observerService.addObserver(this, "chrome-document-global-created", false);
},
unregister: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
observerService.removeObserver(this, "chrome-document-global-created");
}
}
/*
* Bootstrap functions
*/
function startup (data, reason) {
startupObserver.register();
popupObserver.register();
};
function shutdown (data, reason) {
if (AbbrevsFilter.db) {
AbbrevsFilter.db.closeDatabase(true);
}
}
function install (data, reason) {}
function uninstall (data, reason) {}