diff options
Diffstat (limited to 'js/pagestats.js')
-rw-r--r-- | js/pagestats.js | 186 |
1 files changed, 93 insertions, 93 deletions
diff --git a/js/pagestats.js b/js/pagestats.js index b4c65c1..fd1757d 100644 --- a/js/pagestats.js +++ b/js/pagestats.js @@ -2,7 +2,7 @@ ηMatrix - a browser extension to black/white list requests. Copyright (C) 2013-2019 Raymond Hill - Copyright (C) 2019-2020 Alessio Vanni + Copyright (C) 2019-2022 Alessio Vanni 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 @@ -30,58 +30,58 @@ let ηm = ηMatrix; let BlockedCollapsibles = function () { - this.boundPruneAsyncCallback = this.pruneAsyncCallback.bind(this); - this.blocked = new Map(); - this.hash = 0; - this.timer = null; + this.boundPruneAsyncCallback = this.pruneAsyncCallback.bind(this); + this.blocked = new Map(); + this.hash = 0; + this.timer = null; }; BlockedCollapsibles.prototype = { - shelfLife: 10 * 1000, + shelfLife: 10 * 1000, - add: function (type, url, isSpecific) { + add: function (type, url, isSpecific) { if (this.blocked.size === 0) { - this.pruneAsync(); - } + this.pruneAsync(); + } let now = Date.now() / 1000 | 0; // The following "trick" is to encode the specifity into // the lsb of the time stamp so as to avoid to have to // allocate a memory structure to store both time stamp // and specificity. if (isSpecific) { - now |= 0x00000001; + now |= 0x00000001; } else { - now &= 0xFFFFFFFE; + now &= 0xFFFFFFFE; } this.blocked.set(type + ' ' + url, now); this.hash = now; - }, - reset: function () { + }, + reset: function () { this.blocked.clear(); this.hash = 0; if (this.timer !== null) { - clearTimeout(this.timer); - this.timer = null; + clearTimeout(this.timer); + this.timer = null; } - }, - pruneAsync: function () { + }, + pruneAsync: function () { if (this.timer === null) { - this.timer = vAPI.setTimeout(this.boundPruneAsyncCallback, - this.shelfLife * 2); + this.timer = vAPI.setTimeout(this.boundPruneAsyncCallback, + this.shelfLife * 2); } - }, - pruneAsyncCallback: function () { + }, + pruneAsyncCallback: function () { this.timer = null; let obsolete = Date.now() - this.shelfLife; for (let entry of this.blocked) { - if (entry[1] <= obsolete) { + if (entry[1] <= obsolete) { this.blocked.delete(entry[0]); - } + } } if (this.blocked.size !== 0) { - this.pruneAsync(); - } - } + this.pruneAsync(); + } + } }; // Ref: Given a URL, returns a (somewhat) unique 32-bit value @@ -89,19 +89,19 @@ // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-reference-source // The rest is custom, suited for uMatrix. let PageStore = function (tabContext) { - this.hostnameTypeCells = new Map(); - this.domains = new Set(); - this.blockedCollapsibles = new BlockedCollapsibles(); - this.requestStats = ηm.requestStatsFactory(); - this.off = false; - this.init(tabContext); + this.hostnameTypeCells = new Map(); + this.domains = new Set(); + this.blockedCollapsibles = new BlockedCollapsibles(); + this.requestStats = ηm.requestStatsFactory(); + this.off = false; + this.init(tabContext); }; PageStore.prototype = { - collapsibleTypes: new Set([ 'image' ]), - pageStoreJunkyard: [], + collapsibleTypes: new Set([ 'image' ]), + pageStoreJunkyard: [], - init: function (tabContext) { + init: function (tabContext) { this.tabId = tabContext.tabId; this.rawUrl = tabContext.rawURL; this.pageUrl = tabContext.normalURL; @@ -124,8 +124,8 @@ this.mtxContentModifiedTime = 0; this.mtxCountModifiedTime = 0; return this; - }, - dispose: function () { + }, + dispose: function () { this.rawUrl = ''; this.pageUrl = ''; this.pageHostname = ''; @@ -136,72 +136,72 @@ this.allHostnamesString = ' '; this.blockedCollapsibles.reset(); if (this.incinerationTimer !== null) { - clearTimeout(this.incinerationTimer); - this.incinerationTimer = null; + clearTimeout(this.incinerationTimer); + this.incinerationTimer = null; } if (this.pageStoreJunkyard.length < 8) { - this.pageStoreJunkyard.push(this); + this.pageStoreJunkyard.push(this); } - }, - cacheBlockedCollapsible: function (type, url, specificity) { + }, + cacheBlockedCollapsible: function (type, url, specificity) { if (this.collapsibleTypes.has(type)) { - this.blockedCollapsibles.add(type, - url, - specificity !== 0 - && specificity < 5); + this.blockedCollapsibles.add(type, + url, + specificity !== 0 + && specificity < 5); } - }, - lookupBlockedCollapsibles: function (request, response) { + }, + lookupBlockedCollapsibles: function (request, response) { let tabContext = ηm.tabContextManager.lookup(this.tabId); if (tabContext === null) { - return; - } + return; + } let collapseBlacklisted = ηm.userSettings.collapseBlacklisted; - let collapseBlocked = ηm.userSettings.collapseBlocked; + let collapseBlocked = ηm.userSettings.collapseBlocked; let blockedResources = response.blockedResources; if (Array.isArray(request.toFilter) && request.toFilter.length !== 0) { - let roothn = tabContext.rootHostname; + let roothn = tabContext.rootHostname; let hnFromURI = UriTools.hostnameFromURI; let tMatrix = ηm.tMatrix; - for (let entry of request.toFilter) { + for (let entry of request.toFilter) { if (tMatrix.mustBlock(roothn, - hnFromURI(entry.url), - entry.type) === false) { - continue; + hnFromURI(entry.url), + entry.type) === false) { + continue; } blockedResources.push([ - entry.type + ' ' + entry.url, - collapseBlocked - || collapseBlacklisted - && tMatrix.specificityRegister !== 0 - && tMatrix.specificityRegister < 5 + entry.type + ' ' + entry.url, + collapseBlocked + || collapseBlacklisted + && tMatrix.specificityRegister !== 0 + && tMatrix.specificityRegister < 5 ]); - } + } } if (this.blockedCollapsibles.hash === response.hash) { - return; - } + return; + } response.hash = this.blockedCollapsibles.hash; for (let entry of this.blockedCollapsibles.blocked) { - blockedResources.push([ + blockedResources.push([ entry[0], collapseBlocked - || collapseBlacklisted - && (entry[1] & 1) !== 0 - ]); + || collapseBlacklisted + && (entry[1] & 1) !== 0 + ]); } - }, - recordRequest: function (type, url, block) { - if (block !== false) { - this.perLoadBlockedRequestCount++; + }, + recordRequest: function (type, url, block) { + if (block !== false) { + this.perLoadBlockedRequestCount++; } else { - this.perLoadAllowedRequestCount++; + this.perLoadAllowedRequestCount++; } // Store distinct network requests. This is used to: @@ -209,19 +209,19 @@ // - count the number of distinct URLs for any given // hostname-type pair let hostname = UriTools.hostnameFromURI(url); - let key = hostname + ' ' + type; - let uids = this.hostnameTypeCells.get(key); + let key = hostname + ' ' + type; + let uids = this.hostnameTypeCells.get(key); if (uids === undefined) { - this.hostnameTypeCells.set(key, (uids = new Set())); + this.hostnameTypeCells.set(key, (uids = new Set())); } else if (uids.size > 99) { - return; + return; } let uid = this.uidFromURL(url); if (uids.has(uid)) { - return; - } + return; + } uids.add(uid); // Count blocked/allowed requests @@ -236,28 +236,28 @@ this.mtxCountModifiedTime = Date.now(); if (this.domains.has(hostname) === false) { - this.domains.add(hostname); - this.allHostnamesString += hostname + ' '; - this.mtxContentModifiedTime = Date.now(); + this.domains.add(hostname); + this.allHostnamesString += hostname + ' '; + this.mtxContentModifiedTime = Date.now(); } - }, - uidFromURL: function (uri) { + }, + uidFromURL: function (uri) { var hint = 0x811c9dc5; - let i = uri.length; + let i = uri.length; while (i--) { - hint ^= uri.charCodeAt(i) | 0; - hint += (hint<<1) + (hint<<4) + (hint<<7) + (hint<<8) + (hint<<24) | 0; - hint >>>= 0; + hint ^= uri.charCodeAt(i) | 0; + hint += (hint<<1) + (hint<<4) + (hint<<7) + (hint<<8) + (hint<<24) | 0; + hint >>>= 0; } return hint; - } + } }; return function (tabContext) { - let entry = PageStore.prototype.pageStoreJunkyard.pop(); - if (entry) { + let entry = PageStore.prototype.pageStoreJunkyard.pop(); + if (entry) { return entry.init(tabContext); - } - return new PageStore(tabContext); + } + return new PageStore(tabContext); }; })(); |