background.js
// create a new tab when alarmed
chrome.alarms.onAlarm.addListener(function(alarm) {
if (alarm.name === 'myAlarm') {
let tabId = 0;
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
const url = 'https://www.vk.com/feed';
chrome.tabs.create({ url }, (tab) => {
tabId = tab.id!;
});
});
}
});
// wait till the new tab will load completely to do the stuff with DOM in the new tab from content.js
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (tab.url!.indexOf('https://www.vk.com/feed') != -1 && changeInfo.status == 'complete') {
// setTimeout
chrome.tabs.sendMessage(tabId, {
from: Sender.React,
message: 'GET_COOKIES'
});
}
});
I've tried this code above, but it doesn't work I am getting status === 'complete' and sending messages to my content.js too early when DOM is not ready (content.js can't find an element and doesn't do anything when it's getting 'GET_COOKIES' message) When I add setTimeout, with, for example, 3000ms for sending messages - that's works fine! But I don't want to do this, because some users can have an unstable connection and they will need more than 3 seconds, and some will need less than 3 seconds with more good connection I've tried 'onHistoryStateUpdated' with 'webNavigation' - but it seems doesn't work too
How should I resolve this? Please, help...