Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/stats.js

Issue 29785558: Issue 6679 - Don't show block stats in icon if disabled (Closed)
Patch Set: Split logic out into separate function Created May 18, 2018, 12:24 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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-present eyeo GmbH 3 * Copyright (C) 2006-present 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
(...skipping 18 matching lines...) Expand all
29 29
30 let getBlockedPerPage = 30 let getBlockedPerPage =
31 /** 31 /**
32 * Gets the number of requests blocked on the given page. 32 * Gets the number of requests blocked on the given page.
33 * 33 *
34 * @param {Page} page 34 * @param {Page} page
35 * @return {Number} 35 * @return {Number}
36 */ 36 */
37 exports.getBlockedPerPage = page => blockedPerPage.get(page) || 0; 37 exports.getBlockedPerPage = page => blockedPerPage.get(page) || 0;
38 38
39 function updateBadge(page, blockedCount)
40 {
41 if (Prefs.show_statsinicon)
42 {
43 page.browserAction.setBadge(blockedCount && {
44 color: badgeColor,
45 number: blockedCount
46 });
47 }
48 }
49
39 // Once nagivation for the tab has been committed to (e.g. it's no longer 50 // Once nagivation for the tab has been committed to (e.g. it's no longer
40 // being prerendered) we clear its badge, or if some requests were already 51 // being prerendered) we clear its badge, or if some requests were already
41 // blocked beforehand we display those on the badge now. 52 // blocked beforehand we display those on the badge now.
42 browser.webNavigation.onCommitted.addListener(details => 53 browser.webNavigation.onCommitted.addListener(details =>
43 { 54 {
44 if (details.frameId == 0) 55 if (details.frameId == 0)
45 { 56 {
46 let page = new ext.Page({id: details.tabId}); 57 let page = new ext.Page({id: details.tabId});
47 let blocked = blockedPerPage.get(page); 58 let blocked = blockedPerPage.get(page);
48 59
49 page.browserAction.setBadge(blocked && { 60 updateBadge(page, blocked);
50 color: badgeColor,
51 number: blocked
52 });
53 } 61 }
54 }); 62 });
55 63
56 FilterNotifier.on("filter.hitCount", (filter, newValue, oldValue, tabIds) => 64 FilterNotifier.on("filter.hitCount", (filter, newValue, oldValue, tabIds) =>
57 { 65 {
58 if (!(filter instanceof BlockingFilter)) 66 if (!(filter instanceof BlockingFilter))
59 return; 67 return;
60 68
61 for (let tabId of tabIds) 69 for (let tabId of tabIds)
62 { 70 {
63 let page = new ext.Page({id: tabId}); 71 let page = new ext.Page({id: tabId});
64 let blocked = blockedPerPage.get(page) || 0; 72 let blocked = blockedPerPage.get(page) || 0;
73
65 blockedPerPage.set(page, ++blocked); 74 blockedPerPage.set(page, ++blocked);
66 75 updateBadge(page, blocked);
67 // Update number in icon
68 if (Prefs.show_statsinicon)
69 {
70 page.browserAction.setBadge({
71 color: badgeColor,
72 number: blocked
73 });
74 }
75 } 76 }
76 77
77 Prefs.blocked_total++; 78 Prefs.blocked_total++;
78 }); 79 });
79 80
80 Prefs.on("show_statsinicon", () => 81 Prefs.on("show_statsinicon", () =>
81 { 82 {
82 browser.tabs.query({}, tabs => 83 browser.tabs.query({}, tabs =>
83 { 84 {
84 for (let tab of tabs) 85 for (let tab of tabs)
85 { 86 {
86 let page = new ext.Page(tab); 87 let page = new ext.Page(tab);
87 let badge = null;
88 88
89 if (Prefs.show_statsinicon) 89 if (Prefs.show_statsinicon)
90 { 90 updateBadge(page, blockedPerPage.get(page));
91 let blocked = blockedPerPage.get(page); 91 else
92 if (blocked) 92 page.browserAction.setBadge(null);
93 {
94 badge = {
95 color: badgeColor,
96 number: blocked
97 };
98 }
99 }
100
101 page.browserAction.setBadge(badge);
102 } 93 }
103 }); 94 });
104 }); 95 });
105 96
106 port.on("stats.getBlockedPerPage", 97 port.on("stats.getBlockedPerPage",
107 message => getBlockedPerPage(new ext.Page(message.tab))); 98 message => getBlockedPerPage(new ext.Page(message.tab)));
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld