From fdc15d24d9f0f3b50d238e611b4c8ebe4538c120 Mon Sep 17 00:00:00 2001 From: tophf Date: Sun, 23 Apr 2017 13:54:20 +0300 Subject: [PATCH] try to avoid setBadgeText errors --- .eslintrc | 1 + background.js | 18 ++++++++---------- messaging.js | 7 +++++++ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/.eslintrc b/.eslintrc index 8cc63436..73a2efac 100644 --- a/.eslintrc +++ b/.eslintrc @@ -16,6 +16,7 @@ globals: URLS: false BG: false notifyAllTabs: false + getTab: false getActiveTab: false getActiveTabRealURL: false getTabRealURL: false diff --git a/background.js b/background.js index 415de5ae..0790cea1 100644 --- a/background.js +++ b/background.js @@ -210,12 +210,7 @@ function updateIcon(tab, styles) { return; } if (styles) { - // check for not-yet-existing tabs e.g. omnibox instant search - chrome.tabs.get(tab.id, () => { - if (!chrome.runtime.lastError) { - stylesReceived(styles); - } - }); + stylesReceived(styles); return; } getTabRealURL(tab).then(url => @@ -247,11 +242,14 @@ function updateIcon(tab, styles) { // TODO: add Edge preferred sizes: 20, 25, 30, 40 }, }, () => { - if (!chrome.runtime.lastError) { - // Vivaldi bug workaround: setBadgeText must follow setBadgeBackgroundColor - chrome.browserAction.setBadgeBackgroundColor({color}); - chrome.browserAction.setBadgeText({text, tabId: tab.id}); + if (chrome.runtime.lastError) { + return; } + // Vivaldi bug workaround: setBadgeText must follow setBadgeBackgroundColor + chrome.browserAction.setBadgeBackgroundColor({color}); + getTab(tab.id).then(() => { + chrome.browserAction.setBadgeText({text, tabId: tab.id}); + }); }); } } diff --git a/messaging.js b/messaging.js index b688270a..0a1ad059 100644 --- a/messaging.js +++ b/messaging.js @@ -87,6 +87,13 @@ function notifyAllTabs(msg) { } +function getTab(id) { + return new Promise(resolve => + chrome.tabs.get(id, tab => + !chrome.runtime.lastError && resolve(tab))); +} + + function getActiveTab() { return new Promise(resolve => chrome.tabs.query({currentWindow: true, active: true}, tabs =>