diff --git a/src/js/async.js b/src/js/async.js index 585e94ddf..c86fc8c5e 100644 --- a/src/js/async.js +++ b/src/js/async.js @@ -16,7 +16,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see {http://www.gnu.org/licenses/}. - Home: https://github.com/chrisaljoudi/uBlock + Home: https://github.com/gorhill/uBlock */ /* global µBlock */ @@ -173,24 +173,31 @@ return asyncJobManager; µ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() { - var pageStore = µb.pageStoreFromTabId(tabIdToUpdate); - if ( pageStore ) { - pageStore.updateBadge(); + var updateBadge = function(tabId) { + delete tabIdToTimer[tabId]; + + var pageStore = µb.pageStoreFromTabId(tabId); + if ( pageStore === null ) { return; } - vAPI.setIcon(tabIdToUpdate, '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 657e76115..59b934e03 100644 --- a/src/js/pagestore.js +++ b/src/js/pagestore.js @@ -588,17 +588,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 c0a5a3a3d..e3d5ce39a 100644 --- a/src/js/tab.js +++ b/src/js/tab.js @@ -540,7 +540,7 @@ vAPI.tabs.registerListeners(); }; µb.pageStoreFromTabId = function(tabId) { - return this.pageStores[tabId]; + return this.pageStores[tabId] || null; }; /******************************************************************************/