@@ -265,14 +265,26 @@ function changeCurrLocation(tabObj, rootNode, index, name) {
265
265
}
266
266
}
267
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
+
268
280
/*
269
281
The 'chrome.runtime' API allows a connection to the background service worker (background.js).
270
282
This allows us to set up listener's for when we connect, message, and disconnect the script.
271
283
*/
272
284
273
285
// INCOMING CONNECTION FROM FRONTEND (MainContainer) TO BACKGROUND.JS
274
286
// Establishing incoming connection with Reactime.
275
- chrome . runtime . onConnect . addListener ( ( port ) => {
287
+ chrome . runtime . onConnect . addListener ( async ( port ) => {
276
288
/*
277
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.
278
290
@@ -293,17 +305,33 @@ chrome.runtime.onConnect.addListener((port) => {
293
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.
294
306
*/
295
307
portsArr . push ( port ) ; // push each Reactime communication channel object to the portsArr
296
-
297
308
// sets the current Title of the Reactime panel
298
- if ( portsArr . length > 0 && Object . keys ( tabsObj ) . length > 0 ) {
309
+
310
+ /**
311
+ * Sends messages to ports in the portsArr array, triggering a tab change action.
312
+ */
313
+ function sendMessagesToPorts ( ) {
299
314
portsArr . forEach ( ( bg , index ) => {
300
- // go through each port object (each Reactime instance)
301
- bg . postMessage ( {
302
- // send passed in action object as a message to the current port
303
- action : 'changeTab' ,
304
- payload : { tabId : activeTab . id , title : activeTab . title } ,
305
- } ) ;
315
+ bg . postMessage ( {
316
+ action : 'changeTab' ,
317
+ payload : { tabId : activeTab . id , title : activeTab . title } ,
318
+ } ) ;
306
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
+ } ;
307
335
}
308
336
309
337
if ( Object . keys ( tabsObj ) . length > 0 ) {
@@ -381,6 +409,7 @@ chrome.runtime.onConnect.addListener((port) => {
381
409
return true ; // return true so that port remains open
382
410
383
411
case 'launchContentScript' :
412
+ //if (tab.url?.startsWith("chrome://")) return undefined;
384
413
chrome . scripting . executeScript ( {
385
414
target : { tabId } ,
386
415
files : [ 'bundles/content.bundle.js' ] ,
@@ -526,6 +555,7 @@ chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
526
555
htmlBody . appendChild ( script ) ;
527
556
} ;
528
557
558
+ //if (tab.url?.startsWith("chrome://")) return undefined;
529
559
chrome . scripting . executeScript ( {
530
560
target : { tabId } ,
531
561
func : injectScript ,
@@ -674,6 +704,14 @@ chrome.tabs.onActivated.addListener((info) => {
674
704
// never set a reactime instance to the active tab
675
705
if ( ! tab . pendingUrl ?. match ( '^chrome-extension' ) ) {
676
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 ) ;
677
715
if ( portsArr . length > 0 ) {
678
716
portsArr . forEach ( ( bg ) =>
679
717
bg . postMessage ( {
0 commit comments