From 9e2dd8108c1ae6b0d811677bc4e293403e930ac2 Mon Sep 17 00:00:00 2001 From: gorhill Date: Sun, 3 Dec 2017 14:56:08 -0500 Subject: [PATCH] code review: further simplifying; jshint-ing --- src/js/cookies.js | 18 ++++----- src/js/messaging.js | 4 +- src/js/pagestats.js | 91 ++++++++++++--------------------------------- src/js/popup.js | 23 ++++-------- src/js/start.js | 2 - src/js/tab.js | 2 - src/js/traffic.js | 12 ------ 7 files changed, 42 insertions(+), 110 deletions(-) diff --git a/src/js/cookies.js b/src/js/cookies.js index 9579d12..4762223 100644 --- a/src/js/cookies.js +++ b/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 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); diff --git a/src/js/messaging.js b/src/js/messaging.js index e4354e0..340ae0a 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -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); diff --git a/src/js/pagestats.js b/src/js/pagestats.js index 10b4fa9..f180449 100644 --- a/src/js/pagestats.js +++ b/src/js/pagestats.js @@ -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(); } diff --git a/src/js/popup.js b/src/js/popup.js index 88b01a7..e7f72c0 100644 --- a/src/js/popup.js +++ b/src/js/popup.js @@ -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)); } /******************************************************************************/ diff --git a/src/js/start.js b/src/js/start.js index 6cc77cf..25f5259 100644 --- a/src/js/start.js +++ b/src/js/start.js @@ -19,8 +19,6 @@ Home: https://github.com/gorhill/uMatrix */ -/* global µMatrix */ - // ORDER IS IMPORTANT /******************************************************************************/ diff --git a/src/js/tab.js b/src/js/tab.js index 0969c58..e6bc2e3 100644 --- a/src/js/tab.js +++ b/src/js/tab.js @@ -19,8 +19,6 @@ Home: https://github.com/gorhill/uMatrix */ -/* global chrome, µMatrix */ - /******************************************************************************/ /******************************************************************************/ diff --git a/src/js/traffic.js b/src/js/traffic.js index e8fb7e7..2a52c97 100644 --- a/src/js/traffic.js +++ b/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. var headerIndexFromName = function(headerName, headers) {