From 0b64dbf373fe98a313e38c683b981f6de432db30 Mon Sep 17 00:00:00 2001 From: gorhill Date: Thu, 11 Sep 2014 09:53:49 -0400 Subject: [PATCH] this fixes 207, re. "No tab with id: ..." --- js/async.js | 18 ++++++++++++++---- js/pagestore.js | 18 ++++-------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/js/async.js b/js/async.js index 70dd88253..d483b46e1 100644 --- a/js/async.js +++ b/js/async.js @@ -137,16 +137,26 @@ return asyncJobManager; if ( tabId < 0 ) { return; } - var updateBadgeCallback = function(tabId) { - var pageStore = µBlock.pageStoreFromTabId(tabId); + var safeUpdateBagde = function(tab) { + if ( chrome.runtime.lastError ) { + return; + } + var pageStore = µBlock.pageStoreFromTabId(tab.id); if ( pageStore ) { pageStore.updateBadge(); } else { chrome.browserAction.setIcon({ - tabId: tabId, + tabId: tab.id, path: { '19': 'img/browsericons/icon19-off.png', '38': 'img/browsericons/icon38-off.png' } }); } }; - this.asyncJobs.add('updateBadge-' + tabId, tabId, updateBadgeCallback, 250); + // https://github.com/gorhill/uBlock/issues/19 + // https://github.com/gorhill/uBlock/issues/207 + // Since we may be called asynchronously, the tab id may not exist + // anymore, so this ensures it does still exist. + var unsafeUpdateBadge = function(tabId) { + chrome.tabs.get(tabId, safeUpdateBagde); + }; + this.asyncJobs.add('updateBadge-' + tabId, tabId, unsafeUpdateBadge, 250); }; diff --git a/js/pagestore.js b/js/pagestore.js index a0a42efe8..611b32897 100644 --- a/js/pagestore.js +++ b/js/pagestore.js @@ -211,35 +211,25 @@ PageStore.prototype.recordRequest = function(type, url, reason) { /******************************************************************************/ -PageStore.prototype.updateBadgeFromTab = function(tab) { - if ( !tab ) { - return; - } +PageStore.prototype.updateBadge = function() { var netFiltering = this.getNetFilteringSwitch(); var iconPath = netFiltering ? { '19': 'img/browsericons/icon19.png', '38': 'img/browsericons/icon38.png' } : { '19': 'img/browsericons/icon19-off.png', '38': 'img/browsericons/icon38-off.png' }; - chrome.browserAction.setIcon({ tabId: tab.id, path: iconPath }); + chrome.browserAction.setIcon({ tabId: this.tabId, path: iconPath }); var iconStr = ''; if ( µb.userSettings.showIconBadge && netFiltering && this.perLoadBlockedRequestCount ) { iconStr = µb.utils.formatCount(this.perLoadBlockedRequestCount); } - chrome.browserAction.setBadgeText({ tabId: tab.id, text: iconStr }); + chrome.browserAction.setBadgeText({ tabId: this.tabId, text: iconStr }); if ( iconStr !== '' ) { - chrome.browserAction.setBadgeBackgroundColor({ tabId: tab.id, color: '#666' }); + chrome.browserAction.setBadgeBackgroundColor({ tabId: this.tabId, color: '#666' }); } }; -PageStore.prototype.updateBadge = function() { - // https://github.com/gorhill/uBlock/issues/19 - // Since we may be called asynchronously, the tab id may not exist - // anymore, so this ensures it does still exist. - chrome.tabs.get(this.tabId, this.updateBadgeFromTab.bind(this)); -}; - // https://www.youtube.com/watch?v=drW8p_dTLD4 /******************************************************************************/