diff --git a/src/entrypoints/background.ts b/src/entrypoints/background.ts index 0e2713fd..05d09a27 100644 --- a/src/entrypoints/background.ts +++ b/src/entrypoints/background.ts @@ -7,6 +7,7 @@ export default defineBackground({ type: "module", main() { var tabInfo = new Map(); + const BIB_EXPORT_TIMEOUT_MS = 5000; /* Show/hide import button for all tabs (when add-on is loaded). @@ -145,6 +146,9 @@ export default defineBackground({ async function sendBibEntryHttp(bibtex) { const baseUrl = await getBaseUrl(); + await browser.runtime.sendMessage({ + popupLog: `Trying JabRef HTTP endpoint at ${baseUrl}`, + }); const health = await fetch(baseUrl, { method: "GET", cache: "no-store" }); if (!(health.ok || health.status === 404)) { @@ -161,13 +165,23 @@ export default defineBackground({ const body = await resp.text().catch(() => ""); throw new Error(`HTTP ${resp.status}${body ? `: ${body}` : ""}`); } + + await browser.runtime.sendMessage({ + popupLog: "JabRef accepted data over HTTP", + }); } async function sendBibEntryNative(bibtex) { + await browser.runtime.sendMessage({ + popupLog: "Trying native messaging to reach JabRef", + }); const response = await browser.runtime.sendNativeMessage("org.jabref.jabref", { text: bibtex, }); if (response?.message === "ok") { + await browser.runtime.sendMessage({ + popupLog: "JabRef accepted data over native messaging", + }); return; } @@ -190,13 +204,22 @@ export default defineBackground({ try { await sendBibEntryHttp(bibtex); + await browser.runtime.sendMessage({ + popupLog: "Send to JabRef finished", + }); await browser.runtime.sendMessage({ popupClose: "close" }); return; } catch (httpError) { console.warn("JabRef: HTTP send failed, falling back to native messaging", httpError); + await browser.runtime.sendMessage({ + popupLog: `HTTP send failed, falling back to native messaging: ${httpError}`, + }); } await sendBibEntryNative(bibtex); + await browser.runtime.sendMessage({ + popupLog: "Send to JabRef finished", + }); await browser.runtime.sendMessage({ popupClose: "close" }); } @@ -314,6 +337,18 @@ export default defineBackground({ return cfg.exportMode || "bibtex"; } + async function raceWithTimeout(promise, timeoutMs, label) { + return Promise.race([ + promise, + new Promise((_, reject) => { + setTimeout( + () => reject(new Error(`${label} timed out after ${timeoutMs} ms`)), + timeoutMs, + ); + }), + ]); + } + async function prepareForExport(items) { const { takeSnapshots } = await browser.storage.sync.get({ takeSnapshots: false }); @@ -395,11 +430,24 @@ export default defineBackground({ return; } const { url, items } = message; + await browser.runtime.sendMessage({ + popupLog: `Translator returned ${items.length} item(s) for ${url}`, + }); const conversionMode = await getConversionMode(); await prepareForExport(items); await browser.runtime.sendMessage({ onConvertToBibtex: "convertStarted" }); - const bib = await exportItems(items, conversionMode); + await browser.runtime.sendMessage({ + popupLog: `Starting BibTeX export in background for ${items.length} item(s)`, + }); + const bib = await raceWithTimeout( + exportItems(items, conversionMode), + BIB_EXPORT_TIMEOUT_MS, + "BibTeX export", + ); console.debug("JabRef: Exported BibTeX: %o", bib); + await browser.runtime.sendMessage({ + popupLog: `BibTeX export finished using mode ${conversionMode}`, + }); await sendBibTexToJabRef(bib); } else if (message.eval) { console.debug( @@ -419,6 +467,11 @@ export default defineBackground({ } } catch (e) { console.error("JabRef: Error handling message in background.js", e); + try { + await browser.runtime.sendMessage({ + popupLog: `Background error: ${e instanceof Error ? e.message : String(e)}`, + }); + } catch {} throw e; } }); diff --git a/src/entrypoints/content/index.js b/src/entrypoints/content/index.js index fc21230c..8252dbbe 100644 --- a/src/entrypoints/content/index.js +++ b/src/entrypoints/content/index.js @@ -6,6 +6,11 @@ export default defineContentScript({ matches: [], async main() { + if (globalThis.__JABREF_CONTENT_SCRIPT_INITIALIZED__) { + console.debug("[contentScript] already initialized"); + return; + } + globalThis.__JABREF_CONTENT_SCRIPT_INITIALIZED__ = true; console.debug("[contentScript] started"); browser.runtime.onMessage.addListener(async (msg, _sender, _sendResponse) => { @@ -76,7 +81,17 @@ export default defineContentScript({ ); const result = await translateEngine.translate(document, translators); console.debug("Content script obtained translation result %o", result); - await browser.runtime.sendMessage({ type: "offscreenResult", url, items: result.items }); + console.debug( + "Content script sending offscreenResult with %o item(s) for %o", + result.items?.length ?? 0, + url, + ); + const response = await browser.runtime.sendMessage({ + type: "offscreenResult", + url, + items: result.items, + }); + console.debug("Content script received background ack for offscreenResult %o", response); }); }, }); diff --git a/src/entrypoints/options/index.html b/src/entrypoints/options/index.html index e5c3f361..4e2c68d2 100644 --- a/src/entrypoints/options/index.html +++ b/src/entrypoints/options/index.html @@ -86,6 +86,18 @@
No test run yet.+