From ebfe08e0a234a2776fd47099bc8e76e64a921f6e Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Thu, 1 Feb 2018 08:25:23 -0500 Subject: [PATCH] fix #938 --- platform/chromium/vapi-background.js | 40 +++++++++---------- src/_locales/en/messages.json | 2 +- src/js/messaging.js | 2 +- src/js/pagestats.js | 14 +++---- src/js/tab.js | 60 +++++++++++++++------------- src/js/traffic.js | 2 + 6 files changed, 63 insertions(+), 57 deletions(-) diff --git a/platform/chromium/vapi-background.js b/platform/chromium/vapi-background.js index 40d75c3..757eaea 100644 --- a/platform/chromium/vapi-background.js +++ b/platform/chromium/vapi-background.js @@ -1,8 +1,8 @@ /******************************************************************************* uMatrix - a browser extension to block requests. - Copyright (C) 2014-2017 The uBlock Origin authors - Copyright (C) 2017 Raymond Hill + Copyright (C) 2014-2018 The uBlock Origin authors + Copyright (C) 2017-2018 Raymond Hill This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -381,32 +381,32 @@ vAPI.tabs.injectScript = function(tabId, details, callback) { // Since we may be called asynchronously, the tab id may not exist // anymore, so this ensures it does still exist. -vAPI.setIcon = function(tabId, iconId, badge) { - tabId = parseInt(tabId, 10); - if ( isNaN(tabId) || tabId <= 0 ) { - return; - } - var onIconReady = function() { - if ( vAPI.lastError() ) { - return; +vAPI.setIcon = (function() { + let onIconReady = function(tabId, badgeDetails) { + if ( vAPI.lastError() ) { return; } + if ( badgeDetails.text !== undefined ) { + chrome.browserAction.setBadgeText({ + tabId: tabId, + text: badgeDetails.text + }); } - chrome.browserAction.setBadgeText({ tabId: tabId, text: badge }); - if ( badge !== '' ) { + if ( badgeDetails.color !== undefined ) { chrome.browserAction.setBadgeBackgroundColor({ tabId: tabId, - color: '#000' + color: badgeDetails.color }); } }; - var iconSelector = typeof iconId === 'number' ? iconId : 'off'; - var iconPaths = { - '19': 'img/browsericons/icon19-' + iconSelector + '.png'/* , - '38': 'img/browsericons/icon38-' + iconSelector + '.png' */ + return function(tabId, iconDetails, badgeDetails) { + tabId = parseInt(tabId, 10); + if ( isNaN(tabId) || tabId === -1 ) { return; } + chrome.browserAction.setIcon( + { tabId: tabId, path: iconDetails }, + function() { onIconReady(tabId, badgeDetails); } + ); }; - - chrome.browserAction.setIcon({ tabId: tabId, path: iconPaths }, onIconReady); -}; +})(); /******************************************************************************/ /******************************************************************************/ diff --git a/src/_locales/en/messages.json b/src/_locales/en/messages.json index c621379..867587b 100644 --- a/src/_locales/en/messages.json +++ b/src/_locales/en/messages.json @@ -291,7 +291,7 @@ "description": "" }, "settingsIconBadgeEnabled":{ - "message":"Show the number of distinct requests on the icon", + "message":"Show the number of blocked resources on the icon", "description":"" }, "settingsMatrixDisplayColorBlind" : { diff --git a/src/js/messaging.js b/src/js/messaging.js index 94a51b9..4ec891a 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -573,7 +573,7 @@ var onMessage = function(request, sender, callback) { request.documentURI; if ( pageStore !== null ) { pageStore.hasWebWorkers = true; - pageStore.recordRequest('script', url, true); + pageStore.recordRequest('script', url, request.blocked); } if ( tabContext !== null ) { µm.logger.writeOne(tabId, 'net', rootHostname, url, 'worker', request.blocked); diff --git a/src/js/pagestats.js b/src/js/pagestats.js index 9f0baa5..81bc528 100644 --- a/src/js/pagestats.js +++ b/src/js/pagestats.js @@ -1,7 +1,7 @@ /******************************************************************************* uMatrix - a Chromium browser extension to black/white list requests. - Copyright (C) 2013-2017 Raymond Hill + Copyright (C) 2013-2018 Raymond Hill This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -204,6 +204,12 @@ PageStore.prototype = { }, recordRequest: function(type, url, block) { + if ( block ) { + this.perLoadBlockedRequestCount++; + } else { + this.perLoadAllowedRequestCount++; + } + // Store distinct network requests. This is used to: // - remember which hostname/type were seen // - count the number of distinct URLs for any given @@ -228,12 +234,6 @@ PageStore.prototype = { µm.requestStats.record(type, block); µm.updateBadgeAsync(this.tabId); - if ( block !== false ) { - this.perLoadBlockedRequestCount++; - } else { - this.perLoadAllowedRequestCount++; - } - this.distinctRequestCount++; this.mtxCountModifiedTime = Date.now(); diff --git a/src/js/tab.js b/src/js/tab.js index ed96b1f..48dac51 100644 --- a/src/js/tab.js +++ b/src/js/tab.js @@ -1,7 +1,7 @@ /******************************************************************************* - µMatrix - a Chromium browser extension to black/white list requests. - Copyright (C) 2014-2016 Raymond Hill + uMatrix - a Chromium browser extension to black/white list requests. + Copyright (C) 2014-2018 Raymond Hill This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -558,46 +558,50 @@ vAPI.tabs.registerListeners(); /******************************************************************************/ -// Update badge - -// rhill 2013-11-09: well this sucks, I can't update icon/badge -// incrementally, as chromium overwrite the icon at some point without -// notifying me, and this causes internal cached state to be out of sync. - µm.updateBadgeAsync = (function() { - var tabIdToTimer = Object.create(null); + let tabIdToTimer = new Map(); - var updateBadge = function(tabId) { - delete tabIdToTimer[tabId]; + let updateBadge = function(tabId) { + tabIdToTimer.delete(tabId); - var iconId = null; - var badgeStr = ''; + let iconId = 'off'; + let badgeStr = ''; - var pageStore = this.pageStoreFromTabId(tabId); + let pageStore = this.pageStoreFromTabId(tabId); if ( pageStore !== null ) { - var total = pageStore.perLoadAllowedRequestCount + + let total = pageStore.perLoadAllowedRequestCount + pageStore.perLoadBlockedRequestCount; if ( total ) { - var squareSize = 19; - var greenSize = squareSize * Math.sqrt(pageStore.perLoadAllowedRequestCount / total); - iconId = greenSize < squareSize/2 ? Math.ceil(greenSize) : Math.floor(greenSize); + let squareSize = 19; + let greenSize = squareSize * Math.sqrt( + pageStore.perLoadAllowedRequestCount / total + ); + iconId = greenSize < squareSize/2 ? + Math.ceil(greenSize) : + Math.floor(greenSize); } - if ( this.userSettings.iconBadgeEnabled && pageStore.distinctRequestCount !== 0) { - badgeStr = this.formatCount(pageStore.distinctRequestCount); + if ( + this.userSettings.iconBadgeEnabled && + pageStore.perLoadBlockedRequestCount !== 0 + ) { + badgeStr = this.formatCount(pageStore.perLoadBlockedRequestCount); } } - vAPI.setIcon(tabId, iconId, badgeStr); + vAPI.setIcon( + tabId, + 'img/browsericons/icon19-' + iconId + '.png', + { text: badgeStr, color: '#666' } + ); }; return function(tabId) { - if ( tabIdToTimer[tabId] ) { - return; - } - if ( vAPI.isBehindTheSceneTabId(tabId) ) { - return; - } - tabIdToTimer[tabId] = vAPI.setTimeout(updateBadge.bind(this, tabId), 750); + if ( tabIdToTimer.has(tabId) ) { return; } + if ( vAPI.isBehindTheSceneTabId(tabId) ) { return; } + tabIdToTimer.set( + tabId, + vAPI.setTimeout(updateBadge.bind(this, tabId), 750) + ); }; })(); diff --git a/src/js/traffic.js b/src/js/traffic.js index 3c2b6ee..6429449 100644 --- a/src/js/traffic.js +++ b/src/js/traffic.js @@ -47,6 +47,8 @@ var onBeforeRootFrameRequestHandler = function(details) { var pageStore = µm.pageStoreFromTabId(tabId); pageStore.recordRequest('doc', requestURL, block); + pageStore.perLoadAllowedRequestCount = 0; + pageStore.perLoadBlockedRequestCount = 0; µm.logger.writeOne(tabId, 'net', rootHostname, requestURL, 'doc', block); // Not blocked