Skip to content

Commit b1e7c6f

Browse files
Haider AliHaider Ali
authored andcommitted
activeTab error is fixed with a query function
1 parent c68e81b commit b1e7c6f

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

src/extension/background.js

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ const portsArr = [];
77
const reloaded = {};
88
const firstSnapshotReceived = {};
99

10-
console.log('in background.js')
11-
console.log(portsArr);
12-
1310
// Toggle for recording accessibility snapshots
1411
let toggleAxRecord = false;
1512

@@ -268,14 +265,26 @@ function changeCurrLocation(tabObj, rootNode, index, name) {
268265
}
269266
}
270267

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+
271280
/*
272281
The 'chrome.runtime' API allows a connection to the background service worker (background.js).
273282
This allows us to set up listener's for when we connect, message, and disconnect the script.
274283
*/
275284

276285
// INCOMING CONNECTION FROM FRONTEND (MainContainer) TO BACKGROUND.JS
277286
// Establishing incoming connection with Reactime.
278-
chrome.runtime.onConnect.addListener((port) => {
287+
chrome.runtime.onConnect.addListener(async (port) => {
279288
/*
280289
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.
281290
@@ -296,21 +305,33 @@ chrome.runtime.onConnect.addListener((port) => {
296305
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.
297306
*/
298307
portsArr.push(port); // push each Reactime communication channel object to the portsArr
299-
console.log('in background.js on line 296');
300308
// 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() {
306314
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+
});
313319
});
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+
};
314335
}
315336

316337
if (Object.keys(tabsObj).length > 0) {
@@ -388,7 +409,7 @@ chrome.runtime.onConnect.addListener((port) => {
388409
return true; // return true so that port remains open
389410

390411
case 'launchContentScript':
391-
if (tab.url?.startsWith("chrome://")) return undefined;
412+
//if (tab.url?.startsWith("chrome://")) return undefined;
392413
chrome.scripting.executeScript({
393414
target: { tabId },
394415
files: ['bundles/content.bundle.js'],
@@ -534,7 +555,7 @@ chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
534555
htmlBody.appendChild(script);
535556
};
536557

537-
if (tab.url?.startsWith("chrome://")) return undefined;
558+
//if (tab.url?.startsWith("chrome://")) return undefined;
538559
chrome.scripting.executeScript({
539560
target: { tabId },
540561
func: injectScript,
@@ -683,6 +704,14 @@ chrome.tabs.onActivated.addListener((info) => {
683704
// never set a reactime instance to the active tab
684705
if (!tab.pendingUrl?.match('^chrome-extension')) {
685706
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);
686715
if (portsArr.length > 0) {
687716
portsArr.forEach((bg) =>
688717
bg.postMessage({

0 commit comments

Comments
 (0)