1+ // Provide a minimal compatibility shim: if `browser` is missing, alias it to `chrome`.
2+ if ( typeof browser === "undefined" && typeof chrome !== "undefined" ) {
3+ globalThis . browser = chrome ;
4+ }
5+
16async function findMatchesForUrl ( url ) {
2- const manifestUrl = chrome . runtime . getURL ( "translators/manifest.json" ) ;
7+ const manifestUrl = browser . runtime . getURL ( "translators/manifest.json" ) ;
38 const resp = await fetch ( manifestUrl ) ;
49 const list = await resp . json ( ) ;
510
@@ -19,8 +24,6 @@ async function findMatchesForUrl(url) {
1924}
2025
2126function renderResults ( url , matches ) {
22- const log = document . getElementById ( "log" ) ;
23- const bib = document . getElementById ( "bibEntry" ) ;
2427 // Log the URL
2528 appendLog ( `URL: ${ url } ` ) ;
2629 if ( ! matches || ! matches . length ) {
@@ -30,12 +33,12 @@ function renderResults(url, matches) {
3033}
3134
3235async function ensureOffscreen ( ) {
33- if ( ! chrome . offscreen ) return false ;
34- const has = await chrome . offscreen . hasDocument ( ) ;
36+ if ( ! browser . offscreen ) return false ;
37+ const has = await browser . offscreen . hasDocument ( ) ;
3538 if ( has ) return true ;
3639 try {
37- await chrome . offscreen . createDocument ( {
38- url : chrome . runtime . getURL ( "offscreen.html" ) ,
40+ await browser . offscreen . createDocument ( {
41+ url : browser . runtime . getURL ( "offscreen.html" ) ,
3942 reasons : [ "DOM_PARSER" ] ,
4043 justification : "Run translators offscreen" ,
4144 } ) ;
@@ -47,33 +50,20 @@ async function ensureOffscreen() {
4750}
4851
4952async function runTranslatorOffscreen ( translatorPaths , url ) {
50- const ok = await ensureOffscreen ( ) ;
53+ await ensureOffscreen ( ) ;
5154 const payload = { type : "runTranslator" , translators : translatorPaths , url } ;
5255 try {
5356 appendLog ( `Requesting translator run for ${ url } ` , "info" ) ;
54- chrome . runtime . sendMessage ( payload , ( resp ) => {
55- if ( chrome . runtime . lastError ) {
56- appendLog (
57- `Background sendMessage failed: ${ chrome . runtime . lastError . message } ` ,
58- "error" ,
59- ) ;
60- return ;
61- }
62- if ( resp && resp . ok )
63- appendLog ( "Background acknowledged run request" , "info" ) ;
64- else
65- appendLog (
66- `Background error: ${ resp && resp . error ? resp . error : "unknown" } ` ,
67- "error" ,
68- ) ;
69- } ) ;
57+ const resp = await browser . runtime . sendMessage ( payload ) ;
58+ if ( resp && resp . ok ) appendLog ( "Background acknowledged run request" , "info" ) ;
59+ else appendLog ( `Background error: ${ resp && resp . error ? resp . error : "unknown" } ` , "error" ) ;
7060 } catch ( e ) {
7161 console . error ( "Failed to send runTranslator message" , e ) ;
7262 }
7363}
7464
7565// Listen for offscreen results
76- chrome . runtime . onMessage . addListener ( ( msg ) => {
66+ browser . runtime . onMessage . addListener ( ( msg ) => {
7767 if ( ! msg || msg . type !== "offscreenResult" ) return ;
7868 const bib = document . getElementById ( "bibEntry" ) ;
7969 const error = msg . error ;
@@ -129,14 +119,10 @@ function updateStatus(status, className) {
129119
130120// Connect to JabRef via HTTP POST
131121let jabrefBaseUrl = null ;
132- function getBaseUrl ( ) {
133- return new Promise ( ( resolve ) => {
134- chrome . storage . local . get ( { jabrefPort : 23119 } , ( res ) => {
135- const port = res . jabrefPort || 23119 ;
136- const base = `http://localhost:${ port } /` ;
137- resolve ( base ) ;
138- } ) ;
139- } ) ;
122+ async function getBaseUrl ( ) {
123+ const res = await browser . storage . local . get ( { jabrefPort : 23119 } ) ;
124+ const port = res . jabrefPort || 23119 ;
125+ return `http://localhost:${ port } /` ;
140126}
141127
142128async function connectToJabRef ( ) {
@@ -240,18 +226,15 @@ document.addEventListener("DOMContentLoaded", async () => {
240226 if ( noneEl ) noneEl . style . display = "block" ;
241227 return ;
242228 }
243- try {
244- if ( ! window . chrome || ! chrome . tabs ) {
245- urlEl . textContent = "Chrome extension APIs not available." ;
246- document . getElementById ( "none" ) . style . display = "block" ;
247- return ;
248- }
229+ try {
230+ if ( ! window . browser || ! browser . tabs ) {
231+ urlEl . textContent = "Extension APIs not available." ;
232+ document . getElementById ( "none" ) . style . display = "block" ;
233+ return ;
234+ }
249235
250- const tab = await new Promise ( ( resolve ) => {
251- chrome . tabs . query ( { active : true , currentWindow : true } , ( tabs ) => {
252- resolve ( tabs && tabs [ 0 ] ) ;
253- } ) ;
254- } ) ;
236+ const tabs = await browser . tabs . query ( { active : true , currentWindow : true } ) ;
237+ const tab = tabs && tabs [ 0 ] ;
255238 const url = tab && tab . url ? tab . url : "" ;
256239 if ( ! url ) {
257240 urlEl . textContent = "Unable to determine active tab URL." ;
@@ -274,9 +257,7 @@ document.addEventListener("DOMContentLoaded", async () => {
274257 renderResults ( url , matches || [ ] ) ;
275258 if ( matches && matches . length ) {
276259 // Build array of translator URLs and request background/offscreen
277- const translatorPaths = matches . map ( ( m ) =>
278- chrome . runtime . getURL ( m . path || "" ) ,
279- ) ;
260+ const translatorPaths = matches . map ( ( m ) => browser . runtime . getURL ( m . path || "" ) ) ;
280261 runTranslatorOffscreen ( translatorPaths , url ) ;
281262 }
282263 } catch ( e ) {
0 commit comments