From fba85b36cc046f429e2a8f710fcf72fe2a654fac Mon Sep 17 00:00:00 2001 From: tophf Date: Sat, 17 Jun 2017 13:00:10 +0300 Subject: [PATCH] promisify chrome.tabs.query as queryTabs() --- .eslintrc | 1 + background.js | 2 +- backup/fileSaveLoad.js | 2 +- edit.js | 4 ++-- messaging.js | 35 +++++++++++++++++++++-------------- 5 files changed, 26 insertions(+), 18 deletions(-) diff --git a/.eslintrc b/.eslintrc index 24b34542..b52a054d 100644 --- a/.eslintrc +++ b/.eslintrc @@ -16,6 +16,7 @@ globals: URLS: false BG: false notifyAllTabs: false + queryTabs: false getTab: false getActiveTab: false getActiveTabRealURL: false diff --git a/background.js b/background.js index 976d4a82..ea7db689 100644 --- a/background.js +++ b/background.js @@ -201,7 +201,7 @@ contextMenus = Object.assign({ }); }; - chrome.tabs.query({}, tabs => + queryTabs().then(tabs => tabs.forEach(tab => { // skip lazy-loaded aka unloaded tabs that seem to start loading on message in FF if (!FIREFOX || tab.width) { diff --git a/backup/fileSaveLoad.js b/backup/fileSaveLoad.js index ab57ca70..d0c946a2 100644 --- a/backup/fileSaveLoad.js +++ b/backup/fileSaveLoad.js @@ -270,7 +270,7 @@ function importFromString(jsonString) { function refreshAllTabs() { return getActiveTab().then(activeTab => new Promise(resolve => { // list all tabs including chrome-extension:// which can be ours - chrome.tabs.query({}, tabs => { + queryTabs().then(tabs => { const lastTab = tabs[tabs.length - 1]; for (const tab of tabs) { getStylesSafe({matchUrl: tab.url, enabled: true, asHash: true}).then(styles => { diff --git a/edit.js b/edit.js index 2f9ede93..119b22cd 100644 --- a/edit.js +++ b/edit.js @@ -420,7 +420,7 @@ document.addEventListener("wheel", function(event) { } }); -chrome.tabs.query({currentWindow: true}, function(tabs) { +queryTabs({currentWindow: true}).then(tabs => { var windowId = tabs[0].windowId; if (prefs.get("openEditInWindow")) { if (sessionStorage.saveSizeOnClose @@ -1720,7 +1720,7 @@ function showRegExpTester(event, section = getSectionForChild(this)) { chrome.tabs.onUpdated.removeListener(_); } }); - chrome.tabs.query({}, tabs => { + queryTabs().then(tabs => { const supported = tabs.map(tab => tab.url) .filter(url => URLS.supported.test(url)); const unique = [...new Set(supported).values()]; diff --git a/messaging.js b/messaging.js index 93e053c0..e0be522a 100644 --- a/messaging.js +++ b/messaging.js @@ -75,17 +75,18 @@ function notifyAllTabs(msg) { } }; // list all tabs including chrome-extension:// which can be ours - chrome.tabs.query(affectsOwnOriginOnly ? {url: URLS.ownOrigin + '*'} : {}, tabs => { - getActiveTab().then(activeTab => { - const activeTabId = activeTab && activeTab.id; - for (const tab of tabs) { - if (tab.id === activeTabId) { - notifyTab(tab); - } else { - setTimeout(notifyTab, 0, tab); - } + Promise.all([ + queryTabs(affectsOwnOriginOnly ? {url: URLS.ownOrigin + '*'} : {}), + getActiveTab(), + ]).then(([tabs, activeTab]) => { + const activeTabId = activeTab && activeTab.id; + for (const tab of tabs) { + if (tab.id === activeTabId) { + notifyTab(tab); + } else { + setTimeout(notifyTab, 0, tab); } - }); + } }); } // notify self: the message no longer is sent to the origin in new Chrome @@ -103,6 +104,13 @@ function notifyAllTabs(msg) { } +function queryTabs(options = {}) { + return new Promise(resolve => + chrome.tabs.query(options, tabs => + resolve(tabs))); +} + + function getTab(id) { return new Promise(resolve => chrome.tabs.get(id, tab => @@ -111,9 +119,8 @@ function getTab(id) { function getActiveTab() { - return new Promise(resolve => - chrome.tabs.query({currentWindow: true, active: true}, tabs => - resolve(tabs[0]))); + return queryTabs({currentWindow: true, active: true}) + .then(tabs => tabs[0]); } @@ -148,7 +155,7 @@ function openURL({url, currentWindow = true}) { // FF doesn't handle moz-extension:// URLs (bug) // API doesn't handle the hash-fragment part const urlQuery = url.startsWith('moz-extension') ? undefined : url.replace(/#.*/, ''); - chrome.tabs.query({url: urlQuery, currentWindow}, tabs => { + queryTabs({url: urlQuery, currentWindow}).then(tabs => { for (const tab of tabs) { if (tab.url == url) { activateTab(tab).then(resolve);