From e2c79b3919aa33bd30c49fcd05d02f607e6b9eeb Mon Sep 17 00:00:00 2001 From: gorhill Date: Mon, 4 Aug 2014 08:42:57 -0400 Subject: [PATCH] this fixes #19 --- js/async.js | 2 +- js/pagestore.js | 33 ++++++++++++++------------------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/js/async.js b/js/async.js index c232829a2..a8a9c57a1 100644 --- a/js/async.js +++ b/js/async.js @@ -145,5 +145,5 @@ return asyncJobManager; chrome.browserAction.setIcon({ tabId: tabId, path: 'img/browsericons/icon19-off.png' }); } }; - this.asyncJobs.add('updateBadge-' + tabId, tabId, updateBadgeCallback, 200); + this.asyncJobs.add('updateBadge-' + tabId, tabId, updateBadgeCallback, 250); }; diff --git a/js/pagestore.js b/js/pagestore.js index 79f96f2bb..d541305c2 100644 --- a/js/pagestore.js +++ b/js/pagestore.js @@ -207,38 +207,33 @@ PageStore.prototype.recordRequest = function(type, url, reason) { /******************************************************************************/ -// Update badge, incrementally - -// rhill 2013-11-09: well this sucks, I can't update icon/badge -// incrementally, as chromium overwrites the icon at some point without -// notifying me, and this causes internal cached state to be out of sync. - -PageStore.prototype.updateBadge = function() { - // https://github.com/gorhill/uBlock/issues/19 - // TODO: need to check with µb object to see whether tab still exists. - +PageStore.prototype.updateBadgeFromTab = function(tab) { + if ( !tab ) { + return; + } var netFiltering = this.getNetFilteringSwitch(); var iconPath = netFiltering ? 'img/browsericons/icon19.png' : 'img/browsericons/icon19-off.png'; - chrome.browserAction.setIcon({ tabId: this.tabId, path: iconPath }); + chrome.browserAction.setIcon({ tabId: tab.id, path: iconPath }); var iconStr = ''; if ( µb.userSettings.showIconBadge && netFiltering && this.perLoadBlockedRequestCount ) { iconStr = this.perLoadBlockedRequestCount.toLocaleString(); } - chrome.browserAction.setBadgeText({ - tabId: this.tabId, - text: iconStr - }); + chrome.browserAction.setBadgeText({ tabId: tab.id, text: iconStr }); if ( iconStr !== '' ) { - chrome.browserAction.setBadgeBackgroundColor({ - tabId: this.tabId, - color: '#666' - }); + chrome.browserAction.setBadgeBackgroundColor({ tabId: tab.id, 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)); +}; + /******************************************************************************/ return {