@@ -53,8 +53,8 @@ import { isUsingModelName } from '../utils/model-name-convert.mjs'
5353
5454const RECONNECT_CONFIG = {
5555 MAX_ATTEMPTS : 5 ,
56- BASE_DELAY_MS : 1000 , // Base delay in milliseconds
57- BACKOFF_MULTIPLIER : 2 , // Multiplier for exponential backoff
56+ BASE_DELAY_MS : 1000 ,
57+ BACKOFF_MULTIPLIER : 2 ,
5858} ;
5959
6060const SENSITIVE_KEYWORDS = [
@@ -66,6 +66,11 @@ const SENSITIVE_KEYWORDS = [
6666 'auth' ,
6767 'key' ,
6868 'credential' ,
69+ 'jwt' ,
70+ 'session' ,
71+ 'access' ,
72+ 'private' ,
73+ 'oauth' ,
6974] ;
7075
7176function redactSensitiveFields ( obj , recursionDepth = 0 , maxDepth = 5 , seen = new WeakSet ( ) ) {
@@ -94,7 +99,7 @@ function redactSensitiveFields(obj, recursionDepth = 0, maxDepth = 5, seen = new
9499 }
95100 if ( isSensitive ) {
96101 redactedObj [ key ] = 'REDACTED' ;
97- } else if ( typeof obj [ key ] === 'object' ) {
102+ } else if ( obj [ key ] !== null && typeof obj [ key ] === 'object' ) { // Added obj[key] !== null check
98103 redactedObj [ key ] = redactSensitiveFields ( obj [ key ] , recursionDepth + 1 , maxDepth , seen ) ;
99104 } else {
100105 redactedObj [ key ] = obj [ key ] ;
@@ -130,7 +135,16 @@ function setPortProxy(port, proxyTabId) {
130135 port . _portOnMessage = ( msg ) => {
131136 console . debug ( '[background] Message to proxy tab:' , msg )
132137 if ( port . proxy ) {
133- port . proxy . postMessage ( msg )
138+ try {
139+ port . proxy . postMessage ( msg )
140+ } catch ( e ) {
141+ console . error ( '[background] Error posting message to proxy tab in _portOnMessage:' , e , msg ) ;
142+ try { // Attempt to notify the original sender about the failure
143+ port . postMessage ( { error : 'Failed to forward message to target tab. Tab might be closed or an extension error occurred.' } ) ;
144+ } catch ( notifyError ) {
145+ console . error ( '[background] Error sending forwarding failure notification back to original sender:' , notifyError ) ;
146+ }
147+ }
134148 } else {
135149 console . warn ( '[background] Port proxy not available to send message:' , msg )
136150 }
@@ -160,6 +174,15 @@ function setPortProxy(port, proxyTabId) {
160174 try { port . onMessage . removeListener ( port . _portOnMessage ) ; }
161175 catch ( e ) { console . warn ( "[background] Error removing _portOnMessage on max retries:" , e ) ; }
162176 }
177+ if ( port . _portOnDisconnect ) { // Cleanup _portOnDisconnect as well
178+ try { port . onDisconnect . removeListener ( port . _portOnDisconnect ) ; }
179+ catch ( e ) { console . warn ( "[background] Error removing _portOnDisconnect on max retries:" , e ) ; }
180+ }
181+ try { // Notify user about final connection failure
182+ port . postMessage ( { error : `Connection to ChatGPT tab lost after ${ RECONNECT_CONFIG . MAX_ATTEMPTS } attempts. Please refresh the page.` } ) ;
183+ } catch ( e ) {
184+ console . warn ( "[background] Error sending final error message on max retries:" , e ) ;
185+ }
163186 return ;
164187 }
165188
@@ -225,6 +248,7 @@ async function executeApi(session, port, config) {
225248 try {
226249 if ( isUsingCustomModel ( session ) ) {
227250 console . debug ( '[background] Using Custom Model API' )
251+ // ... (rest of the logic for custom model remains the same)
228252 if ( ! session . apiMode )
229253 await generateAnswersWithCustomApi (
230254 port ,
@@ -270,7 +294,16 @@ async function executeApi(session, port, config) {
270294 }
271295 if ( port . proxy ) {
272296 console . debug ( '[background] Posting message to proxy tab:' , { session } )
273- port . proxy . postMessage ( { session } )
297+ try {
298+ port . proxy . postMessage ( { session } )
299+ } catch ( e ) {
300+ console . error ( '[background] Error posting message to proxy tab in executeApi (ChatGPT Web Model):' , e , { session } ) ;
301+ try {
302+ port . postMessage ( { error : 'Failed to communicate with ChatGPT tab. Try refreshing the page.' } ) ;
303+ } catch ( notifyError ) {
304+ console . error ( '[background] Error sending communication failure notification back:' , notifyError ) ;
305+ }
306+ }
274307 } else {
275308 console . error (
276309 '[background] Failed to send message: port.proxy is still not available after setPortProxy.' ,
@@ -281,7 +314,7 @@ async function executeApi(session, port, config) {
281314 const accessToken = await getChatGptAccessToken ( )
282315 await generateAnswersWithChatgptWebApi ( port , session . question , session , accessToken )
283316 }
284- } else if ( isUsingClaudeWebModel ( session ) ) {
317+ } else if ( isUsingClaudeWebModel ( session ) ) { // ... other models
285318 console . debug ( '[background] Using Claude Web Model' )
286319 const sessionKey = await getClaudeSessionKey ( )
287320 await generateAnswersWithClaudeWebApi ( port , session . question , session , sessionKey )
@@ -433,12 +466,16 @@ Browser.runtime.onMessage.addListener(async (message, sender) => {
433466 try {
434467 const response = await fetch ( message . data . input , message . data . init )
435468 const text = await response . text ( )
469+ if ( ! response . ok ) { // Added check for HTTP error statuses
470+ console . warn ( `[background] FETCH received error status: ${ response . status } for ${ message . data . input } ` ) ;
471+ }
436472 console . debug (
437473 `[background] FETCH successful for ${ message . data . input } , status: ${ response . status } ` ,
438474 )
439475 return [
440476 {
441477 body : text ,
478+ ok : response . ok , // Added ok status
442479 status : response . status ,
443480 statusText : response . statusText ,
444481 headers : Object . fromEntries ( response . headers ) ,
@@ -562,11 +599,11 @@ try {
562599 urls : [ 'wss://sydney.bing.com/*' , 'https://www.bing.com/*' ] ,
563600 types : [ 'xmlhttprequest' , 'websocket' ] ,
564601 } ,
565- [ 'blocking ' , 'requestHeaders' ] ,
602+ [ 'requestHeaders ' , ... ( Browser . runtime . getManifest ( ) . manifest_version < 3 ? [ 'blocking' ] : [ ] ) ] ,
566603 )
567604
568605 Browser . tabs . onUpdated . addListener ( async ( tabId , info , tab ) => {
569- const outerTryCatchError = ( error ) => { // Renamed to avoid conflict with inner error
606+ const outerTryCatchError = ( error ) => {
570607 console . error ( '[background] Error in tabs.onUpdated listener callback (outer):' , error , tabId , info ) ;
571608 } ;
572609 try {
@@ -593,7 +630,6 @@ try {
593630 }
594631 } catch ( browserError ) {
595632 console . warn ( '[background] Browser.sidePanel.setOptions failed:' , browserError . message ) ;
596- // Fallback will be attempted below if sidePanelSet is still false
597633 }
598634
599635 if ( ! sidePanelSet ) {
@@ -618,7 +654,7 @@ try {
618654 if ( ! sidePanelSet ) {
619655 console . warn ( '[background] SidePanel API (Browser.sidePanel or chrome.sidePanel) not available or setOptions failed in this browser. Side panel options not set for tab:' , tabId ) ;
620656 }
621- } catch ( error ) { // This is the outer try-catch from the original code
657+ } catch ( error ) {
622658 outerTryCatchError ( error ) ;
623659 }
624660 } ) ;
0 commit comments