aboutsummaryrefslogtreecommitdiffstats
path: root/js/pagestats.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/pagestats.js')
-rw-r--r--js/pagestats.js186
1 files changed, 86 insertions, 100 deletions
diff --git a/js/pagestats.js b/js/pagestats.js
index 2851df6..b4c65c1 100644
--- a/js/pagestats.js
+++ b/js/pagestats.js
@@ -17,25 +17,19 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
- Home: https://libregit.spks.xyz/heckyel/ematrix
+ Home: https://gitlab.com/vannilla/ematrix
uMatrix Home: https://github.com/gorhill/uMatrix
*/
'use strict';
-/******************************************************************************/
-
ηMatrix.pageStoreFactory = (function() {
Cu.import('chrome://ematrix/content/lib/UriTools.jsm');
- /******************************************************************************/
-
- var ηm = ηMatrix;
+ let ηm = ηMatrix;
- /******************************************************************************/
-
- var BlockedCollapsibles = function() {
+ let BlockedCollapsibles = function () {
this.boundPruneAsyncCallback = this.pruneAsyncCallback.bind(this);
this.blocked = new Map();
this.hash = 0;
@@ -43,16 +37,18 @@
};
BlockedCollapsibles.prototype = {
-
shelfLife: 10 * 1000,
- add: function(type, url, isSpecific) {
- if ( this.blocked.size === 0 ) { this.pruneAsync(); }
- var 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 ) {
+ add: function (type, url, isSpecific) {
+ if (this.blocked.size === 0) {
+ 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;
} else {
now &= 0xFFFFFFFE;
@@ -60,45 +56,39 @@
this.blocked.set(type + ' ' + url, now);
this.hash = now;
},
-
- reset: function() {
+ reset: function () {
this.blocked.clear();
this.hash = 0;
- if ( this.timer !== null ) {
+ if (this.timer !== null) {
clearTimeout(this.timer);
this.timer = null;
}
},
-
- pruneAsync: function() {
- if ( this.timer === null ) {
- this.timer = vAPI.setTimeout(
- this.boundPruneAsyncCallback,
- this.shelfLife * 2
- );
+ pruneAsync: function () {
+ if (this.timer === null) {
+ this.timer = vAPI.setTimeout(this.boundPruneAsyncCallback,
+ this.shelfLife * 2);
}
},
-
- pruneAsyncCallback: function() {
+ pruneAsyncCallback: function () {
this.timer = null;
- var obsolete = Date.now() - this.shelfLife;
- for ( var entry of this.blocked ) {
- if ( entry[1] <= obsolete ) {
+ let obsolete = Date.now() - this.shelfLife;
+ for (let entry of this.blocked) {
+ if (entry[1] <= obsolete) {
this.blocked.delete(entry[0]);
}
}
- if ( this.blocked.size !== 0 ) { this.pruneAsync(); }
+ if (this.blocked.size !== 0) {
+ this.pruneAsync();
+ }
}
};
- /******************************************************************************/
-
// Ref: Given a URL, returns a (somewhat) unique 32-bit value
// Based on: FNV32a
// http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-reference-source
// The rest is custom, suited for uMatrix.
-
- var PageStore = function(tabContext) {
+ let PageStore = function (tabContext) {
this.hostnameTypeCells = new Map();
this.domains = new Set();
this.blockedCollapsibles = new BlockedCollapsibles();
@@ -108,11 +98,10 @@
};
PageStore.prototype = {
-
collapsibleTypes: new Set([ 'image' ]),
pageStoreJunkyard: [],
- init: function(tabContext) {
+ init: function (tabContext) {
this.tabId = tabContext.tabId;
this.rawUrl = tabContext.rawURL;
this.pageUrl = tabContext.normalURL;
@@ -136,8 +125,7 @@
this.mtxCountModifiedTime = 0;
return this;
},
-
- dispose: function() {
+ dispose: function () {
this.rawUrl = '';
this.pageUrl = '';
this.pageHostname = '';
@@ -147,68 +135,70 @@
this.domains.clear();
this.allHostnamesString = ' ';
this.blockedCollapsibles.reset();
- if ( this.incinerationTimer !== null ) {
+ if (this.incinerationTimer !== null) {
clearTimeout(this.incinerationTimer);
this.incinerationTimer = null;
}
- if ( this.pageStoreJunkyard.length < 8 ) {
+ if (this.pageStoreJunkyard.length < 8) {
this.pageStoreJunkyard.push(this);
}
},
-
- cacheBlockedCollapsible: function(type, url, specificity) {
- if ( this.collapsibleTypes.has(type) ) {
- this.blockedCollapsibles.add(
- type,
- url,
- specificity !== 0 && specificity < 5
- );
+ cacheBlockedCollapsible: function (type, url, specificity) {
+ if (this.collapsibleTypes.has(type)) {
+ this.blockedCollapsibles.add(type,
+ url,
+ specificity !== 0
+ && specificity < 5);
}
},
+ lookupBlockedCollapsibles: function (request, response) {
+ let tabContext = ηm.tabContextManager.lookup(this.tabId);
+ if (tabContext === null) {
+ return;
+ }
- lookupBlockedCollapsibles: function(request, response) {
- var tabContext = ηm.tabContextManager.lookup(this.tabId);
- if ( tabContext === null ) { return; }
-
- var collapseBlacklisted = ηm.userSettings.collapseBlacklisted,
- collapseBlocked = ηm.userSettings.collapseBlocked,
- entry;
+ let collapseBlacklisted = ηm.userSettings.collapseBlacklisted;
+ let collapseBlocked = ηm.userSettings.collapseBlocked;
+ let blockedResources = response.blockedResources;
- var blockedResources = response.blockedResources;
+ if (Array.isArray(request.toFilter) && request.toFilter.length !== 0) {
+ let roothn = tabContext.rootHostname;
+ let hnFromURI = UriTools.hostnameFromURI;
+ let tMatrix = ηm.tMatrix;
- if (
- Array.isArray(request.toFilter) &&
- request.toFilter.length !== 0
- ) {
- var roothn = tabContext.rootHostname,
- hnFromURI = UriTools.hostnameFromURI,
- tMatrix = ηm.tMatrix;
- for ( entry of request.toFilter ) {
- if ( tMatrix.mustBlock(roothn, hnFromURI(entry.url), entry.type) === false ) {
+ for (let entry of request.toFilter) {
+ if (tMatrix.mustBlock(roothn,
+ hnFromURI(entry.url),
+ entry.type) === false) {
continue;
}
+
blockedResources.push([
entry.type + ' ' + entry.url,
- collapseBlocked ||
- collapseBlacklisted && tMatrix.specificityRegister !== 0 &&
- tMatrix.specificityRegister < 5
+ collapseBlocked
+ || collapseBlacklisted
+ && tMatrix.specificityRegister !== 0
+ && tMatrix.specificityRegister < 5
]);
}
}
- if ( this.blockedCollapsibles.hash === response.hash ) { return; }
+ if (this.blockedCollapsibles.hash === response.hash) {
+ return;
+ }
response.hash = this.blockedCollapsibles.hash;
- for ( entry of this.blockedCollapsibles.blocked ) {
+ for (let entry of this.blockedCollapsibles.blocked) {
blockedResources.push([
entry[0],
- collapseBlocked || collapseBlacklisted && (entry[1] & 1) !== 0
+ collapseBlocked
+ || collapseBlacklisted
+ && (entry[1] & 1) !== 0
]);
}
},
-
- recordRequest: function(type, url, block) {
- if ( block !== false ) {
+ recordRequest: function (type, url, block) {
+ if (block !== false) {
this.perLoadBlockedRequestCount++;
} else {
this.perLoadAllowedRequestCount++;
@@ -218,16 +208,20 @@
// - remember which hostname/type were seen
// - count the number of distinct URLs for any given
// hostname-type pair
- var hostname = UriTools.hostnameFromURI(url),
- key = hostname + ' ' + type,
- uids = this.hostnameTypeCells.get(key);
- if ( uids === undefined ) {
+ let hostname = UriTools.hostnameFromURI(url);
+ let key = hostname + ' ' + type;
+ let uids = this.hostnameTypeCells.get(key);
+
+ if (uids === undefined) {
this.hostnameTypeCells.set(key, (uids = new Set()));
- } else if ( uids.size > 99 ) {
+ } else if (uids.size > 99) {
+ return;
+ }
+
+ let uid = this.uidFromURL(url);
+ if (uids.has(uid)) {
return;
}
- var uid = this.uidFromURL(url);
- if ( uids.has(uid) ) { return; }
uids.add(uid);
// Count blocked/allowed requests
@@ -241,17 +235,16 @@
// this.distinctRequestCount++;
this.mtxCountModifiedTime = Date.now();
- if ( this.domains.has(hostname) === false ) {
+ if (this.domains.has(hostname) === false) {
this.domains.add(hostname);
this.allHostnamesString += hostname + ' ';
this.mtxContentModifiedTime = Date.now();
}
},
-
- uidFromURL: function(uri) {
- var hint = 0x811c9dc5,
- i = uri.length;
- while ( i-- ) {
+ uidFromURL: function (uri) {
+ var hint = 0x811c9dc5;
+ 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;
@@ -260,18 +253,11 @@
}
};
- /******************************************************************************/
-
- return function pageStoreFactory(tabContext) {
- var entry = PageStore.prototype.pageStoreJunkyard.pop();
- if ( entry ) {
+ return function (tabContext) {
+ let entry = PageStore.prototype.pageStoreJunkyard.pop();
+ if (entry) {
return entry.init(tabContext);
}
return new PageStore(tabContext);
};
-
- /******************************************************************************/
-
})();
-
-/******************************************************************************/