lazify and fix refreshAllTabs

This commit is contained in:
tophf 2017-06-27 12:39:51 +03:00
parent e199e1b3c7
commit 9163a856f6
3 changed files with 24 additions and 5 deletions

View File

@ -18,6 +18,7 @@ globals:
notifyAllTabs: false notifyAllTabs: false
queryTabs: false queryTabs: false
getTab: false getTab: false
getOwnTab: false
getActiveTab: false getActiveTab: false
getActiveTabRealURL: false getActiveTabRealURL: false
getTabRealURL: false getTabRealURL: false

View File

@ -217,7 +217,7 @@ function importFromString(jsonString) {
return new Promise(resolve_ => { return new Promise(resolve_ => {
resolve = resolve_; resolve = resolve_;
undoNextId(); undoNextId();
}).then(BG.refreshAllTabs) }).then(refreshAllTabs)
.then(() => messageBox({ .then(() => messageBox({
title: t('importReportUndoneTitle'), title: t('importReportUndoneTitle'),
contents: newIds.length + ' ' + t('importReportUndone'), contents: newIds.length + ' ' + t('importReportUndone'),
@ -268,19 +268,31 @@ function importFromString(jsonString) {
} }
function refreshAllTabs() { function refreshAllTabs() {
return getActiveTab().then(activeTab => new Promise(resolve => { return Promise.all([
getActiveTab(),
getOwnTab(),
]).then(([activeTab, ownTab]) => new Promise(resolve => {
// list all tabs including chrome-extension:// which can be ours // list all tabs including chrome-extension:// which can be ours
queryTabs().then(tabs => { queryTabs().then(tabs => {
const lastTab = tabs[tabs.length - 1]; const lastTab = tabs[tabs.length - 1];
for (const tab of tabs) { for (const tab of tabs) {
// skip lazy-loaded aka unloaded tabs that seem to start loading on message in FF
if (FIREFOX && !tab.width) {
if (tab == lastTab) {
resolve();
}
continue;
}
getStylesSafe({matchUrl: tab.url, enabled: true, asHash: true}).then(styles => { getStylesSafe({matchUrl: tab.url, enabled: true, asHash: true}).then(styles => {
const message = {method: 'styleReplaceAll', styles}; const message = {method: 'styleReplaceAll', styles};
if (tab.id == activeTab.id) { if (tab.id == ownTab.id) {
applyOnMessage(message); applyOnMessage(message);
} else if (tab.id == activeTab.id) {
chrome.tabs.sendMessage(tab.id, message, ignoreChromeError);
} else { } else {
chrome.tabs.sendMessage(tab.id, message); setTimeout(chrome.tabs.sendMessage, 0, tab.id, message, ignoreChromeError);
} }
BG.updateIcon(tab, styles); setTimeout(BG.updateIcon, 0, tab, styles);
if (tab == lastTab) { if (tab == lastTab) {
resolve(); resolve();
} }

View File

@ -118,6 +118,12 @@ function getTab(id) {
} }
function getOwnTab() {
return new Promise(resolve =>
chrome.tabs.getCurrent(tab => resolve(tab)));
}
function getActiveTab() { function getActiveTab() {
return queryTabs({currentWindow: true, active: true}) return queryTabs({currentWindow: true, active: true})
.then(tabs => tabs[0]); .then(tabs => tabs[0]);