Index: lib/stats.js |
=================================================================== |
--- a/lib/stats.js |
+++ b/lib/stats.js |
@@ -23,6 +23,8 @@ |
let {BlockingFilter} = require("filterClasses"); |
let {FilterNotifier} = require("filterNotifier"); |
+let badgeColor = [100, 100, 100, 255]; |
+ |
/** |
* Get statistics for specified tab |
* @param {String} key field key |
@@ -45,7 +47,7 @@ |
if (action != "filter.hitCount") |
return; |
- var blocked = item instanceof BlockingFilter; |
+ let blocked = item instanceof BlockingFilter; |
// Increment counts |
if (blocked) |
@@ -64,5 +66,76 @@ |
else |
frameData.blocked = 1; |
} |
+ |
+ // Update number in icon |
+ if (Prefs.show_iconnumber) |
+ { |
+ chrome.browserAction.setBadgeBackgroundColor({ |
+ color: badgeColor |
+ }); |
+ |
+ chrome.browserAction.setBadgeText({ |
+ tabId: tabId, |
+ text: frameData.blocked.toString() |
+ }); |
+ } |
} |
}); |
+ |
+/** |
+ * Execute function for each tab in any window |
+ * @param {Function} func function to be executed |
+ */ |
+ |
+function forEachTab(func) |
+{ |
+ chrome.windows.getAll(function(windows) |
+ { |
+ for (let i = 0; i < windows.length; i++) |
+ { |
+ chrome.tabs.query({ |
+ windowId: windows[i].id |
+ }, function(tabs) |
+ { |
+ for (let i = 0; i < tabs.length; i++) |
+ func(tabs[i]); |
+ }); |
+ } |
+ }); |
+} |
+ |
+Prefs.addListener(function(name) |
+{ |
+ if (name != "show_iconnumber") |
+ return; |
+ |
+ if (Prefs.show_iconnumber) |
+ { |
+ // Add number to icon |
+ chrome.browserAction.setBadgeBackgroundColor({ |
+ color: badgeColor |
+ }); |
+ |
+ forEachTab(function(tab) { |
+ let frameData = getFrameData(tab.id, 0); |
+ if (!frameData || !("blocked" in frameData)) |
+ return; |
+ |
+ chrome.browserAction.setBadgeText({ |
+ tabId: tab.id, |
+ text: frameData.blocked.toString() |
+ }); |
+ }); |
+ } |
+ else |
+ { |
+ // Remove number from icon |
+ forEachTab(function(tab) |
+ { |
+ chrome.browserAction.setBadgeText({ |
+ tabId: tab.id, |
+ text: "" |
+ }); |
+ }); |
+ } |
+}); |