@@ -265,14 +265,26 @@ function changeCurrLocation(tabObj, rootNode, index, name) {
265265 }
266266}
267267
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+
268280/*
269281 The 'chrome.runtime' API allows a connection to the background service worker (background.js).
270282 This allows us to set up listener's for when we connect, message, and disconnect the script.
271283*/
272284
273285// INCOMING CONNECTION FROM FRONTEND (MainContainer) TO BACKGROUND.JS
274286// Establishing incoming connection with Reactime.
275- chrome . runtime . onConnect . addListener ( ( port ) => {
287+ chrome . runtime . onConnect . addListener ( async ( port ) => {
276288 /*
277289 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.
278290
@@ -293,17 +305,33 @@ chrome.runtime.onConnect.addListener((port) => {
293305 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.
294306 */
295307 portsArr . push ( port ) ; // push each Reactime communication channel object to the portsArr
296-
297308 // 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 ( ) {
299314 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+ } ) ;
306319 } ) ;
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+ } ;
307335 }
308336
309337 if ( Object . keys ( tabsObj ) . length > 0 ) {
@@ -381,6 +409,7 @@ chrome.runtime.onConnect.addListener((port) => {
381409 return true ; // return true so that port remains open
382410
383411 case 'launchContentScript' :
412+ //if (tab.url?.startsWith("chrome://")) return undefined;
384413 chrome . scripting . executeScript ( {
385414 target : { tabId } ,
386415 files : [ 'bundles/content.bundle.js' ] ,
@@ -526,6 +555,7 @@ chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
526555 htmlBody . appendChild ( script ) ;
527556 } ;
528557
558+ //if (tab.url?.startsWith("chrome://")) return undefined;
529559 chrome . scripting . executeScript ( {
530560 target : { tabId } ,
531561 func : injectScript ,
@@ -674,6 +704,14 @@ chrome.tabs.onActivated.addListener((info) => {
674704 // never set a reactime instance to the active tab
675705 if ( ! tab . pendingUrl ?. match ( '^chrome-extension' ) ) {
676706 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 ) ;
677715 if ( portsArr . length > 0 ) {
678716 portsArr . forEach ( ( bg ) =>
679717 bg . postMessage ( {
0 commit comments