|
1 | | -import { createTranslateEngine, exportItems } from "./sources/translateEngine.js"; |
| 1 | +import { exportItems } from "./sources/translateEngine.js"; |
2 | 2 |
|
3 | 3 | // Provide a minimal compatibility shim: if `browser` is missing, alias it to `chrome`. |
4 | 4 | if (typeof browser === "undefined" && typeof chrome !== "undefined") { |
@@ -103,9 +103,14 @@ function installInTab(tab) { |
103 | 103 | */ |
104 | 104 | async function lookForTranslators(tab) { |
105 | 105 | 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); |
109 | 114 | } |
110 | 115 |
|
111 | 116 | async function evalInTab(tabsId, code) { |
@@ -228,22 +233,22 @@ function savePdf(tab) { |
228 | 233 | Is called after lookForTranslators found matching translators. |
229 | 234 | We need to hide or show the page action accordingly. |
230 | 235 | */ |
231 | | -function onTranslators(translators, tabId) { |
232 | | - if (!translators || translators.length === 0) { |
| 236 | +function onTranslators(translatorsInfo, tabId) { |
| 237 | + if (!translatorsInfo || translatorsInfo.length === 0) { |
233 | 238 | 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 }); |
235 | 240 | browser.pageAction.show(tabId); |
236 | 241 | browser.pageAction.setTitle({ |
237 | 242 | tabId: tabId, |
238 | 243 | title: "Import simple website reference into JabRef", |
239 | 244 | }); |
240 | 245 | } 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 }); |
243 | 248 | browser.pageAction.show(tabId); |
244 | 249 | browser.pageAction.setTitle({ |
245 | 250 | tabId: tabId, |
246 | | - title: "Import references into JabRef using " + translators[0].label, |
| 251 | + title: "Import references into JabRef using " + translatorsInfo[0].label, |
247 | 252 | }); |
248 | 253 | } |
249 | 254 | } |
@@ -272,34 +277,26 @@ async function initContentScript(tabId) { |
272 | 277 | }); |
273 | 278 | } |
274 | 279 |
|
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). |
281 | 286 | if (browser.offscreen) { |
282 | 287 | await initOffscreenDocument(); |
283 | 288 | } else { |
284 | 289 | await initContentScript(tab.id); |
285 | 290 | } |
| 291 | +} |
| 292 | + |
| 293 | +async function onPopupOpened(tab, info) { |
| 294 | + if (!info.translatorsInfo.length) throw new Error("No translator paths provided"); |
| 295 | + |
286 | 296 | await browser.tabs.sendMessage(tab.id, { |
287 | 297 | type: "runTranslators", |
288 | 298 | 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, |
303 | 300 | }); |
304 | 301 | } |
305 | 302 |
|
@@ -356,7 +353,7 @@ browser.runtime.onMessage.addListener(async function (message, sender, _sendResp |
356 | 353 | if (info && info.isPDF) { |
357 | 354 | console.log("JabRef: Export PDF in tab %o", JSON.parse(JSON.stringify(tab))); |
358 | 355 | savePdf(tab); |
359 | | - } else if (!info.translators) { |
| 356 | + } else if (!info.translatorsInfo) { |
360 | 357 | console.log("JabRef: No translators, simple saving %o", JSON.parse(JSON.stringify(tab))); |
361 | 358 | saveAsWebpage(tab); |
362 | 359 | } else { |
|
0 commit comments