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