Browse Source

code review: further simplifying; jshint-ing

pull/2/head
gorhill 7 years ago
parent
commit
9e2dd8108c
No known key found for this signature in database GPG Key ID: 25E1490B761470C2
  1. 18
      src/js/cookies.js
  2. 4
      src/js/messaging.js
  3. 91
      src/js/pagestats.js
  4. 13
      src/js/popup.js
  5. 2
      src/js/start.js
  6. 2
      src/js/tab.js
  7. 12
      src/js/traffic.js

18
src/js/cookies.js

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

4
src/js/messaging.js

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

91
src/js/pagestats.js

@ -19,31 +19,19 @@
Home: https://github.com/gorhill/uMatrix Home: https://github.com/gorhill/uMatrix
*/ */
/* jshint bitwise: false, boss: true */
'use strict'; '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 µm = µMatrix;
var µmuri;
var pageRequestStoreJunkyard = [];
var pageStoreJunkyard = [];
// Ref: Given a URL, returns a (somewhat) unique 32-bit value // Ref: Given a URL, returns a (somewhat) unique 32-bit value
// Based on: FNV32a // Based on: FNV32a
// http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-reference-source // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-reference-source
// The rest is custom, suited for µMatrix. // The rest is custom, suited for µMatrix.
var uidFromURL = function(uri) { var uidFromURL = function(uri) {
var hint = 0x811c9dc5; var hint = 0x811c9dc5;
var i = uri.length; var i = uri.length;
@ -55,50 +43,6 @@
return hint; 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) { function PageStore(tabContext) {
this.requestStats = µm.requestStatsFactory(); this.requestStats = µm.requestStatsFactory();
this.off = false; this.off = false;
@ -113,8 +57,8 @@
this.pageHostname = tabContext.rootHostname; this.pageHostname = tabContext.rootHostname;
this.pageDomain = tabContext.rootDomain; this.pageDomain = tabContext.rootDomain;
this.title = ''; this.title = '';
this.requests = µm.pageRequestStatsFactory();
this.domains = {};
this.hostnameTypeCells = new Map();
this.domains = new Set();
this.allHostnamesString = ' '; this.allHostnamesString = ' ';
this.requestStats.reset(); this.requestStats.reset();
this.distinctRequestCount = 0; this.distinctRequestCount = 0;
@ -126,13 +70,13 @@
return this; return this;
}, },
dispose: function() { dispose: function() {
this.requests.dispose();
this.hostnameTypeCells.clear();
this.rawUrl = ''; this.rawUrl = '';
this.pageUrl = ''; this.pageUrl = '';
this.pageHostname = ''; this.pageHostname = '';
this.pageDomain = ''; this.pageDomain = '';
this.title = ''; this.title = '';
this.domains = {};
this.domains.clear();
this.allHostnamesString = ' '; this.allHostnamesString = ' ';
if ( this.incinerationTimer !== null ) { if ( this.incinerationTimer !== null ) {
clearTimeout(this.incinerationTimer); clearTimeout(this.incinerationTimer);
@ -143,9 +87,22 @@
} }
}, },
recordRequest: function(type, url, block) { 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; return;
} }
var uid = uidFromURL(url);
if ( uids.has(uid) ) { return; }
uids.add(uid);
// Count blocked/allowed requests // Count blocked/allowed requests
this.requestStats.record(type, block); this.requestStats.record(type, block);
@ -161,13 +118,11 @@
this.perLoadAllowedRequestCount++; this.perLoadAllowedRequestCount++;
} }
var hostname = µm.URI.hostnameFromURI(url);
this.distinctRequestCount++; this.distinctRequestCount++;
this.mtxCountModifiedTime = Date.now(); 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.allHostnamesString += hostname + ' ';
this.mtxContentModifiedTime = Date.now(); this.mtxContentModifiedTime = Date.now();
} }

13
src/js/popup.js

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

2
src/js/start.js

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

2
src/js/tab.js

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

12
src/js/traffic.js

@ -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. // Caller must ensure headerName is normalized to lower case.
var headerIndexFromName = function(headerName, headers) { var headerIndexFromName = function(headerName, headers) {

Loading…
Cancel
Save