| Left: | ||
| Right: |
| 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-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 |
| 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 "use strict"; | 20 "use strict"; |
| 21 | 21 |
| 22 const {Prefs} = require("./prefs"); | 22 const {Prefs} = require("./prefs"); |
| 23 const {BlockingFilter} = require("../adblockpluscore/lib/filterClasses"); | 23 const {BlockingFilter} = require("../adblockpluscore/lib/filterClasses"); |
| 24 const {filterNotifier} = require("../adblockpluscore/lib/filterNotifier"); | 24 const {filterNotifier} = require("../adblockpluscore/lib/filterNotifier"); |
| 25 const {port} = require("./messaging"); | 25 const {port} = require("./messaging"); |
| 26 | 26 |
| 27 const badgeColor = "#646464"; | 27 const badgeColor = "#646464"; |
| 28 | |
| 29 // 4 fps. | |
|
Sebastian Noack
2019/02/04 07:16:41
Nit: This comment seems somewhat redundant. Perhap
Manish Jethani
2019/02/04 07:36:14
I think it's fine to just remove the comment.
Don
| |
| 30 const badgeRefreshRate = 4; | |
| 31 | |
| 28 let blockedPerPage = new ext.PageMap(); | 32 let blockedPerPage = new ext.PageMap(); |
| 29 | 33 |
| 30 let getBlockedPerPage = | 34 let getBlockedPerPage = |
| 31 /** | 35 /** |
| 32 * Gets the number of requests blocked on the given page. | 36 * Gets the number of requests blocked on the given page. |
| 33 * | 37 * |
| 34 * @param {Page} page | 38 * @param {Page} page |
| 35 * @return {Number} | 39 * @return {Number} |
| 36 */ | 40 */ |
| 37 exports.getBlockedPerPage = page => blockedPerPage.get(page) || 0; | 41 exports.getBlockedPerPage = page => blockedPerPage.get(page) || 0; |
| 38 | 42 |
| 39 function updateBadge(page, blockedCount) | 43 let activeTabIds = new Set(); |
| 44 let activeTabIdByWindowId = new Map(); | |
| 45 | |
| 46 let badgeUpdateScheduled = false; | |
| 47 | |
| 48 function updateBadge(tabId = -1) | |
|
Sebastian Noack
2019/02/04 07:16:41
Nit: What would you think about not setting a defa
Manish Jethani
2019/02/04 07:36:14
Works for me, done.
| |
| 40 { | 49 { |
| 41 if (Prefs.show_statsinicon) | 50 if (!Prefs.show_statsinicon) |
| 51 return; | |
|
Sebastian Noack
2019/02/04 07:16:41
The only purpose of the check here seems to be avo
Manish Jethani
2019/02/04 07:36:14
updateBadge is also called directly, not necessari
| |
| 52 | |
| 53 for (let id of (tabId == -1 ? activeTabIds : [tabId])) | |
| 42 { | 54 { |
| 55 let page = new ext.Page({id}); | |
| 56 let blockedCount = blockedPerPage.get(page); | |
| 57 | |
| 43 page.browserAction.setBadge(blockedCount && { | 58 page.browserAction.setBadge(blockedCount && { |
| 44 color: badgeColor, | 59 color: badgeColor, |
| 45 number: blockedCount | 60 number: blockedCount |
| 46 }); | 61 }); |
| 47 } | 62 } |
| 48 } | 63 } |
| 49 | 64 |
| 65 function scheduleBadgeUpdate(tabId = -1) | |
| 66 { | |
| 67 if (!badgeUpdateScheduled && Prefs.show_statsinicon && | |
| 68 (tabId == -1 || activeTabIds.has(tabId))) | |
| 69 { | |
| 70 setTimeout(() => { badgeUpdateScheduled = false; updateBadge(); }, | |
| 71 1000 / badgeRefreshRate); | |
| 72 badgeUpdateScheduled = true; | |
| 73 } | |
| 74 } | |
| 75 | |
| 50 // Once nagivation for the tab has been committed to (e.g. it's no longer | 76 // Once nagivation for the tab has been committed to (e.g. it's no longer |
| 51 // being prerendered) we clear its badge, or if some requests were already | 77 // being prerendered) we clear its badge, or if some requests were already |
| 52 // blocked beforehand we display those on the badge now. | 78 // blocked beforehand we display those on the badge now. |
| 53 browser.webNavigation.onCommitted.addListener(details => | 79 browser.webNavigation.onCommitted.addListener(details => |
| 54 { | 80 { |
| 55 if (details.frameId == 0) | 81 if (details.frameId == 0) |
| 56 { | 82 updateBadge(details.tabId); |
| 57 let page = new ext.Page({id: details.tabId}); | |
| 58 let blocked = blockedPerPage.get(page); | |
| 59 | |
| 60 updateBadge(page, blocked); | |
| 61 } | |
| 62 }); | 83 }); |
| 63 | 84 |
| 64 filterNotifier.on("filter.hitCount", (filter, newValue, oldValue, tabIds) => | 85 filterNotifier.on("filter.hitCount", (filter, newValue, oldValue, tabIds) => |
| 65 { | 86 { |
| 66 if (!(filter instanceof BlockingFilter)) | 87 if (!(filter instanceof BlockingFilter)) |
| 67 return; | 88 return; |
| 68 | 89 |
| 69 for (let tabId of tabIds) | 90 for (let tabId of tabIds) |
| 70 { | 91 { |
| 71 let page = new ext.Page({id: tabId}); | 92 let page = new ext.Page({id: tabId}); |
| 72 let blocked = blockedPerPage.get(page) || 0; | 93 let blocked = blockedPerPage.get(page) || 0; |
| 73 | 94 |
| 74 blockedPerPage.set(page, ++blocked); | 95 blockedPerPage.set(page, ++blocked); |
| 75 updateBadge(page, blocked); | 96 scheduleBadgeUpdate(tabId); |
| 76 } | 97 } |
| 77 | 98 |
| 78 Prefs.blocked_total++; | 99 Prefs.blocked_total++; |
| 79 }); | 100 }); |
| 80 | 101 |
| 81 Prefs.on("show_statsinicon", () => | 102 Prefs.on("show_statsinicon", () => |
| 82 { | 103 { |
| 83 browser.tabs.query({}, tabs => | 104 browser.tabs.query({}, tabs => |
| 84 { | 105 { |
| 85 for (let tab of tabs) | 106 for (let tab of tabs) |
| 86 { | 107 { |
| 87 let page = new ext.Page(tab); | 108 let page = new ext.Page(tab); |
| 88 | 109 |
| 89 if (Prefs.show_statsinicon) | 110 if (Prefs.show_statsinicon) |
| 90 updateBadge(page, blockedPerPage.get(page)); | 111 updateBadge(tab.id); |
| 91 else | 112 else |
| 92 page.browserAction.setBadge(null); | 113 page.browserAction.setBadge(null); |
| 93 } | 114 } |
| 94 }); | 115 }); |
| 95 }); | 116 }); |
| 96 | 117 |
| 97 port.on("stats.getBlockedPerPage", | 118 port.on("stats.getBlockedPerPage", |
| 98 message => getBlockedPerPage(new ext.Page(message.tab))); | 119 message => getBlockedPerPage(new ext.Page(message.tab))); |
| 120 | |
| 121 browser.tabs.query({active: true}, tabs => | |
| 122 { | |
| 123 for (let tab of tabs) | |
| 124 { | |
| 125 activeTabIds.add(tab.id); | |
| 126 activeTabIdByWindowId.set(tab.windowId, tab.id); | |
| 127 } | |
| 128 | |
| 129 scheduleBadgeUpdate(); | |
| 130 }); | |
| 131 | |
| 132 browser.tabs.onActivated.addListener(tab => | |
| 133 { | |
| 134 let lastActiveTabId = activeTabIdByWindowId.get(tab.windowId); | |
| 135 if (typeof lastActiveTabId != "undefined") | |
| 136 activeTabIds.delete(lastActiveTabId); | |
| 137 | |
| 138 activeTabIds.add(tab.tabId); | |
| 139 activeTabIdByWindowId.set(tab.windowId, tab.tabId); | |
| 140 | |
| 141 scheduleBadgeUpdate(); | |
| 142 }); | |
| 143 | |
| 144 if ("windows" in browser) | |
| 145 { | |
| 146 browser.windows.onRemoved.addListener(windowId => | |
| 147 { | |
| 148 activeTabIds.delete(activeTabIdByWindowId.get(windowId)); | |
| 149 activeTabIdByWindowId.delete(windowId); | |
| 150 }); | |
| 151 } | |
| OLD | NEW |