Use chrome.tabs.query instead of 2-tier window+tabs

This commit is contained in:
tophf 2017-03-15 20:15:32 +03:00
parent 59e0f3f45e
commit 31a24717b4

View File

@ -3,15 +3,14 @@ const KEEP_CHANNEL_OPEN = true;
const OWN_ORIGIN = chrome.runtime.getURL(''); const OWN_ORIGIN = chrome.runtime.getURL('');
function notifyAllTabs(request) { function notifyAllTabs(request) {
chrome.windows.getAll({populate: true}, windows => { // list all tabs including chrome-extension:// which can be ours
windows.forEach(win => { chrome.tabs.query({}, tabs => {
win.tabs.forEach(tab => { for (let tab of tabs) {
if (request.codeIsUpdated !== false || tab.url.startsWith(OWN_ORIGIN)) { if (request.codeIsUpdated !== false || tab.url.startsWith(OWN_ORIGIN)) {
chrome.tabs.sendMessage(tab.id, request); chrome.tabs.sendMessage(tab.id, request);
updateIcon(tab); updateIcon(tab);
} }
}); }
});
}); });
// notify all open popups // notify all open popups
const reqPopup = Object.assign({}, request, {method: 'updatePopup', reason: request.method}); const reqPopup = Object.assign({}, request, {method: 'updatePopup', reason: request.method});
@ -24,23 +23,23 @@ function notifyAllTabs(request) {
function refreshAllTabs() { function refreshAllTabs() {
return new Promise(resolve => { return new Promise(resolve => {
chrome.windows.getAll({populate: true}, windows => { // list all tabs including chrome-extension:// which can be ours
windows.forEach((win, winIndex) => { chrome.tabs.query({}, tabs => {
win.tabs.forEach((tab, tabIndex) => { const lastTab = tabs[tabs.length - 1];
getStyles({matchUrl: tab.url, enabled: true, asHash: true}, styles => { for (let tab of tabs) {
const message = {method: 'styleReplaceAll', styles}; getStyles({matchUrl: tab.url, enabled: true, asHash: true}, styles => {
if (tab.url == location.href && typeof applyOnMessage !== 'undefined') { const message = {method: 'styleReplaceAll', styles};
applyOnMessage(message); if (tab.url == location.href && typeof applyOnMessage !== 'undefined') {
} else { applyOnMessage(message);
chrome.tabs.sendMessage(tab.id, message); } else {
} chrome.tabs.sendMessage(tab.id, message);
updateIcon(tab, styles); }
if (winIndex == windows.length - 1 && tabIndex == win.tabs.length - 1) { updateIcon(tab, styles);
resolve(); if (tab == lastTab) {
} resolve();
}); }
}); });
}); }
}); });
}); });
} }