From b4e00cd892e09dff9e38802c00fdd0c6b0c0568e Mon Sep 17 00:00:00 2001 From: tophf Date: Sat, 17 Jun 2017 08:49:12 +0300 Subject: [PATCH] Lazify notifyAllTabs() and injectCS() * notifyAllTabs: the active tab gets notified immediately, the rest lazily * notifyAllTabs + injectCS: don't notify lazy-loaded tabs in FF --- background.js | 10 +++++++--- messaging.js | 30 +++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/background.js b/background.js index eed9a9aa..976d4a82 100644 --- a/background.js +++ b/background.js @@ -202,9 +202,13 @@ contextMenus = Object.assign({ }; chrome.tabs.query({}, tabs => - tabs.forEach(tab => - contentScripts.forEach(cs => - pingCS(cs, tab)))); + tabs.forEach(tab => { + // skip lazy-loaded aka unloaded tabs that seem to start loading on message in FF + if (!FIREFOX || tab.width) { + contentScripts.forEach(cs => + setTimeout(pingCS, 0, cs, tab)); + } + })); } diff --git a/messaging.js b/messaging.js index 5cf66013..93e053c0 100644 --- a/messaging.js +++ b/messaging.js @@ -62,18 +62,30 @@ function notifyAllTabs(msg) { const affectsPopup = affectsAll || msg.affects.popup; const affectsSelf = affectsPopup || msg.prefs; if (affectsTabs || affectsIcon) { + const notifyTab = tab => { + // own pages will be notified via runtime.sendMessage later + if ((affectsTabs || URLS.optionsUI.includes(tab.url)) + && !(affectsSelf && tab.url.startsWith(URLS.ownOrigin)) + // skip lazy-loaded aka unloaded tabs that seem to start loading on message in FF + && (!FIREFOX || tab.width)) { + chrome.tabs.sendMessage(tab.id, msg); + } + if (affectsIcon && BG) { + BG.updateIcon(tab); + } + }; // list all tabs including chrome-extension:// which can be ours chrome.tabs.query(affectsOwnOriginOnly ? {url: URLS.ownOrigin + '*'} : {}, tabs => { - for (const tab of tabs) { - // own pages will be notified via runtime.sendMessage later - if ((affectsTabs || URLS.optionsUI.includes(tab.url)) - && !(affectsSelf && tab.url.startsWith(URLS.ownOrigin))) { - chrome.tabs.sendMessage(tab.id, msg); + getActiveTab().then(activeTab => { + const activeTabId = activeTab && activeTab.id; + for (const tab of tabs) { + if (tab.id === activeTabId) { + notifyTab(tab); + } else { + setTimeout(notifyTab, 0, tab); + } } - if (affectsIcon && BG) { - BG.updateIcon(tab); - } - } + }); }); } // notify self: the message no longer is sent to the origin in new Chrome