Skip to content

Commit 04113eb

Browse files
committed
fix: run detect step also in the content script
1 parent 3256f64 commit 04113eb

4 files changed

Lines changed: 54 additions & 44 deletions

File tree

background.js

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createTranslateEngine, exportItems } from "./sources/translateEngine.js";
1+
import { exportItems } from "./sources/translateEngine.js";
22

33
// Provide a minimal compatibility shim: if `browser` is missing, alias it to `chrome`.
44
if (typeof browser === "undefined" && typeof chrome !== "undefined") {
@@ -103,9 +103,14 @@ function installInTab(tab) {
103103
*/
104104
async function lookForTranslators(tab) {
105105
console.log("JabRef: Searching for translators for %o", tab);
106-
const engine = await createTranslateEngine(tab.url);
107-
const translators = await engine.detect();
108-
onTranslators(translators, tab.id);
106+
107+
await initTranslateEngine(tab)
108+
const response = await browser.tabs.sendMessage(tab.id, {
109+
type: "detectTranslators",
110+
url: tab.url,
111+
});
112+
const translatorsInfo = response?.translatorsInfo || [];
113+
onTranslators(translatorsInfo, tab.id);
109114
}
110115

111116
async function evalInTab(tabsId, code) {
@@ -228,22 +233,22 @@ function savePdf(tab) {
228233
Is called after lookForTranslators found matching translators.
229234
We need to hide or show the page action accordingly.
230235
*/
231-
function onTranslators(translators, tabId) {
232-
if (!translators || translators.length === 0) {
236+
function onTranslators(translatorsInfo, tabId) {
237+
if (!translatorsInfo || translatorsInfo.length === 0) {
233238
console.log(`JabRef: Found no suitable translators for tab ${tabId}`);
234-
tabInfo.set(tabId, { ...tabInfo.get(tabId), translators });
239+
tabInfo.set(tabId, { ...tabInfo.get(tabId), translatorsInfo });
235240
browser.pageAction.show(tabId);
236241
browser.pageAction.setTitle({
237242
tabId: tabId,
238243
title: "Import simple website reference into JabRef",
239244
});
240245
} else {
241-
console.log(`JabRef: Found translators %o for tab ${tabId}`, translators);
242-
tabInfo.set(tabId, { ...tabInfo.get(tabId), translators });
246+
console.log(`JabRef: Found translators %o for tab ${tabId}`, translatorsInfo);
247+
tabInfo.set(tabId, { ...tabInfo.get(tabId), translatorsInfo });
243248
browser.pageAction.show(tabId);
244249
browser.pageAction.setTitle({
245250
tabId: tabId,
246-
title: "Import references into JabRef using " + translators[0].label,
251+
title: "Import references into JabRef using " + translatorsInfo[0].label,
247252
});
248253
}
249254
}
@@ -272,34 +277,26 @@ async function initContentScript(tabId) {
272277
});
273278
}
274279

275-
async function onPopupOpened(tab, info) {
276-
if (!info.translators.length) throw new Error("No translator paths provided");
277-
278-
// If offscreen is available (Chrome), forward the request so the offscreen
279-
// document runs the translator. If not (Firefox), run the translator
280-
// from the content script (which has a DOM available, unlike the background page).
280+
async function initTranslateEngine(tab) {
281+
// The basic issue is that the background script doesn't have access
282+
// to the DOM.
283+
// Depending on the browser, we run the translators thus in:
284+
// - the offscreen page (Chrome),
285+
// - the content script (Firefox).
281286
if (browser.offscreen) {
282287
await initOffscreenDocument();
283288
} else {
284289
await initContentScript(tab.id);
285290
}
291+
}
292+
293+
async function onPopupOpened(tab, info) {
294+
if (!info.translatorsInfo.length) throw new Error("No translator paths provided");
295+
286296
await browser.tabs.sendMessage(tab.id, {
287297
type: "runTranslators",
288298
url: tab.url,
289-
translatorsInfo: info.translators.map((translator) => {
290-
// We cannot send the full translator object as it contains functions
291-
return {
292-
translatorID: translator.translatorID,
293-
translatorType: translator.translatorType,
294-
label: translator.label,
295-
creator: translator.creator,
296-
target: translator.target,
297-
priority: translator.priority,
298-
path: translator.path,
299-
file: translator.file,
300-
lastUpdated: translator.lastUpdated,
301-
};
302-
}),
299+
translatorsInfo: info.translatorsInfo,
303300
});
304301
}
305302

@@ -356,7 +353,7 @@ browser.runtime.onMessage.addListener(async function (message, sender, _sendResp
356353
if (info && info.isPDF) {
357354
console.log("JabRef: Export PDF in tab %o", JSON.parse(JSON.stringify(tab)));
358355
savePdf(tab);
359-
} else if (!info.translators) {
356+
} else if (!info.translatorsInfo) {
360357
console.log("JabRef: No translators, simple saving %o", JSON.parse(JSON.stringify(tab)));
361358
saveAsWebpage(tab);
362359
} else {

manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@
100100
"sources/http.js",
101101
"sources/webRequestIntercept.js",
102102
"sources/translateWeb.js",
103-
"translators/zotero/*.js"
103+
"translators/zotero/*.js",
104+
"translators/manifest.json"
104105
]
105106
}
106107
],

sources/contentScript.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
console.debug("[contentScript] started");
22

3-
browser.runtime.onMessage.addListener(async (msg, sender, sendResponse) => {
3+
browser.runtime.onMessage.addListener(async (msg, _sender, _sendResponse) => {
44
console.debug("[contentScript] received message: %o", msg);
55

66
await import(browser.runtime.getURL("./sources/setupZotero.js"));
@@ -35,15 +35,32 @@ browser.runtime.onMessage.addListener(async (msg, sender, sendResponse) => {
3535
};
3636
Zotero.Translate.ItemSaver.prototype.saveItems = async function (
3737
jsonItems,
38-
attachmentCallback,
39-
itemsDoneCallback,
38+
_attachmentCallback,
39+
_itemsDoneCallback,
4040
) {
4141
return jsonItems;
4242
};
4343

44-
if (!msg || msg.type !== "runTranslators") return;
45-
const { url, translatorsInfo } = msg;
46-
const translators = translatorsInfo.map((info) => {
44+
if (!msg) return;
45+
const { url } = msg;
46+
47+
// Dynamic import as workaround for Chrome's content script limitations (no module support)
48+
// see https://stackoverflow.com/a/53033388/873661
49+
const { createTranslateEngine } = await import(
50+
browser.runtime.getURL("./sources/translateEngine.js")
51+
);
52+
const translateEngine = await createTranslateEngine(url);
53+
54+
if (msg.type === "detectTranslators") {
55+
const translatorsInfo = await translateEngine.detect();
56+
return {
57+
translatorsInfo,
58+
};
59+
}
60+
61+
if (msg.type !== "runTranslators") return;
62+
63+
const translators = msg.translatorsInfo.map((info) => {
4764
const translator = new Zotero.Translator(info);
4865
// Zotero expects the path to be under `file`
4966
translator.file = {
@@ -56,12 +73,6 @@ browser.runtime.onMessage.addListener(async (msg, sender, sendResponse) => {
5673
url,
5774
translators,
5875
);
59-
// Dynamic import as workaround for Chrome's content script limitations (no module support)
60-
// see https://stackoverflow.com/a/53033388/873661
61-
const { createTranslateEngine } = await import(
62-
browser.runtime.getURL("./sources/translateEngine.js")
63-
);
64-
const translateEngine = await createTranslateEngine(url);
6576
const result = await translateEngine.translate(document, translators);
6677
console.debug("Content script obtained translation result %o", result);
6778
await browser.runtime.sendMessage({ type: "offscreenResult", url, items: result.items });

sources/sandboxManager.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class SandboxManager {
7272

7373
let cached = this._moduleCache.get(url);
7474
if (!cached) {
75+
setSandbox(this.sandbox);
7576
const promise = import(url).then((mod) => {
7677
cached.module = mod;
7778
cached.loaded = true;

0 commit comments

Comments
 (0)