OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2013 Eyeo GmbH | 3 * Copyright (C) 2006-2013 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 /** | 18 /** |
19 * @fileOverview Provides usage stats | 19 * @fileOverview Provides usage stats |
20 */ | 20 */ |
21 | 21 |
22 let {Prefs} = require("prefs"); | 22 let {Prefs} = require("prefs"); |
23 let {BlockingFilter} = require("filterClasses"); | 23 let {BlockingFilter} = require("filterClasses"); |
24 let {FilterNotifier} = require("filterNotifier"); | 24 let {FilterNotifier} = require("filterNotifier"); |
25 | 25 |
| 26 let badgeColor = "#646464"; |
| 27 |
26 /** | 28 /** |
27 * Get statistics for specified tab | 29 * Get statistics for specified tab |
28 * @param {String} key field key | 30 * @param {String} key field key |
29 * @param {Number} tabId tab ID (leave undefined for total stats) | 31 * @param {Number} tabId tab ID (leave undefined for total stats) |
30 * @return {Number} field value | 32 * @return {Number} field value |
31 */ | 33 */ |
32 let getStats = exports.getStats = function getStats(key, tab) | 34 let getStats = exports.getStats = function getStats(key, tab) |
33 { | 35 { |
34 if (!tab) | 36 if (!tab) |
35 return (key in Prefs.stats_total ? Prefs.stats_total[key] : 0); | 37 return (key in Prefs.stats_total ? Prefs.stats_total[key] : 0); |
36 | 38 |
37 let frameData = getFrameData(tab, 0); | 39 let frameData = getFrameData(tab, 0); |
38 return (frameData && key in frameData ? frameData[key] : 0); | 40 return (frameData && key in frameData ? frameData[key] : 0); |
39 }; | 41 }; |
40 | 42 |
41 FilterNotifier.addListener(function(action, item, newValue, oldValue, tab) | 43 FilterNotifier.addListener(function(action, item, newValue, oldValue, tab) |
42 { | 44 { |
43 if (action != "filter.hitCount") | 45 if (action != "filter.hitCount") |
44 return; | 46 return; |
45 | 47 |
46 var blocked = item instanceof BlockingFilter; | 48 let blocked = item instanceof BlockingFilter; |
47 | 49 |
48 // Increment counts | 50 // Increment counts |
49 if (blocked) | 51 if (blocked) |
50 { | 52 { |
51 if ("blocked" in Prefs.stats_total) | 53 if ("blocked" in Prefs.stats_total) |
52 Prefs.stats_total.blocked++; | 54 Prefs.stats_total.blocked++; |
53 else | 55 else |
54 Prefs.stats_total.blocked = 1; | 56 Prefs.stats_total.blocked = 1; |
55 Prefs.stats_total = Prefs.stats_total; | 57 Prefs.stats_total = Prefs.stats_total; |
56 | 58 |
57 let frameData = getFrameData(tab, 0); | 59 let frameData = getFrameData(tab, 0); |
58 if (frameData) | 60 if (frameData) |
59 { | 61 { |
60 if ("blocked" in frameData) | 62 if ("blocked" in frameData) |
61 frameData.blocked++; | 63 frameData.blocked++; |
62 else | 64 else |
63 frameData.blocked = 1; | 65 frameData.blocked = 1; |
64 } | 66 } |
| 67 |
| 68 // Update number in icon |
| 69 if (Prefs.show_statsinicon) |
| 70 { |
| 71 tab.browserAction.setBadgeBackgroundColor(badgeColor); |
| 72 tab.browserAction.setBadgeNumber(frameData.blocked); |
| 73 } |
65 } | 74 } |
66 }); | 75 }); |
| 76 |
| 77 /** |
| 78 * Execute function for each tab in any window |
| 79 * @param {Function} func function to be executed |
| 80 */ |
| 81 function forEachTab(func) |
| 82 { |
| 83 ext.windows.getAll(function(windows) |
| 84 { |
| 85 for each (let window in windows) |
| 86 { |
| 87 window.getAllTabs(function(tabs) |
| 88 { |
| 89 for (let i = 0; i < tabs.length; i++) |
| 90 func(tabs[i]); |
| 91 }); |
| 92 } |
| 93 }); |
| 94 } |
| 95 |
| 96 Prefs.addListener(function(name) |
| 97 { |
| 98 if (name != "show_statsinicon") |
| 99 return; |
| 100 |
| 101 if (Prefs.show_statsinicon) |
| 102 { |
| 103 // Add number to icon |
| 104 forEachTab(function(tab) |
| 105 { |
| 106 tab.browserAction.setBadgeBackgroundColor(badgeColor); |
| 107 |
| 108 let frameData = getFrameData(tab, 0); |
| 109 if (frameData && "blocked" in frameData) |
| 110 tab.browserAction.setBadgeNumber(frameData.blocked); |
| 111 }); |
| 112 } |
| 113 else |
| 114 { |
| 115 // Remove number from icon |
| 116 forEachTab(function(tab) |
| 117 { |
| 118 tab.browserAction.setBadgeNumber(null); |
| 119 }); |
| 120 } |
| 121 }); |
OLD | NEW |