promisify chrome.tabs.query as queryTabs()

This commit is contained in:
tophf 2017-06-17 13:00:10 +03:00
parent b4e00cd892
commit fba85b36cc
5 changed files with 26 additions and 18 deletions

View File

@ -16,6 +16,7 @@ globals:
URLS: false
BG: false
notifyAllTabs: false
queryTabs: false
getTab: false
getActiveTab: false
getActiveTabRealURL: false

View File

@ -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) {

View File

@ -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 => {

View File

@ -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()];

View File

@ -75,8 +75,10 @@ function notifyAllTabs(msg) {
}
};
// list all tabs including chrome-extension:// which can be ours
chrome.tabs.query(affectsOwnOriginOnly ? {url: URLS.ownOrigin + '*'} : {}, tabs => {
getActiveTab().then(activeTab => {
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) {
@ -86,7 +88,6 @@ function notifyAllTabs(msg) {
}
}
});
});
}
// notify self: the message no longer is sent to the origin in new Chrome
if (typeof onRuntimeMessage != 'undefined') {
@ -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);