LEFT | RIGHT |
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 = [100, 100, 100, 255]; | 26 let badgeColor = "#646464"; |
27 | 27 |
28 /** | 28 /** |
29 * Get statistics for specified tab | 29 * Get statistics for specified tab |
30 * @param {String} key field key | 30 * @param {String} key field key |
31 * @param {Number} tabId tab ID (leave undefined for total stats) | 31 * @param {Number} tabId tab ID (leave undefined for total stats) |
32 * @return {Number} field value | 32 * @return {Number} field value |
33 */ | 33 */ |
34 let getStats = exports.getStats = function getStats(key, tabId) | 34 let getStats = exports.getStats = function getStats(key, tab) |
35 { | 35 { |
36 if (tabId) | 36 if (!tab) |
37 { | |
38 let frameData = getFrameData(tabId, 0); | |
39 return (frameData && key in frameData ? frameData[key] : 0); | |
40 } | |
41 else | |
42 return (key in Prefs.stats_total ? Prefs.stats_total[key] : 0); | 37 return (key in Prefs.stats_total ? Prefs.stats_total[key] : 0); |
| 38 |
| 39 let frameData = getFrameData(tab, 0); |
| 40 return (frameData && key in frameData ? frameData[key] : 0); |
43 }; | 41 }; |
44 | 42 |
45 FilterNotifier.addListener(function(action, item, newValue, oldValue, tabId) | 43 FilterNotifier.addListener(function(action, item, newValue, oldValue, tab) |
46 { | 44 { |
47 if (action != "filter.hitCount") | 45 if (action != "filter.hitCount") |
48 return; | 46 return; |
49 | 47 |
50 let blocked = item instanceof BlockingFilter; | 48 let blocked = item instanceof BlockingFilter; |
51 | 49 |
52 // Increment counts | 50 // Increment counts |
53 if (blocked) | 51 if (blocked) |
54 { | 52 { |
55 if ("blocked" in Prefs.stats_total) | 53 if ("blocked" in Prefs.stats_total) |
56 Prefs.stats_total.blocked++; | 54 Prefs.stats_total.blocked++; |
57 else | 55 else |
58 Prefs.stats_total.blocked = 1; | 56 Prefs.stats_total.blocked = 1; |
59 Prefs.stats_total = Prefs.stats_total; | 57 Prefs.stats_total = Prefs.stats_total; |
60 | 58 |
61 let frameData = getFrameData(tabId, 0); | 59 let frameData = getFrameData(tab, 0); |
62 if (frameData) | 60 if (frameData) |
63 { | 61 { |
64 if ("blocked" in frameData) | 62 if ("blocked" in frameData) |
65 frameData.blocked++; | 63 frameData.blocked++; |
66 else | 64 else |
67 frameData.blocked = 1; | 65 frameData.blocked = 1; |
68 } | 66 } |
69 | 67 |
70 // Update number in icon | 68 // Update number in icon |
71 if (Prefs.show_iconnumber) | 69 if (Prefs.show_statsinicon) |
72 { | 70 { |
73 chrome.browserAction.setBadgeBackgroundColor({ | 71 tab.browserAction.setBadge({ |
74 color: badgeColor | 72 color: badgeColor, |
75 }); | 73 number: frameData.blocked |
76 | |
77 chrome.browserAction.setBadgeText({ | |
78 tabId: tabId, | |
79 text: frameData.blocked.toString() | |
80 }); | 74 }); |
81 } | 75 } |
82 } | 76 } |
83 }); | 77 }); |
84 | 78 |
85 /** | 79 /** |
86 * Execute function for each tab in any window | 80 * Execute function for each tab in any window |
87 * @param {Function} func function to be executed | 81 * @param {Function} func function to be executed |
88 */ | 82 */ |
89 | |
90 function forEachTab(func) | 83 function forEachTab(func) |
91 { | 84 { |
92 chrome.windows.getAll(function(windows) | 85 ext.windows.getAll(function(windows) |
93 { | 86 { |
94 for (let i = 0; i < windows.length; i++) | 87 for each (let window in windows) |
95 { | 88 { |
96 chrome.tabs.query({ | 89 window.getAllTabs(function(tabs) |
97 windowId: windows[i].id | |
98 }, function(tabs) | |
99 { | 90 { |
100 for (let i = 0; i < tabs.length; i++) | 91 for (let i = 0; i < tabs.length; i++) |
101 func(tabs[i]); | 92 func(tabs[i]); |
102 }); | 93 }); |
103 } | 94 } |
104 }); | 95 }); |
105 } | 96 } |
106 | 97 |
107 Prefs.addListener(function(name) | 98 Prefs.addListener(function(name) |
108 { | 99 { |
109 if (name != "show_iconnumber") | 100 if (name != "show_statsinicon") |
110 return; | 101 return; |
111 | 102 |
112 if (Prefs.show_iconnumber) | 103 forEachTab(function(tab) |
113 { | 104 { |
114 // Add number to icon | 105 let badge = null; |
115 chrome.browserAction.setBadgeBackgroundColor({ | 106 if (Prefs.show_statsinicon) |
116 color: badgeColor | |
117 }); | |
118 | |
119 forEachTab(function(tab) { | |
120 let frameData = getFrameData(tab.id, 0); | |
121 if (!frameData || !("blocked" in frameData)) | |
122 return; | |
123 | |
124 chrome.browserAction.setBadgeText({ | |
125 tabId: tab.id, | |
126 text: frameData.blocked.toString() | |
127 }); | |
128 }); | |
129 } | |
130 else | |
131 { | |
132 // Remove number from icon | |
133 forEachTab(function(tab) | |
134 { | 107 { |
135 chrome.browserAction.setBadgeText({ | 108 let frameData = getFrameData(tab, 0); |
136 tabId: tab.id, | 109 if (frameData && "blocked" in frameData) |
137 text: "" | 110 { |
138 }); | 111 badge = { |
139 }); | 112 color: badgeColor, |
140 } | 113 number: frameData.blocked |
| 114 }; |
| 115 } |
| 116 } |
| 117 tab.browserAction.setBadge(badge); |
| 118 }); |
141 }); | 119 }); |
LEFT | RIGHT |