Lazify notifyAllTabs() and injectCS()

* notifyAllTabs: the active tab gets notified immediately, the rest lazily
* notifyAllTabs + injectCS: don't notify lazy-loaded tabs in FF
This commit is contained in:
tophf 2017-06-17 08:49:12 +03:00
parent c5a340e44e
commit b4e00cd892
2 changed files with 28 additions and 12 deletions

View File

@ -202,9 +202,13 @@ contextMenus = Object.assign({
}; };
chrome.tabs.query({}, tabs => chrome.tabs.query({}, tabs =>
tabs.forEach(tab => tabs.forEach(tab => {
contentScripts.forEach(cs => // skip lazy-loaded aka unloaded tabs that seem to start loading on message in FF
pingCS(cs, tab)))); if (!FIREFOX || tab.width) {
contentScripts.forEach(cs =>
setTimeout(pingCS, 0, cs, tab));
}
}));
} }

View File

@ -62,18 +62,30 @@ function notifyAllTabs(msg) {
const affectsPopup = affectsAll || msg.affects.popup; const affectsPopup = affectsAll || msg.affects.popup;
const affectsSelf = affectsPopup || msg.prefs; const affectsSelf = affectsPopup || msg.prefs;
if (affectsTabs || affectsIcon) { 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 // list all tabs including chrome-extension:// which can be ours
chrome.tabs.query(affectsOwnOriginOnly ? {url: URLS.ownOrigin + '*'} : {}, tabs => { chrome.tabs.query(affectsOwnOriginOnly ? {url: URLS.ownOrigin + '*'} : {}, tabs => {
for (const tab of tabs) { getActiveTab().then(activeTab => {
// own pages will be notified via runtime.sendMessage later const activeTabId = activeTab && activeTab.id;
if ((affectsTabs || URLS.optionsUI.includes(tab.url)) for (const tab of tabs) {
&& !(affectsSelf && tab.url.startsWith(URLS.ownOrigin))) { if (tab.id === activeTabId) {
chrome.tabs.sendMessage(tab.id, msg); 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 // notify self: the message no longer is sent to the origin in new Chrome