diff --git a/src/js/async.js b/src/js/async.js index 93af28fca..c86fc8c5e 100644 --- a/src/js/async.js +++ b/src/js/async.js @@ -170,28 +170,34 @@ return asyncJobManager; /******************************************************************************/ // Update visual of extension icon. -// A time out is used to coalesce adjacent requests to update badge. -µBlock.updateBadgeAsync = (function(){ +µBlock.updateBadgeAsync = (function() { var µb = µBlock; + var tabIdToTimer = {}; - // Cache callback definition, it was a bad idea to define this one inside - // updateBadgeAsync var updateBadge = function(tabId) { + delete tabIdToTimer[tabId]; + var pageStore = µb.pageStoreFromTabId(tabId); - if ( pageStore ) { - pageStore.updateBadge(); + if ( pageStore === null ) { return; } - vAPI.setIcon(tabId, 'off', ''); + + var netFiltering = pageStore.getNetFilteringSwitch(); + var badge = ''; + if ( µb.userSettings.showIconBadge && netFiltering && pageStore.perLoadBlockedRequestCount ) { + badge = µb.utils.formatCount(pageStore.perLoadBlockedRequestCount); + } + vAPI.setIcon(tabId, netFiltering ? 'on' : 'off', badge); }; - var updateBadgeAsync = function(tabId) { + return function(tabId) { if ( vAPI.isBehindTheSceneTabId(tabId) ) { return; } - µb.asyncJobs.add('updateBadge-' + tabId, tabId, updateBadge, 250); + if ( tabIdToTimer.hasOwnProperty(tabId) ) { + return; + } + tabIdToTimer[tabId] = setTimeout(updateBadge.bind(null, tabId), 500); }; - - return updateBadgeAsync; })(); diff --git a/src/js/pagestore.js b/src/js/pagestore.js index 4bf580465..da201e151 100644 --- a/src/js/pagestore.js +++ b/src/js/pagestore.js @@ -589,17 +589,6 @@ PageStore.prototype.logRequest = function(context, result) { µb.localSettingsModifyTime = now; }; -/******************************************************************************/ - -PageStore.prototype.updateBadge = function() { - var netFiltering = this.getNetFilteringSwitch(); - var badge = ''; - if ( µb.userSettings.showIconBadge && netFiltering && this.perLoadBlockedRequestCount ) { - badge = µb.utils.formatCount(this.perLoadBlockedRequestCount); - } - vAPI.setIcon(this.tabId, netFiltering ? 'on' : 'off', badge); -}; - // https://www.youtube.com/watch?v=drW8p_dTLD4 /******************************************************************************/ diff --git a/src/js/tab.js b/src/js/tab.js index d4cadf1e1..bfc209793 100644 --- a/src/js/tab.js +++ b/src/js/tab.js @@ -556,7 +556,7 @@ vAPI.tabs.registerListeners(); /******************************************************************************/ µb.pageStoreFromTabId = function(tabId) { - return this.pageStores[tabId]; + return this.pageStores[tabId] || null; }; /******************************************************************************/