From 77cfb56d94d1548c8a6ae34a92d981e1328b586c Mon Sep 17 00:00:00 2001 From: gorhill Date: Sun, 26 Oct 2014 13:36:27 -0400 Subject: [PATCH] this fixes #12 --- src/js/async.js | 34 ++++++++++++++++++++++++---------- src/js/pagestats.js | 10 ++++++---- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/js/async.js b/src/js/async.js index 2580f12..f9b065f 100644 --- a/src/js/async.js +++ b/src/js/async.js @@ -133,8 +133,12 @@ return asyncJobManager; // Update visual of extension icon. // A time out is used to coalesce adjacent requests to update badge. -µMatrix.updateBadge = function(pageUrl) { - var updateBadgeCallback = function(pageUrl) { +µMatrix.updateBadgeAsync = (function(){ + var µm = µMatrix; + + // Cache callback definition, it was a bad idea to define this one inside + // updateBadgeAsync + var updateBadge = function(pageUrl) { var µm = µMatrix; if ( pageUrl === µm.behindTheSceneURL ) { return; @@ -143,17 +147,27 @@ return asyncJobManager; if ( !tabId ) { return; } - var pageStats = µm.pageStatsFromTabId(tabId); - if ( pageStats ) { - pageStats.updateBadge(tabId); - } else { - chrome.browserAction.setIcon({ tabId: tabId, path: 'img/browsericons/icon19.png' }); - chrome.browserAction.setBadgeText({ tabId: tabId, text: '?' }); + var pageStore = µm.pageStatsFromTabId(tabId); + if ( pageStore ) { + pageStore.updateBadge(tabId); + return; } + µm.XAL.setIcon( + tabId, + { '19': 'img/browsericons/icon19.png' }, + '?' + ); }; - this.asyncJobs.add('updateBadge ' + pageUrl, pageUrl, updateBadgeCallback, 250); -}; + var updateBadgeAsync = function(pageUrl) { + if ( typeof pageUrl !== 'string' || pageUrl === '' ) { + return; + } + µm.asyncJobs.add('updateBadge-' + pageUrl, pageUrl, updateBadge, 250); + }; + + return updateBadgeAsync; +})(); /******************************************************************************/ diff --git a/src/js/pagestats.js b/src/js/pagestats.js index 9b36a4c..783a307 100644 --- a/src/js/pagestats.js +++ b/src/js/pagestats.js @@ -504,7 +504,7 @@ PageStore.prototype.recordRequest = function(type, url, block) { // rhill 2013-10-26: This needs to be called even if the request is // already logged, since the request stats are cached for a while after // the page is no longer visible in a browser tab. - µm.updateBadge(this.pageUrl); + µm.updateBadgeAsync(this.pageUrl); // Count blocked/allowed requests this.requestStats.record(type, block); @@ -571,9 +571,11 @@ PageStore.prototype.updateBadge = function(tabId) { } else { iconPath = 'img/browsericons/icon19.png'; } - chrome.browserAction.setIcon({ tabId: tabId, path: iconPath }); - chrome.browserAction.setBadgeText({ tabId: tabId, text: µm.formatCount(this.distinctRequestCount) }); - chrome.browserAction.setBadgeBackgroundColor({ tabId: tabId, color: '#000' }); + µm.XAL.setIcon( + tabId, + iconPath, + µm.formatCount(this.distinctRequestCount) + ); }; /******************************************************************************/