Index: lib/stats.js |
=================================================================== |
--- a/lib/stats.js |
+++ b/lib/stats.js |
@@ -20,79 +20,159 @@ |
"use strict"; |
const {Prefs} = require("./prefs"); |
const {BlockingFilter} = require("../adblockpluscore/lib/filterClasses"); |
const {filterNotifier} = require("../adblockpluscore/lib/filterNotifier"); |
const {port} = require("./messaging"); |
const badgeColor = "#646464"; |
+ |
+// 4 fps. |
+const badgeRefreshRate = 4; |
+ |
let blockedPerPage = new ext.PageMap(); |
let getBlockedPerPage = |
/** |
* Gets the number of requests blocked on the given page. |
* |
* @param {Page} page |
* @return {Number} |
*/ |
exports.getBlockedPerPage = page => blockedPerPage.get(page) || 0; |
-function updateBadge(page, blockedCount) |
+// The ID of the focused window. If all windows lose focus, this is the ID of |
+// the last focused window. |
+let focusedWindowId = -1; |
+ |
+// The ID of the active tab in the focused window. This can be unknown (-1) on |
+// startup and (briefly) when the focus shifts to a different window. |
+let activeTabId = -1; |
+ |
+let Scheduler = function() |
{ |
- if (Prefs.show_statsinicon) |
+ this.scheduled = false; |
+}; |
+Scheduler.prototype = { |
+ schedule(delay, func) |
{ |
- page.browserAction.setBadge(blockedCount && { |
- color: badgeColor, |
- number: blockedCount |
- }); |
+ setTimeout(() => |
+ { |
+ this.scheduled = false; |
+ func(); |
+ }, |
+ delay); |
+ |
+ this.scheduled = true; |
} |
+}; |
+ |
+let badgeUpdateScheduler = new Scheduler(); |
+ |
+function updateBadgeNow() |
+{ |
+ if (activeTabId == -1) |
+ return; |
+ |
+ let page = new ext.Page({id: activeTabId}); |
+ let blockedCount = blockedPerPage.get(page); |
+ |
+ page.browserAction.setBadge(blockedCount && { |
+ color: badgeColor, |
+ number: blockedCount |
+ }); |
+} |
+ |
+function updateBadge(tabId = -1) |
+{ |
+ if (!badgeUpdateScheduler.scheduled && |
+ (tabId == activeTabId || tabId == -1) && activeTabId != -1 && |
+ Prefs.show_statsinicon) |
+ { |
+ // Schedule an update. |
+ badgeUpdateScheduler.schedule(1000 / badgeRefreshRate, updateBadgeNow); |
+ } |
+} |
+ |
+function findAndUpdateActiveTab() |
+{ |
+ browser.tabs.query({active: true, lastFocusedWindow: true}, ([tab]) => |
+ { |
+ if (tab && activeTabId != tab.id) |
+ { |
+ activeTabId = tab.id; |
+ focusedWindowId = tab.windowId; |
+ |
+ updateBadge(); |
+ } |
+ }); |
} |
// Once nagivation for the tab has been committed to (e.g. it's no longer |
// being prerendered) we clear its badge, or if some requests were already |
// blocked beforehand we display those on the badge now. |
browser.webNavigation.onCommitted.addListener(details => |
{ |
if (details.frameId == 0) |
- { |
- let page = new ext.Page({id: details.tabId}); |
- let blocked = blockedPerPage.get(page); |
- |
- updateBadge(page, blocked); |
- } |
+ updateBadge(details.tabId); |
}); |
filterNotifier.on("filter.hitCount", (filter, newValue, oldValue, tabIds) => |
{ |
if (!(filter instanceof BlockingFilter)) |
return; |
for (let tabId of tabIds) |
{ |
let page = new ext.Page({id: tabId}); |
let blocked = blockedPerPage.get(page) || 0; |
blockedPerPage.set(page, ++blocked); |
- updateBadge(page, blocked); |
+ updateBadge(tabId); |
} |
Prefs.blocked_total++; |
}); |
Prefs.on("show_statsinicon", () => |
{ |
browser.tabs.query({}, tabs => |
{ |
for (let tab of tabs) |
{ |
let page = new ext.Page(tab); |
if (Prefs.show_statsinicon) |
- updateBadge(page, blockedPerPage.get(page)); |
+ updateBadge(tab.id); |
else |
page.browserAction.setBadge(null); |
} |
}); |
}); |
port.on("stats.getBlockedPerPage", |
message => getBlockedPerPage(new ext.Page(message.tab))); |
+ |
+browser.tabs.onActivated.addListener(tab => |
+{ |
+ if (tab.windowId == focusedWindowId && activeTabId != tab.tabId) |
+ { |
+ activeTabId = tab.tabId; |
+ updateBadge(); |
+ } |
+}); |
+ |
+if ("windows" in browser) |
+{ |
+ browser.windows.onFocusChanged.addListener(windowId => |
+ { |
+ if (windowId == browser.windows.WINDOW_ID_NONE) |
+ return; |
+ |
+ focusedWindowId = windowId; |
+ activeTabId = -1; |
+ |
+ findAndUpdateActiveTab(); |
+ }); |
+} |
+ |
+findAndUpdateActiveTab(); |