-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathbackground.js
More file actions
409 lines (364 loc) · 12.2 KB
/
background.js
File metadata and controls
409 lines (364 loc) · 12.2 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import { exportItems } from "./sources/translateEngine.js";
// Provide a minimal compatibility shim: if `browser` is missing, alias it to `chrome`.
if (typeof browser === "undefined" && typeof chrome !== "undefined") {
globalThis.browser = chrome;
}
var tabInfo = new Map();
/*
Show/hide import button for all tabs (when add-on is loaded).
*/
browser.tabs.query({}).then((tabs) => {
console.log("JabRef: Inject into open tabs %o", tabs);
for (let tab of tabs) {
installInTab(tab);
}
});
/*
Show/hide import button for the currently active tab, whenever the user navigates.
*/
browser.tabs.onUpdated.addListener((tabId, changeInfo, _tab) => {
if (!changeInfo.url) {
return;
}
browser.tabs
.query({
active: true,
currentWindow: true,
})
.then((tabs) => {
if (tabId === tabs[0].id) {
var tab = tabs[0];
installInTab(tab);
}
});
});
/*
Remove translator information when tab is closed.
*/
browser.tabs.onRemoved.addListener((tabId, _removeInfo) => {
tabInfo.delete(tabId);
});
/*
Disable add-on for special browser pages
*/
function isDisabledForURL(url) {
return (
url.includes("chrome://") ||
url.includes("about:") ||
(url.includes("-extension://") && !url.includes("/test/"))
);
}
function getDocumentContentType() {
return document.contentType;
}
/*
Searches for translators for the given tab and shows/hides the import button accordingly.
Zotero.Connector_Browser.onPageLoad is the original function from the Zotero Connector,
see https://github.com/zotero/zotero-connectors/blob/dac609fb9dea1e98dbcc73387b05f7af5ef7814d/src/browserExt/background.js#L968.
*/
function installInTab(tab) {
if (isDisabledForURL(tab.url)) {
return;
}
// Reset tab info
tabInfo.delete(tab.id);
// We cannot inject content scripts into PDF: https://bugzilla.mozilla.org/show_bug.cgi?id=1454760
// Thus, our detection algorithm silently fails in this case
// Try to detect these situations by calling a content script; this fails
browser.scripting
.executeScript({
target: { tabId: tab.id },
func: getDocumentContentType,
})
.then((_result) => {
lookForTranslators(tab);
tabInfo.set(tab.id, { isPDF: false });
})
.catch((error) => {
console.debug(`JabRef: Error calling content script: ${error}`);
// Assume a PDF is displayed in this tab
browser.pageAction.show(tab.id);
browser.pageAction.setTitle({
tabId: tab.id,
title: "Import references into JabRef as PDF",
});
tabInfo.set(tab.id, { isPDF: true });
});
}
/*
Looks for potential translators for the given tab.
*/
async function lookForTranslators(tab) {
console.log("JabRef: Searching for translators for %o", tab);
await initTranslateEngine(tab);
const response = await browser.tabs.sendMessage(tab.id, {
type: "detectTranslators",
url: tab.url,
});
const translatorsInfo = response?.translatorsInfo || [];
onTranslators(translatorsInfo, tab.id);
}
async function evalInTab(tabsId, code) {
try {
result = await browser.tabs.executeScript(tabsId, {
code: code,
});
console.log(`JabRef: code executed with result ${result}`);
return result;
} catch (error) {
console.log(`JabRef: Error executing script: ${error}`);
}
}
function openErrorPage(message, details = "", stacktrace = "") {
browser.tabs.create({
url:
"/data/error.html?message=" +
encodeURIComponent(message) +
"&details=" +
encodeURIComponent(details ?? "") +
"&stacktrace=" +
encodeURIComponent(stacktrace ?? ""),
});
}
async function getBaseUrl() {
const settings = await browser.storage.sync.get({ httpPort: 23119 });
return `http://localhost:${settings.httpPort}/`;
}
async function sendBibEntryHttp(bibtex) {
const baseUrl = await getBaseUrl();
const health = await fetch(baseUrl, { method: "GET", cache: "no-store" });
if (!(health.ok || health.status === 404)) {
throw new Error(`JabRef HTTP endpoint unavailable (${health.status})`);
}
const resp = await fetch(baseUrl + "libraries/current/entries", {
method: "POST",
headers: { "Content-Type": "application/x-bibtex" },
body: bibtex,
});
if (!resp.ok) {
const body = await resp.text().catch(() => "");
throw new Error(`HTTP ${resp.status}${body ? `: ${body}` : ""}`);
}
}
async function sendBibEntryNative(bibtex) {
const response = await browser.runtime.sendNativeMessage("org.jabref.jabref", {
text: bibtex,
});
if (response?.message === "ok") {
return;
}
if (response?.message === "error") {
console.error(
`JabRef: Error connecting to JabRef: '${response.output}' at '${response.stacktrace}'`,
);
openErrorPage(response.output, "", response.stacktrace);
}
console.error(
`JabRef: Error connecting to JabRef: '${response.message}' with details '${response.output}' at '${response.stacktrace}'`,
);
openErrorPage(response.message, response.output, response.stacktrace);
}
async function sendBibTexToJabRef(bibtex) {
await browser.runtime.sendMessage({ onSendToJabRef: "sendToJabRefStarted" });
console.log("JabRef: Send BibTeX to JabRef: %o", bibtex);
try {
await sendBibEntryHttp(bibtex);
await browser.runtime.sendMessage({ popupClose: "close" });
return;
} catch (httpError) {
console.warn("JabRef: HTTP send failed, falling back to native messaging", httpError);
}
await sendBibEntryNative(bibtex);
await browser.runtime.sendMessage({ popupClose: "close" });
}
function saveAsWebpage(tab) {
var title = tab.title;
var url = tab.url;
var date = new Date().toISOString();
// Construct a manual Bibtex Entry for the webpage
var bibtexString = `@misc{,\
title={${title}},\
url = {${url}},\
urlDate={${date}},\
}`;
sendBibTexToJabRef(bibtexString);
}
function savePdf(tab) {
var title = tab.title.replace(".pdf", "");
var url = tab.url;
var urlEscaped = tab.url.replace(":", "\\:");
var date = new Date().toISOString();
// Construct a manual Bibtex Entry for the PDF
var bibtexString = `@misc{,\
title={${title}},\
file={:${urlEscaped}:PDF},\
url = {${url}},\
urlDate={${date}},\
}`;
sendBibTexToJabRef(bibtexString);
}
/*
Is called after lookForTranslators found matching translators.
We need to hide or show the page action accordingly.
*/
function onTranslators(translatorsInfo, tabId) {
if (!translatorsInfo || translatorsInfo.length === 0) {
console.log(`JabRef: Found no suitable translators for tab ${tabId}`);
tabInfo.set(tabId, { ...tabInfo.get(tabId), translatorsInfo });
browser.pageAction.show(tabId);
browser.pageAction.setTitle({
tabId: tabId,
title: "Import simple website reference into JabRef",
});
} else {
console.log(`JabRef: Found translators %o for tab ${tabId}`, translatorsInfo);
tabInfo.set(tabId, { ...tabInfo.get(tabId), translatorsInfo });
browser.pageAction.show(tabId);
browser.pageAction.setTitle({
tabId: tabId,
title: "Import references into JabRef using " + translatorsInfo[0].label,
});
}
}
async function initOffscreenDocument() {
if (!browser.offscreen) return false;
const has = await browser.offscreen.hasDocument();
if (has) return true;
try {
await browser.offscreen.createDocument({
url: browser.runtime.getURL("offscreen.html"),
reasons: ["DOM_PARSER"],
justification: "Scraping the document for bibliographic data",
});
return true;
} catch (e) {
console.warn("Failed to create offscreen document", e);
return false;
}
}
async function initContentScript(tabId) {
return await browser.scripting.executeScript({
target: { tabId },
files: ["sources/contentScript.js"],
});
}
async function initTranslateEngine(tab) {
// The basic issue is that the background script doesn't have access
// to the DOM.
// Depending on the browser, we run the translators thus in:
// - the offscreen page (Chrome),
// - the content script (Firefox).
if (browser.offscreen) {
await initOffscreenDocument();
} else {
await initContentScript(tab.id);
}
}
async function onPopupOpened(tab, info) {
if (!info.translatorsInfo.length) throw new Error("No translator paths provided");
await browser.tabs.sendMessage(tab.id, {
type: "runTranslators",
url: tab.url,
translatorsInfo: info.translatorsInfo,
});
}
async function getConversionMode() {
const cfg = await browser.storage.sync.get({ exportMode: "bibtex" });
return cfg.exportMode || "bibtex";
}
async function prepareForExport(items) {
const { takeSnapshots } = await browser.storage.sync.get({ takeSnapshots: false });
for (var i = 0; i < items.length; i++) {
var item = items[i];
for (var j = 0; j < item.attachments.length; j++) {
var attachment = item.attachments[j];
var isLink =
attachment.mimeType === "text/html" || attachment.mimeType === "application/xhtml+xml";
if (isLink && attachment.snapshot !== false) {
// Snapshot
if (takeSnapshots && attachment.url) {
attachment.localPath = attachment.url;
} else {
// Ignore
}
} else {
// Normal file
// Pretend we downloaded the file since otherwise it is not exported
if (attachment.url) {
attachment.localPath = attachment.url;
}
}
}
// Fix date string
if (item.accessDate) {
item.accessDate = new Date().toISOString();
}
}
}
browser.runtime.onMessage.addListener(async function (message, sender, _sendResponse) {
try {
if (message.type === "popupOpened") {
// The popup opened, i.e. the user clicked on the page action button
console.log("JabRef: Popup opened confirmed");
const tabs = await browser.tabs.query({
active: true,
currentWindow: true,
});
var tab = tabs[0];
var info = tabInfo.get(tab.id);
if (info && info.isPDF) {
console.log("JabRef: Export PDF in tab %o", JSON.parse(JSON.stringify(tab)));
savePdf(tab);
} else if (!info.translatorsInfo) {
console.log("JabRef: No translators, simple saving %o", JSON.parse(JSON.stringify(tab)));
saveAsWebpage(tab);
} else {
console.log("JabRef: Start translation for tab %o", JSON.parse(JSON.stringify(tab)));
await onPopupOpened(tab, info);
}
return { ok: true };
} else if (message.type === "COHTTP.request") {
const { method, url, options } = message;
console.debug(`JabRef: COHTTP request in background.js: ${method} ${url} %o`, options);
const xhr = await Zotero.HTTP.request(method, url, options);
// From upstream: https://github.com/zotero/zotero-connectors/blob/ea060a0aa2fea1267049b5fc880e53aa6c915eeb/src/common/messages.js#L302-L316
let result = {
response: xhr.response,
responseType: xhr.responseType,
status: xhr.status,
statusText: xhr.statusText,
responseHeaders: xhr.getAllResponseHeaders(),
responseURL: xhr.responseURL,
};
return result;
} else if (message.type === "offscreenResult") {
console.debug("JabRef: offscreenResult in background.js: %o", message);
if (message.error) {
console.error("JabRef: Error in offscreen translator execution", message.error);
return;
}
const { url, items } = message;
const conversionMode = await getConversionMode();
await prepareForExport(items);
await browser.runtime.sendMessage({ onConvertToBibtex: "convertStarted" });
const bib = await exportItems(items, conversionMode);
console.debug("JabRef: Exported BibTeX: %o", bib);
await sendBibTexToJabRef(bib);
} else if (message.eval) {
console.debug("JabRef: eval in background.js: %o", JSON.parse(JSON.stringify(message.eval)));
return evalInTab(sender.tab.id, message.eval);
} else if (message[0] === "Debug.log") {
console.log(message[1]);
} else if (message[0] === "Errors.log") {
console.log(message[1]);
} else {
console.log(
"JabRef: other message in background.js: %o",
JSON.parse(JSON.stringify(message)),
);
}
} catch (e) {
console.error("JabRef: Error handling message in background.js", e);
throw e;
}
});