@@ -7,9 +7,6 @@ const portsArr = [];
7
7
const reloaded = { } ;
8
8
const firstSnapshotReceived = { } ;
9
9
10
- console . log ( 'in background.js' )
11
- console . log ( portsArr ) ;
12
-
13
10
// Toggle for recording accessibility snapshots
14
11
let toggleAxRecord = false ;
15
12
@@ -268,14 +265,26 @@ function changeCurrLocation(tabObj, rootNode, index, name) {
268
265
}
269
266
}
270
267
268
+ async function getActiveTab ( ) {
269
+ return new Promise ( ( resolve , reject ) => {
270
+ chrome . tabs . query ( { active : true , currentWindow : true } , function ( tabs ) {
271
+ if ( tabs . length > 0 ) {
272
+ resolve ( tabs [ 0 ] . id ) ;
273
+ } else {
274
+ reject ( new Error ( 'No active tab' ) )
275
+ }
276
+ } ) ;
277
+ } )
278
+ }
279
+
271
280
/*
272
281
The 'chrome.runtime' API allows a connection to the background service worker (background.js).
273
282
This allows us to set up listener's for when we connect, message, and disconnect the script.
274
283
*/
275
284
276
285
// INCOMING CONNECTION FROM FRONTEND (MainContainer) TO BACKGROUND.JS
277
286
// Establishing incoming connection with Reactime.
278
- chrome . runtime . onConnect . addListener ( ( port ) => {
287
+ chrome . runtime . onConnect . addListener ( async ( port ) => {
279
288
/*
280
289
On initial connection, there is an onConnect event emitted. The 'addlistener' method provides a communication channel object ('port') when we connect to the service worker ('background.js') and applies it as the argument to it's 1st callback parameter.
281
290
@@ -296,21 +305,33 @@ chrome.runtime.onConnect.addListener((port) => {
296
305
Again, this port object is used for communication within your extension, not for communication with external ports or tabs in the Chrome browser. If you need to interact with specific tabs or external ports, you would use other APIs or methods, such as chrome.tabs or other Chrome Extension APIs.
297
306
*/
298
307
portsArr . push ( port ) ; // push each Reactime communication channel object to the portsArr
299
- console . log ( 'in background.js on line 296' ) ;
300
308
// sets the current Title of the Reactime panel
301
- if ( portsArr . length > 0 && Object . keys ( tabsObj ) . length > 0 ) {
302
- console . log ( JSON . stringify ( 'portsArr' ) ) ;
303
- console . log ( portsArr ) ;
304
- console . log ( 'activeTab' ) ;
305
- console . log ( activeTab ) ;
309
+
310
+ /**
311
+ * Sends messages to ports in the portsArr array, triggering a tab change action.
312
+ */
313
+ function sendMessagesToPorts ( ) {
306
314
portsArr . forEach ( ( bg , index ) => {
307
- // go through each port object (each Reactime instance)
308
- bg . postMessage ( {
309
- // send passed in action object as a message to the current port
310
- action : 'changeTab' ,
311
- payload : { tabId : activeTab . id , title : activeTab . title } ,
312
- } ) ;
315
+ bg . postMessage ( {
316
+ action : 'changeTab' ,
317
+ payload : { tabId : activeTab . id , title : activeTab . title } ,
318
+ } ) ;
313
319
} ) ;
320
+ }
321
+
322
+
323
+ if ( portsArr . length > 0 && Object . keys ( tabsObj ) . length > 0 ) {
324
+ //if the activeTab is not set during the onActivate API, run a query to get the tabId and set activeTab
325
+ if ( ! activeTab ) {
326
+ const tabId = await getActiveTab ( ) ;
327
+ chrome . tabs . get ( tabId , ( tab ) => {
328
+ // never set a reactime instance to the active tab
329
+ if ( ! tab . pendingUrl ?. match ( '^chrome-extension' ) ) {
330
+ activeTab = tab ;
331
+ sendMessagesToPorts ( ) ;
332
+ }
333
+ } ) ;
334
+ } ;
314
335
}
315
336
316
337
if ( Object . keys ( tabsObj ) . length > 0 ) {
@@ -388,7 +409,7 @@ chrome.runtime.onConnect.addListener((port) => {
388
409
return true ; // return true so that port remains open
389
410
390
411
case 'launchContentScript' :
391
- if ( tab . url ?. startsWith ( "chrome://" ) ) return undefined ;
412
+ // if (tab.url?.startsWith("chrome://")) return undefined;
392
413
chrome . scripting . executeScript ( {
393
414
target : { tabId } ,
394
415
files : [ 'bundles/content.bundle.js' ] ,
@@ -534,7 +555,7 @@ chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
534
555
htmlBody . appendChild ( script ) ;
535
556
} ;
536
557
537
- if ( tab . url ?. startsWith ( "chrome://" ) ) return undefined ;
558
+ // if (tab.url?.startsWith("chrome://")) return undefined;
538
559
chrome . scripting . executeScript ( {
539
560
target : { tabId } ,
540
561
func : injectScript ,
@@ -683,6 +704,14 @@ chrome.tabs.onActivated.addListener((info) => {
683
704
// never set a reactime instance to the active tab
684
705
if ( ! tab . pendingUrl ?. match ( '^chrome-extension' ) ) {
685
706
activeTab = tab ;
707
+
708
+ /**this setInterval is here to make sure that the app does not stop working even
709
+ * if chrome pauses to save energy. There is probably a better solution, but v25 did
710
+ * not have time to complete.
711
+ */
712
+ setInterval ( ( ) => {
713
+ console . log ( activeTab )
714
+ } , 10000 ) ;
686
715
if ( portsArr . length > 0 ) {
687
716
portsArr . forEach ( ( bg ) =>
688
717
bg . postMessage ( {
0 commit comments