OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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 /** @module stats */ | 18 /** @module stats */ |
19 | 19 |
20 let {Prefs} = require("prefs"); | 20 let {Prefs} = require("prefs"); |
21 let {BlockingFilter} = require("filterClasses"); | 21 let {BlockingFilter} = require("filterClasses"); |
22 let {FilterNotifier} = require("filterNotifier"); | 22 let {FilterNotifier} = require("filterNotifier"); |
23 | 23 |
24 let badgeColor = "#646464"; | 24 let badgeColor = "#646464"; |
25 let statsPerPage = new ext.PageMap(); | 25 let blockedPerPage = new ext.PageMap(); |
26 | 26 |
27 /** | 27 /** |
28 * Get statistics for specified page | 28 * Gets the number of requests blocked on the given page. |
29 * @param {String} key field key | 29 * |
30 * @param {Page} page field page | 30 * @param {Page} page |
31 * @return {Number} field value | 31 * @return {Number} |
32 */ | 32 */ |
33 exports.getStats = function(key, page) | 33 exports.getBlockedPerPage = function(page) |
34 { | 34 { |
35 if (!page) | 35 return blockedPerPage.get(page) || 0; |
36 return (key in Prefs.stats_total ? Prefs.stats_total[key] : 0); | |
37 | |
38 let pageStats = statsPerPage.get(page); | |
39 return pageStats ? pageStats.blocked : 0; | |
40 }; | 36 }; |
41 | 37 |
42 FilterNotifier.addListener(function(action, item, newValue, oldValue, page) | 38 FilterNotifier.addListener(function(action, item, newValue, oldValue, page) |
43 { | 39 { |
44 if (action != "filter.hitCount" || !page) | 40 if (action != "filter.hitCount" || !page) |
45 return; | 41 return; |
46 | 42 |
47 let blocked = item instanceof BlockingFilter; | 43 if (!(item instanceof BlockingFilter)) |
| 44 return; |
48 | 45 |
49 // Increment counts | 46 Prefs.blocked_total++; |
50 if (blocked) | 47 |
| 48 let blocked = blockedPerPage.get(page) || 0; |
| 49 blockedPerPage.set(page, ++blocked); |
| 50 |
| 51 // Update number in icon |
| 52 if (Prefs.show_statsinicon) |
51 { | 53 { |
52 if ("blocked" in Prefs.stats_total) | 54 page.browserAction.setBadge({ |
53 Prefs.stats_total.blocked++; | 55 color: badgeColor, |
54 else | 56 number: blocked |
55 Prefs.stats_total.blocked = 1; | 57 }); |
56 Prefs.stats_total = Prefs.stats_total; | |
57 | |
58 let pageStats = statsPerPage.get(page); | |
59 if (!pageStats) | |
60 { | |
61 pageStats = {}; | |
62 statsPerPage.set(page, pageStats); | |
63 } | |
64 if ("blocked" in pageStats) | |
65 pageStats.blocked++; | |
66 else | |
67 pageStats.blocked = 1; | |
68 | |
69 // Update number in icon | |
70 if (Prefs.show_statsinicon) | |
71 { | |
72 page.browserAction.setBadge({ | |
73 color: badgeColor, | |
74 number: pageStats.blocked | |
75 }); | |
76 } | |
77 } | 58 } |
78 }); | 59 }); |
79 | 60 |
80 Prefs.onChanged.addListener(function(name) | 61 Prefs.onChanged.addListener(function(name) |
81 { | 62 { |
82 if (name != "show_statsinicon") | 63 if (name != "show_statsinicon") |
83 return; | 64 return; |
84 | 65 |
85 ext.pages.query({}, function(pages) | 66 ext.pages.query({}, function(pages) |
86 { | 67 { |
87 for (var i = 0; i < pages.length; i++) | 68 for (var i = 0; i < pages.length; i++) |
88 { | 69 { |
89 let page = pages[i]; | 70 let page = pages[i]; |
90 let badge = null; | 71 let badge = null; |
91 | 72 |
92 if (Prefs.show_statsinicon) | 73 if (Prefs.show_statsinicon) |
93 { | 74 { |
94 let pageStats = statsPerPage.get(page); | 75 let blocked = blockedPerPage.get(page); |
95 if (pageStats && "blocked" in pageStats) | 76 if (blocked) |
96 { | 77 { |
97 badge = { | 78 badge = { |
98 color: badgeColor, | 79 color: badgeColor, |
99 number: pageStats.blocked | 80 number: blocked |
100 }; | 81 }; |
101 } | 82 } |
102 } | 83 } |
103 | 84 |
104 page.browserAction.setBadge(badge); | 85 page.browserAction.setBadge(badge); |
105 } | 86 } |
106 }); | 87 }); |
107 }); | 88 }); |
OLD | NEW |