Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Commit

Permalink
this fixes gorhill#12
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Oct 26, 2014
1 parent 53f8817 commit 521e5b4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
34 changes: 24 additions & 10 deletions src/js/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
})();

/******************************************************************************/

Expand Down
10 changes: 6 additions & 4 deletions src/js/pagestats.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
);
};

/******************************************************************************/
Expand Down

0 comments on commit 521e5b4

Please sign in to comment.