From 8374799c7de293fd9c6cb3bc14c7bddd279f5260 Mon Sep 17 00:00:00 2001 From: gorhill Date: Thu, 30 Jun 2016 22:03:29 -0400 Subject: [PATCH] various minor code review --- src/js/contentscript.js | 49 +++++++++++++++++++++++------------------ src/js/messaging.js | 2 ++ src/js/pagestore.js | 10 ++++----- src/js/tab.js | 11 +++++++++ src/js/traffic.js | 14 +++++++++--- 5 files changed, 57 insertions(+), 29 deletions(-) diff --git a/src/js/contentscript.js b/src/js/contentscript.js index 154cde405..48037fc7d 100644 --- a/src/js/contentscript.js +++ b/src/js/contentscript.js @@ -923,15 +923,15 @@ vAPI.executionCost.start(); // Extract and return the staged nodes which (may) match the selectors. var selectNodes = function(selector) { - var targetNodes = []; - var i = contextNodes.length; - var node, nodeList, j; - var doc = document; + var stagedNodes = contextNodes, + i = stagedNodes.length; + if ( i === 1 && stagedNodes[0] === document.documentElement ) { + return document.querySelectorAll(selector); + } + var targetNodes = [], + node, nodeList, j; while ( i-- ) { - node = contextNodes[i]; - if ( node === doc ) { - return doc.querySelectorAll(selector); - } + node = stagedNodes[i]; targetNodes.push(node); nodeList = node.querySelectorAll(selector); j = nodeList.length; @@ -988,29 +988,31 @@ vAPI.executionCost.start(); // - [href^="http"] var processHighMediumGenerics = function(generics) { - var doc = document; - var i = contextNodes.length; - var aa = [ null ]; - var node, nodes; + var stagedNodes = contextNodes, + i = stagedNodes.length; + if ( i === 1 && stagedNodes[0] === document.documentElement ) { + processHighMediumGenericsForNodes(document.links, generics); + return; + } + var aa = [ null ], + node, nodes; while ( i-- ) { - node = contextNodes[i]; + node = stagedNodes[i]; if ( node.localName === 'a' ) { aa[0] = node; processHighMediumGenericsForNodes(aa, generics); } nodes = node.getElementsByTagName('a'); - if ( nodes.length === 0 ) { continue; } - processHighMediumGenericsForNodes(nodes, generics); - if ( node === doc ) { - break; + if ( nodes.length !== 0 ) { + processHighMediumGenericsForNodes(nodes, generics); } } }; var processHighMediumGenericsForNodes = function(nodes, generics) { - var i = nodes.length; - var node, href, pos, hash, selectors, j, selector; - var aa = [ '' ]; + var i = nodes.length, + node, href, pos, hash, selectors, j, selector, + aa = [ '' ]; while ( i-- ) { node = nodes[i]; href = node.getAttribute('href'); @@ -1139,7 +1141,12 @@ vAPI.executionCost.start(); // Start cosmetic filtering. - classesAndIdsFromNodeList(document.querySelectorAll('[class],[id]')); + var allElems = document.all; + classesAndIdsFromNodeList( + allElems instanceof Object ? + allElems : + document.querySelectorAll('[class],[id]') + ); retrieveGenericSelectors(); //console.debug('%f: uBlock: survey time', timer.now() - tStart); diff --git a/src/js/messaging.js b/src/js/messaging.js index 492936fb0..360c17b7a 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -503,6 +503,8 @@ var filterRequests = function(pageStore, details) { } request.collapse = true; } + + context.dispose(); return requests; }; diff --git a/src/js/pagestore.js b/src/js/pagestore.js index d0ed1d87c..fbfeecdb9 100644 --- a/src/js/pagestore.js +++ b/src/js/pagestore.js @@ -19,6 +19,8 @@ Home: https://github.com/gorhill/uBlock */ +'use strict'; + /******************************************************************************* A PageRequestStore object is used to store net requests in two ways: @@ -33,8 +35,6 @@ To create a log of net requests µBlock.PageStore = (function() { -'use strict'; - /******************************************************************************/ var µb = µBlock; @@ -428,14 +428,14 @@ PageStore.prototype.setFrame = function(frameId, frameURL) { /******************************************************************************/ PageStore.prototype.createContextFromPage = function() { - var context = new µb.tabContextManager.createContext(this.tabId); + var context = µb.tabContextManager.createContext(this.tabId); context.pageHostname = context.rootHostname; context.pageDomain = context.rootDomain; return context; }; PageStore.prototype.createContextFromFrameId = function(frameId) { - var context = new µb.tabContextManager.createContext(this.tabId); + var context = µb.tabContextManager.createContext(this.tabId); var frameStore = this.frames[frameId]; if ( frameStore ) { context.pageHostname = frameStore.pageHostname; @@ -448,7 +448,7 @@ PageStore.prototype.createContextFromFrameId = function(frameId) { }; PageStore.prototype.createContextFromFrameHostname = function(frameHostname) { - var context = new µb.tabContextManager.createContext(this.tabId); + var context = µb.tabContextManager.createContext(this.tabId); context.pageHostname = frameHostname; context.pageDomain = µb.URI.domainFromHostname(frameHostname) || frameHostname; return context; diff --git a/src/js/tab.js b/src/js/tab.js index 51dc61d23..21773044d 100644 --- a/src/js/tab.js +++ b/src/js/tab.js @@ -405,7 +405,11 @@ housekeep itself. })(); // Context object, typically to be used to feed filtering engines. + var contextJunkyard = []; var Context = function(tabId) { + this.init(tabId); + }; + Context.prototype.init = function(tabId) { var tabContext = lookup(tabId); this.rootHostname = tabContext.rootHostname; this.rootDomain = tabContext.rootDomain; @@ -414,9 +418,16 @@ housekeep itself. this.requestURL = this.requestHostname = this.requestDomain = ''; + return this; + }; + Context.prototype.dispose = function() { + contextJunkyard.push(this); }; var createContext = function(tabId) { + if ( contextJunkyard.length ) { + return contextJunkyard.pop().init(tabId); + } return new Context(tabId); }; diff --git a/src/js/traffic.js b/src/js/traffic.js index bda7615d7..b57e3aa75 100644 --- a/src/js/traffic.js +++ b/src/js/traffic.js @@ -19,7 +19,7 @@ Home: https://github.com/gorhill/uBlock */ -/* global µBlock, vAPI */ +'use strict'; /******************************************************************************/ @@ -27,8 +27,6 @@ µBlock.webRequest = (function() { -'use strict'; - /******************************************************************************/ var exports = {}; @@ -115,6 +113,7 @@ var onBeforeRequest = function(details) { if ( frameId > 0 && isFrame ) { pageStore.setFrame(frameId, requestURL); } + requestContext.dispose(); return; } @@ -141,9 +140,11 @@ var onBeforeRequest = function(details) { requestContext.pageHostname ); } + requestContext.dispose(); return { redirectUrl: url }; } + requestContext.dispose(); return { cancel: true }; }; @@ -306,6 +307,7 @@ var onBeforeBeacon = function(details) { context.rootHostname ); } + context.dispose(); if ( result !== '' ) { return { cancel: true }; } @@ -351,6 +353,8 @@ var onBeforeBehindTheSceneRequest = function(details) { ); } + context.dispose(); + // Not blocked if ( µb.isAllowResult(result) ) { return; @@ -425,6 +429,8 @@ var onRootFrameHeadersReceived = function(details) { ); } + context.dispose(); + // Don't block if ( µb.isAllowResult(result) ) { return; @@ -471,6 +477,8 @@ var onFrameHeadersReceived = function(details) { ); } + context.dispose(); + // Don't block if ( µb.isAllowResult(result) ) { return;