I am working on a chrome extension, I have the following code in my popup.js which works correctly if the background page finishes loading, however, if I try to open the extension before the page finishes loading there is a JS error and nothing get executed;
There error I get in the extension report is: Uncaught TypeError: Cannot read properties of undefined (reading 'query') I have tried a few typeOf == undefined and add a delay until it loads but with not success.
function initHome()
{
try {
chrome.tabs.query({
//This method output active URL
"active": true,
"currentWindow": true,
"status": "complete",
"windowType": "normal"
}, function (tabs) {
for (tab in tabs) {
if(tabs[tab].active == true)
{
var newUrl = new URL(tabs[tab].url);
var currentTab = tabs[0];
currentDomain = newUrl.hostname.replace('www.','');
$('#header_domain').html(currentDomain);
if(currentDomain.indexOf('google.') == -1 && currentDomain.indexOf('chrome') == -1)
process_home(currentDomain);
}
}
});
} catch (error) {
console.error(error);
}
}
initHome();