1
0
Fork 0
mirror of https://github.com/gorhill/uMatrix.git synced 2024-06-17 09:44:59 +12:00

code review: further simplifying; jshint-ing

This commit is contained in:
gorhill 2017-12-03 14:56:08 -05:00
parent 6e15dba281
commit 9e2dd8108c
No known key found for this signature in database
GPG key ID: 25E1490B761470C2
7 changed files with 42 additions and 110 deletions

View file

@ -1,7 +1,7 @@
/*******************************************************************************
µMatrix - a Chromium browser extension to black/white list requests.
Copyright (C) 2013-2106 Raymond Hill
uMatrix - a Chromium browser extension to black/white list requests.
Copyright (C) 2013-2017 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
@ -26,6 +26,8 @@
// to record cookie for a web page *only* when its value changes.
// https://github.com/gorhill/httpswitchboard/issues/79
"use strict";
/******************************************************************************/
// Isolate from global namespace
@ -35,8 +37,6 @@
µMatrix.cookieHunter = (function() {
"use strict";
/******************************************************************************/
var µm = µMatrix;
@ -424,26 +424,26 @@ var processClean = function() {
/******************************************************************************/
var findAndRecordPageCookies = function(pageStats) {
var findAndRecordPageCookies = function(pageStore) {
for ( var cookieKey in cookieDict ) {
if ( !cookieDict.hasOwnProperty(cookieKey) ) {
continue;
}
if ( cookieMatchDomains(cookieKey, pageStats.allHostnamesString) === false ) {
if ( cookieMatchDomains(cookieKey, pageStore.allHostnamesString) === false ) {
continue;
}
recordPageCookie(pageStats, cookieKey);
recordPageCookie(pageStore, cookieKey);
}
};
/******************************************************************************/
var findAndRemovePageCookies = function(pageStats) {
var findAndRemovePageCookies = function(pageStore) {
for ( var cookieKey in cookieDict ) {
if ( !cookieDict.hasOwnProperty(cookieKey) ) {
continue;
}
if ( !cookieMatchDomains(cookieKey, pageStats.allHostnamesString) ) {
if ( !cookieMatchDomains(cookieKey, pageStore.allHostnamesString) ) {
continue;
}
removeCookieAsync(cookieKey);

View file

@ -207,13 +207,13 @@ var matrixSnapshot = function(pageStore, details) {
r.rowCount += 1;
var µmuri = µm.URI;
var reqKey, reqType, reqHostname, reqDomain;
var reqType, reqHostname, reqDomain;
var desHostname;
var row, typeIndex;
var anyIndex = headerIndices.get('*');
var pos, count;
for ( var entry of pageStore.requests.hostnameTypeCells ) {
for ( var entry of pageStore.hostnameTypeCells ) {
pos = entry[0].indexOf(' ');
reqHostname = entry[0].slice(0, pos);
reqType = entry[0].slice(pos + 1);

View file

@ -19,31 +19,19 @@
Home: https://github.com/gorhill/uMatrix
*/
/* jshint bitwise: false, boss: true */
'use strict';
/*******************************************************************************
/******************************************************************************/
A PageRequestStats object is used to store distinct network requests.
This is used to:
- remember which hostname/type were seen
- count the number of distinct URLs for any given hostname-type pair
**/
µMatrix.pageRequestStatsFactory = (function() {
µMatrix.pageStoreFactory = (function() {
var µm = µMatrix;
var µmuri;
var pageRequestStoreJunkyard = [];
var pageStoreJunkyard = [];
// 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 µMatrix.
var uidFromURL = function(uri) {
var hint = 0x811c9dc5;
var i = uri.length;
@ -55,50 +43,6 @@
return hint;
};
var PageRequestStats = function() {
this.hostnameTypeCells = new Map();
};
PageRequestStats.prototype = {
dispose: function() {
this.hostnameTypeCells.clear();
if ( pageRequestStoreJunkyard.length < 8 ) {
pageRequestStoreJunkyard.push(this);
}
},
createEntryIfNotExists: function(url, type) {
var hn = µmuri.hostnameFromURI(url),
key = hn + ' ' + type,
uids = this.hostnameTypeCells.get(key);
if ( uids === undefined ) {
this.hostnameTypeCells.set(key, (uids = new Set()));
} else {
if ( uids.size > 99 ) { return false; }
}
var uid = uidFromURL(url);
if ( uids.has(uid) ) { return false; }
uids.add(uid);
return true;
}
};
return function pageRequestStatsFactory() {
if ( pageRequestStoreJunkyard.length !== 0 ) {
return pageRequestStoreJunkyard.pop();
}
if ( µmuri === undefined ) { µmuri = µm.URI; }
return new PageRequestStats();
};
})();
/******************************************************************************/
/******************************************************************************/
µMatrix.pageStoreFactory = (function() {
var µm = µMatrix;
var pageStoreJunkyard = [];
function PageStore(tabContext) {
this.requestStats = µm.requestStatsFactory();
this.off = false;
@ -113,8 +57,8 @@
this.pageHostname = tabContext.rootHostname;
this.pageDomain = tabContext.rootDomain;
this.title = '';
this.requests = µm.pageRequestStatsFactory();
this.domains = {};
this.hostnameTypeCells = new Map();
this.domains = new Set();
this.allHostnamesString = ' ';
this.requestStats.reset();
this.distinctRequestCount = 0;
@ -126,13 +70,13 @@
return this;
},
dispose: function() {
this.requests.dispose();
this.hostnameTypeCells.clear();
this.rawUrl = '';
this.pageUrl = '';
this.pageHostname = '';
this.pageDomain = '';
this.title = '';
this.domains = {};
this.domains.clear();
this.allHostnamesString = ' ';
if ( this.incinerationTimer !== null ) {
clearTimeout(this.incinerationTimer);
@ -143,9 +87,22 @@
}
},
recordRequest: function(type, url, block) {
if ( this.requests.createEntryIfNotExists(url, type) === false ) {
var hostname = µm.URI.hostnameFromURI(url);
// Store distinct network requests. This is used to:
// - remember which hostname/type were seen
// - count the number of distinct URLs for any given
// hostname-type pair
var key = hostname + ' ' + type,
uids = this.hostnameTypeCells.get(key);
if ( uids === undefined ) {
this.hostnameTypeCells.set(key, (uids = new Set()));
} else if ( uids.size > 99 ) {
return;
}
var uid = uidFromURL(url);
if ( uids.has(uid) ) { return; }
uids.add(uid);
// Count blocked/allowed requests
this.requestStats.record(type, block);
@ -161,13 +118,11 @@
this.perLoadAllowedRequestCount++;
}
var hostname = µm.URI.hostnameFromURI(url);
this.distinctRequestCount++;
this.mtxCountModifiedTime = Date.now();
if ( this.domains.hasOwnProperty(hostname) === false ) {
this.domains[hostname] = true;
if ( this.domains.has(hostname) === false ) {
this.domains.add(hostname);
this.allHostnamesString += hostname + ' ';
this.mtxContentModifiedTime = Date.now();
}

View file

@ -91,18 +91,11 @@ var resizePopup = (function() {
/******************************************************************************/
// Must be consistent with definitions in matrix.js
var Pale = 0x00;
var Dark = 0x80;
var Transparent = 0;
var Red = 1;
var Green = 2;
var Gray = 3;
var DarkRed = Dark | Red;
var PaleRed = Pale | Red;
var DarkGreen = Dark | Green;
var PaleGreen = Pale | Green;
var DarkGray = Dark | Gray;
var PaleGray = Pale | Gray;
var Dark = 0x80;
var Red = 1;
var Green = 2;
var DarkRed = Dark | Red;
var DarkGreen = Dark | Green;
var matrixSnapshot = {};
var groupsSnapshot = [];
@ -351,9 +344,9 @@ function getPermanentColor(hostname, type) {
function addCellClass(cell, hostname, type) {
var cl = cell.classList;
cell.classList.add('matCell');
cell.classList.add('t' + getTemporaryColor(hostname, type).toString(16));
cell.classList.add('p' + getPermanentColor(hostname, type).toString(16));
cl.add('matCell');
cl.add('t' + getTemporaryColor(hostname, type).toString(16));
cl.add('p' + getPermanentColor(hostname, type).toString(16));
}
/******************************************************************************/

View file

@ -19,8 +19,6 @@
Home: https://github.com/gorhill/uMatrix
*/
/* global µMatrix */
// ORDER IS IMPORTANT
/******************************************************************************/

View file

@ -19,8 +19,6 @@
Home: https://github.com/gorhill/uMatrix
*/
/* global chrome, µMatrix */
/******************************************************************************/
/******************************************************************************/

View file

@ -340,18 +340,6 @@ var onHeadersReceived = function(details) {
/******************************************************************************/
var headerValue = function(headers, name) {
var i = headers.length;
while ( i-- ) {
if ( headers[i].name.toLowerCase() === name ) {
return headers[i].value.trim();
}
}
return '';
};
/******************************************************************************/
// Caller must ensure headerName is normalized to lower case.
var headerIndexFromName = function(headerName, headers) {