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