@@ -18,6 +18,7 @@ let newRecCollId = null;
1818// @ts -expect-error - TS7034 - Variable 'defaultCollId' implicitly has type 'any' in some locations where its type cannot be determined.
1919let defaultCollId = null ;
2020let autorun = false ;
21+ let isRecordingEnabled = false ;
2122
2223const openWinMap = new Map ( ) ;
2324
@@ -159,16 +160,49 @@ function sidepanelHandler(port) {
159160 }
160161
161162 case "startRecording" : {
162- const { collId, autorun } = message ;
163- // @ts -expect-error - TS2554 - Expected 2 arguments, but got 3.
164- startRecorder ( tabId , { collId, port, autorun } , message . url ) ;
163+ isRecordingEnabled = true ;
164+ defaultCollId = message . collId ;
165+ autorun = message . autorun ;
166+
167+ // @ts -expect-error - tabs doesn't have type definitions
168+ chrome . tabs . query ( { active : true , currentWindow : true } , async ( tabs ) => {
169+ for ( const tab of tabs ) {
170+ if ( ! isValidUrl ( tab . url ) ) continue ;
171+
172+ // @ts -expect-error - TS2554 - Expected 2 arguments, but got 3.
173+ await startRecorder ( tab . id , { collId : defaultCollId , port : null , autorun } , tab . url ) ;
174+ }
175+
176+ port . postMessage ( {
177+ type : "status" ,
178+ recording : true ,
179+ autorun,
180+ // @ts -expect-error - defaultCollId implicitly has an 'any' type.
181+ collId : defaultCollId ,
182+ } ) ;
183+ } ) ;
184+
165185 break ;
166186 }
167187
168- case "stopRecording" :
169- // @ts -expect-error - TS7005 - Variable 'tabId' implicitly has an 'any' type.
170- stopRecorder ( tabId ) ;
188+ case "stopRecording" : {
189+ isRecordingEnabled = false ;
190+
191+ for ( const [ tabIdStr , rec ] of Object . entries ( self . recorders ) ) {
192+ const tabId = parseInt ( tabIdStr ) ;
193+ stopRecorder ( tabId ) ;
194+ }
195+
196+ port . postMessage ( {
197+ type : "status" ,
198+ recording : false ,
199+ autorun,
200+ // @ts -expect-error - defaultCollId implicitly has an 'any' type.
201+ collId : defaultCollId ,
202+ } ) ;
203+
171204 break ;
205+ }
172206
173207 case "toggleBehaviors" :
174208 // @ts -expect-error - TS7005 - Variable 'tabId' implicitly has an 'any' type.
@@ -203,6 +237,21 @@ chrome.debugger.onDetach.addListener((tab, reason) => {
203237 }
204238} ) ;
205239
240+ // @ts -expect-error - TS7006 - Parameter 'tab' implicitly has an 'any' type.
241+ chrome . tabs . onActivated . addListener ( async ( { tabId } ) => {
242+ if ( ! isRecordingEnabled ) return ;
243+
244+ // @ts -expect-error - chrome doesn't have type definitions
245+ const tab = await new Promise < chrome . tabs . Tab > ( ( resolve ) => chrome . tabs . get ( tabId , resolve ) ) ;
246+
247+ if ( ! isValidUrl ( tab . url ) ) return ;
248+
249+ if ( ! self . recorders [ tabId ] ) {
250+ // @ts -expect-error - TS2554 - Expected 2 arguments, but got 3.
251+ startRecorder ( tabId , { collId : defaultCollId , port : null , autorun } , tab . url ) ;
252+ }
253+ } ) ;
254+
206255// ===========================================================================
207256// @ts -expect-error - TS7006 - Parameter 'tab' implicitly has an 'any' type.
208257chrome . tabs . onCreated . addListener ( ( tab ) => {
@@ -278,14 +327,20 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
278327 return ;
279328 }
280329 }
281- } else if ( changeInfo . url && openWinMap . has ( changeInfo . url ) ) {
282- const collId = openWinMap . get ( changeInfo . url ) ;
283- openWinMap . delete ( changeInfo . url ) ;
284- if ( ! tabId || ! isValidUrl ( changeInfo . url ) ) {
330+ } else if ( changeInfo . url ) {
331+ if ( isRecordingEnabled && isValidUrl ( changeInfo . url ) && ! self . recorders [ tabId ] ) {
332+ // @ts -expect-error - TS2554 - Expected 2 arguments, but got 3.
333+ startRecorder ( tabId , { collId : defaultCollId , autorun } , changeInfo . url ) ;
285334 return ;
286335 }
287- // @ts -expect-error - TS2554 - Expected 2 arguments, but got 3.
288- startRecorder ( tabId , { collId, autorun } , changeInfo . url ) ;
336+ if ( openWinMap . has ( changeInfo . url ) ) {
337+ const collId = openWinMap . get ( changeInfo . url ) ;
338+ openWinMap . delete ( changeInfo . url ) ;
339+ if ( ! tabId || ! isValidUrl ( changeInfo . url ) ) return ;
340+
341+ // @ts -expect-error - TS2554 - Expected 2 arguments, but got 3.
342+ startRecorder ( tabId , { collId, autorun } , changeInfo . url ) ;
343+ }
289344 }
290345} ) ;
291346
0 commit comments