From 7109d33e4e1e2dbdad359220aed8f32de1871611 Mon Sep 17 00:00:00 2001 From: tophf Date: Wed, 12 Feb 2020 15:49:14 +0300 Subject: [PATCH] wait for tabs to load when reinjecting (#849) --- background/content-scripts.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/background/content-scripts.js b/background/content-scripts.js index d617796a..aa76ebf7 100644 --- a/background/content-scripts.js +++ b/background/content-scripts.js @@ -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}); + } + }); + } }); } })();