@@ -120,25 +120,99 @@ async function evalInTab(tabsId, code) {
120120 }
121121}
122122
123+ function openErrorPage ( message , details = "" , stacktrace = "" ) {
124+ browser . tabs . create ( {
125+ url :
126+ "/data/error.html?message=" +
127+ encodeURIComponent ( message ) +
128+ "&details=" +
129+ encodeURIComponent ( details ?? "" ) +
130+ "&stacktrace=" +
131+ encodeURIComponent ( stacktrace ?? "" ) ,
132+ } ) ;
133+ }
134+
135+ async function getBaseUrl ( ) {
136+ const settings = await browser . storage . sync . get ( { httpPort : 23119 } ) ;
137+ return `http://localhost:${ settings . httpPort } /` ;
138+ }
139+
140+ async function sendBibEntryHttp ( bibtex ) {
141+ const baseUrl = await getBaseUrl ( ) ;
142+
143+ const health = await fetch ( baseUrl , { method : "GET" , cache : "no-store" } ) ;
144+ if ( ! ( health . ok || health . status === 404 ) ) {
145+ throw new Error ( `JabRef HTTP endpoint unavailable (${ health . status } )` ) ;
146+ }
147+
148+ const resp = await fetch ( baseUrl + "libraries/current/entries" , {
149+ method : "POST" ,
150+ headers : { "Content-Type" : "application/x-bibtex" } ,
151+ body : bibtex ,
152+ } ) ;
153+
154+ if ( ! resp . ok ) {
155+ const body = await resp . text ( ) . catch ( ( ) => "" ) ;
156+ throw new Error ( `HTTP ${ resp . status } ${ body ? `: ${ body } ` : "" } ` ) ;
157+ }
158+ }
159+
160+ async function sendBibEntryNative ( bibtex ) {
161+ const response = await browser . runtime . sendNativeMessage ( "org.jabref.jabref" , {
162+ text : bibtex ,
163+ } ) ;
164+ if ( response ?. message === "ok" ) {
165+ return ;
166+ }
167+
168+ if ( response ?. message === "error" ) {
169+ console . error (
170+ `JabRef: Error connecting to JabRef: '${ response . output } ' at '${ response . stacktrace } '` ,
171+ ) ;
172+ handleError ( response . output , "" , response . stacktrace ) ;
173+ }
174+
175+ console . error (
176+ `JabRef: Error connecting to JabRef: '${ response . message } ' with details '${ response . output } ' at '${ response . stacktrace } '` ,
177+ ) ;
178+ handleError ( response . message , response . output , response . stacktrace ) ;
179+ }
180+
181+ async function sendBibTexToJabRef ( bibtex ) {
182+ await browser . runtime . sendMessage ( { onSendToJabRef : "sendToJabRefStarted" } ) ;
183+ console . log ( "JabRef: Send BibTeX to JabRef: %o" , bibtex ) ;
184+
185+ try {
186+ await sendBibEntryHttp ( bibtex ) ;
187+ await browser . runtime . sendMessage ( { popupClose : "close" } ) ;
188+ return ;
189+ } catch ( httpError ) {
190+ console . warn ( "JabRef: HTTP send failed, falling back to native messaging" , httpError ) ;
191+ }
192+
193+ await sendBibEntryNative ( bibtex ) ;
194+ await browser . runtime . sendMessage ( { popupClose : "close" } ) ;
195+ }
196+
123197function saveAsWebpage ( tab ) {
124198 var title = tab . title ;
125199 var url = tab . url ;
126- var date = new Date ( ) . toISODate ( ) ;
200+ var date = new Date ( ) . toISOString ( ) ;
127201
128202 // Construct a manual Bibtex Entry for the webpage
129203 var bibtexString = `@misc{,\
130204 title={${ title } },\
131205 url = {${ url } },\
132206 urlDate={${ date } },\
133207 }` ;
134- Zotero . Connector . sendBibTexToJabRef ( bibtexString ) ;
208+ sendBibTexToJabRef ( bibtexString ) ;
135209}
136210
137211function savePdf ( tab ) {
138212 var title = tab . title . replace ( ".pdf" , "" ) ;
139213 var url = tab . url ;
140214 var urlEscaped = tab . url . replace ( ":" , "\\:" ) ;
141- var date = new Date ( ) . toISODate ( ) ;
215+ var date = new Date ( ) . toISOString ( ) ;
142216
143217 // Construct a manual Bibtex Entry for the PDF
144218 var bibtexString = `@misc{,\
@@ -147,7 +221,7 @@ function savePdf(tab) {
147221 url = {${ url } },\
148222 urlDate={${ date } },\
149223 }` ;
150- Zotero . Connector . sendBibTexToJabRef ( bibtexString ) ;
224+ sendBibTexToJabRef ( bibtexString ) ;
151225}
152226
153227/*
@@ -308,7 +382,7 @@ browser.runtime.onMessage.addListener(async function (message, sender, _sendResp
308382 } else if ( message . type === "offscreenResult" ) {
309383 console . debug ( "JabRef: offscreenResult in background.js: %o" , message ) ;
310384 if ( message . error ) {
311- await browser . runtime . sendMessage ( { type : "offscreenResult" , url , error : message . error } ) ;
385+ console . error ( "JabRef: Error in offscreen translator execution" , message . error ) ;
312386 return ;
313387 }
314388 const { url, items } = message ;
@@ -317,6 +391,7 @@ browser.runtime.onMessage.addListener(async function (message, sender, _sendResp
317391 await browser . runtime . sendMessage ( { onConvertToBibtex : "convertStarted" } ) ;
318392 const bib = await exportItems ( items , conversionMode ) ;
319393 console . debug ( "JabRef: Exported BibTeX: %o" , bib ) ;
394+ await sendBibTexToJabRef ( bib ) ;
320395 } else if ( message . eval ) {
321396 console . debug ( "JabRef: eval in background.js: %o" , JSON . parse ( JSON . stringify ( message . eval ) ) ) ;
322397 return evalInTab ( sender . tab . id , message . eval ) ;
0 commit comments