1-
21chrome . runtime . onMessage . addListener ( ( request , _ , sendResponse ) => {
3- if ( request . type === "TRANSLATE_CODE" ) {
4- const BACKEND_URL = process . env . BACKEND_URL ;
5-
6- chrome . storage . sync . get ( [ 'targetLanguage' ] , ( result ) => {
7- const targetLanguage = result . targetLanguage || 'Java' ;
8- fetch ( BACKEND_URL , {
9- method : 'POST' ,
10- headers : {
11- 'Content-Type' : 'application/json' ,
12- } ,
13- body : JSON . stringify ( {
14- code : request . code ,
15- targetLanguage : targetLanguage ,
16- } ) ,
17- } )
18- . then ( response => {
19- if ( ! response . ok ) {
20- throw new Error ( `Network response was not ok: ${ response . statusText } ` ) ;
21- }
22- return response . json ( ) ;
23- } )
24- . then ( data => {
25-
26- if ( data . error ) {
27- sendResponse ( { error : data . error } ) ;
28- } else {
29- sendResponse ( { translation : data . translation } ) ;
30- }
31- } )
32- . catch ( error => {
2+ if ( request . type === "TRANSLATE_CODE" ) {
3+ const BACKEND_URL = process . env . BACKEND_URL ;
334
34- console . error ( "Error calling backend:" , error ) ;
35- sendResponse ( { error : `Failed to connect to the translation service: ${ error . message } ` } ) ;
36- } ) ;
5+ chrome . storage . sync . get ( [ "targetLanguage" ] , ( result ) => {
6+ const targetLanguage = result . targetLanguage || "Java" ;
7+ fetch ( BACKEND_URL , {
8+ method : "POST" ,
9+ headers : {
10+ "Content-Type" : "application/json" ,
11+ } ,
12+ body : JSON . stringify ( {
13+ code : request . code ,
14+ targetLanguage : targetLanguage ,
15+ } ) ,
16+ } )
17+ . then ( ( response ) => {
18+ if ( ! response . ok ) {
19+ throw new Error (
20+ `Network response was not ok: ${ response . statusText } `
21+ ) ;
22+ }
23+ return response . json ( ) ;
24+ } )
25+ . then ( ( data ) => {
26+ if ( data . error ) {
27+ sendResponse ( { error : data . error } ) ;
28+ } else {
29+ sendResponse ( { translation : data . translation } ) ;
30+ }
31+ } )
32+ . catch ( ( error ) => {
33+ console . error ( "Error calling backend:" , error ) ;
34+ sendResponse ( {
35+ error : `Failed to connect to the translation service: ${ error . message } ` ,
36+ } ) ;
3737 } ) ;
38+ } ) ;
3839
39- return true ;
40- }
41- } ) ;
40+ return true ;
41+ }
42+ } ) ;
0 commit comments