wait for tabs to load when reinjecting (#849)

This commit is contained in:
tophf 2020-02-12 15:49:14 +03:00 committed by GitHub
parent 02a575a9d6
commit 7109d33e4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -54,15 +54,29 @@ const contentScripts = (() => {
function injectToAllTabs() {
return queryTabs({}).then(tabs => {
const busyTabs = new Set();
for (const tab of tabs) {
// skip lazy-loaded aka unloaded tabs that seem to start loading on message in FF
if (tab.width) {
// skip unloaded/discarded tabs
if (!tab.width || tab.discarded) continue;
// our content scripts may still be pending injection at browser start so it's too early to ping them
if (tab.status === 'loading') {
busyTabs.add(tab.id);
} else {
injectToTab({
url: tab.url,
tabId: tab.id
});
}
}
if (busyTabs.size) {
chrome.tabs.onUpdated.addListener(function _(tabId, {status}, {url}) {
if (status === 'complete' && busyTabs.has(tabId)) {
busyTabs.delete(tabId);
if (!busyTabs.size) chrome.tabs.onUpdated.removeListener(_);
injectToTab({tabId, url});
}
});
}
});
}
})();