This is part of a Chrome Extension (MV3).
A helper function in background.js
tries to determine the active Chrome tab:
async function getCurrentTab() {
let queryOptions = { active: true, currentWindow: true };
let [tab] = await chrome.tabs.query(queryOptions);
if (chrome.runtime.lastError) {
console.log(`[getCurrentTab] Error : ${chrome.runtime.lastError.message}.`);
window.setTimeout(() => getCurrentTab(), 100);
} else {
console.log(`[getCurrentTab] ✅`);
}
return tab;
}
When the extension performs an action (i.e. sends a message to contentScript.js
), I pass the activeTab
as an argument to chrome.tabs.sendMessage
:
getCurrentTab().then((activeTab) => {
if (!isAuthenticated) {
chrome.tabs.sendMessage(activeTab.id, { name: "unauthenticated" }, function(response) {
if (chrome.runtime.lastError) {
console.log(`[background.js] Error from response for "unauthenticated" received : ${chrome.runtime.lastError.message}`);
} else {
console.log(`[background.js] Response for "unauthenticated" received ✅: ${response}`);
}
});
return;
}
This part (activeTab.id
) sometimes (it's ad-hoc) throws this error:
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'id')
I haven't been able to identify a pattern in terms of the error, and most time a page refresh will fix it.
Any clues?
EDIT (based on comment from @lawrence-cherone)
I've now changed code to:
async function getCurrentTab() {
let queryOptions = { active: true, currentWindow: true };
let [tab] = await chrome.tabs.query(queryOptions);
if (chrome.runtime.lastError) {
console.log(`[getCurrentTab] Error : ${chrome.runtime.lastError.message}.`);
await asyncTimeout(100);
} else {
console.log(`[getCurrentTab] ✅`);
}
return tab;
}
const asyncTimeout = (ms) => {
return new Promise((resolve) => {
window.setTimeout(() => {
getCurrentTab(),
ms
});
});
};
Let's see what happens.