@@ -95,6 +95,15 @@ function sidepanelHandler(port) {
9595 // @ts -expect-error - TS2339 - Property 'port' does not exist on type 'BrowserRecorder'.
9696 self . recorders [ tabId ] . port = port ;
9797 self . recorders [ tabId ] . doUpdateStatus ( ) ;
98+ } else if ( isRecordingEnabled ) {
99+ // Send the current recording state even if no recorder exists for this tab
100+ port . postMessage ( {
101+ type : "status" ,
102+ recording : false , // No recorder for this tab
103+ autorun,
104+ // @ts -expect-error
105+ collId : defaultCollId ,
106+ } ) ;
98107 }
99108 port . postMessage ( await listAllMsg ( collLoader ) ) ;
100109 break ;
@@ -353,8 +362,18 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
353362
354363 if ( changeInfo . url && ! isValidUrl ( changeInfo . url , skipDomains ) ) {
355364 stopRecorder ( tabId ) ;
365+
366+ // Ensure debugger is detached when navigating to a skipped domain
367+ try {
368+ chrome . debugger . detach ( { tabId } , ( ) => {
369+ // Debugger detached, ignore any errors as it might already be detached
370+ } ) ;
371+ } catch ( e ) {
372+ // Ignore errors - debugger might already be detached
373+ }
374+
356375 delete self . recorders [ tabId ] ;
357- // let the side-panel know the ’ canRecord’ /UI state changed
376+ // let the side-panel know the ' canRecord' /UI state changed
358377 // @ts -expect-error
359378 if ( sidepanelPort ) {
360379 sidepanelPort . postMessage ( { type : "update" } ) ;
@@ -440,6 +459,9 @@ async function startRecorder(tabId, opts) {
440459 let err = null ;
441460 // @ts -expect-error - TS7034 - Variable 'sidepanelPort' implicitly has type 'any' in some locations where its type cannot be determined.
442461 if ( sidepanelPort ) {
462+ // Set the port on the recorder so it can send status updates
463+ // @ts -expect-error
464+ self . recorders [ tabId ] . port = sidepanelPort ;
443465 sidepanelPort . postMessage ( { type : "update" } ) ;
444466 }
445467 const { waitForTabUpdate } = opts ;
@@ -449,9 +471,28 @@ async function startRecorder(tabId, opts) {
449471 try {
450472 self . recorders [ tabId ] . setCollId ( opts . collId ) ;
451473 await self . recorders [ tabId ] . attach ( ) ;
474+
475+ // Send status update after successful attach
476+ // @ts -expect-error
477+ if ( sidepanelPort && self . recorders [ tabId ] ) {
478+ self . recorders [ tabId ] . doUpdateStatus ( ) ;
479+ }
452480 } catch ( e ) {
453481 console . warn ( e ) ;
454482 err = e ;
483+
484+ // Clean up on error
485+ // @ts -expect-error
486+ if ( err ?. message ?. includes ( "already attached" ) ) {
487+ // Try to detach and delete the recorder
488+ try {
489+ chrome . debugger . detach ( { tabId } , ( ) => {
490+ delete self . recorders [ tabId ] ;
491+ } ) ;
492+ } catch ( detachErr ) {
493+ console . warn ( "Failed to detach debugger:" , detachErr ) ;
494+ }
495+ }
455496 }
456497 return err ;
457498 }
@@ -462,6 +503,19 @@ async function startRecorder(tabId, opts) {
462503function stopRecorder ( tabId ) {
463504 if ( self . recorders [ tabId ] ) {
464505 self . recorders [ tabId ] . detach ( ) ;
506+
507+ // Ensure the sidepanel is notified about the stop
508+ // @ts -expect-error - TS7034 - Variable 'sidepanelPort' implicitly has type 'any' in some locations where its type cannot be determined.
509+ if ( sidepanelPort ) {
510+ sidepanelPort . postMessage ( {
511+ type : "status" ,
512+ recording : false ,
513+ autorun,
514+ // @ts -expect-error - defaultCollId implicitly has an 'any' type.
515+ collId : defaultCollId ,
516+ } ) ;
517+ }
518+
465519 return true ;
466520 }
467521
0 commit comments