1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-09-15 07:22:28 +02:00

various minor code review

This commit is contained in:
gorhill 2016-06-30 22:03:29 -04:00
parent a3f6a4186a
commit 8374799c7d
5 changed files with 57 additions and 29 deletions

View File

@ -923,15 +923,15 @@ vAPI.executionCost.start();
// Extract and return the staged nodes which (may) match the selectors. // Extract and return the staged nodes which (may) match the selectors.
var selectNodes = function(selector) { var selectNodes = function(selector) {
var targetNodes = []; var stagedNodes = contextNodes,
var i = contextNodes.length; i = stagedNodes.length;
var node, nodeList, j; if ( i === 1 && stagedNodes[0] === document.documentElement ) {
var doc = document; return document.querySelectorAll(selector);
}
var targetNodes = [],
node, nodeList, j;
while ( i-- ) { while ( i-- ) {
node = contextNodes[i]; node = stagedNodes[i];
if ( node === doc ) {
return doc.querySelectorAll(selector);
}
targetNodes.push(node); targetNodes.push(node);
nodeList = node.querySelectorAll(selector); nodeList = node.querySelectorAll(selector);
j = nodeList.length; j = nodeList.length;
@ -988,29 +988,31 @@ vAPI.executionCost.start();
// - [href^="http"] // - [href^="http"]
var processHighMediumGenerics = function(generics) { var processHighMediumGenerics = function(generics) {
var doc = document; var stagedNodes = contextNodes,
var i = contextNodes.length; i = stagedNodes.length;
var aa = [ null ]; if ( i === 1 && stagedNodes[0] === document.documentElement ) {
var node, nodes; processHighMediumGenericsForNodes(document.links, generics);
return;
}
var aa = [ null ],
node, nodes;
while ( i-- ) { while ( i-- ) {
node = contextNodes[i]; node = stagedNodes[i];
if ( node.localName === 'a' ) { if ( node.localName === 'a' ) {
aa[0] = node; aa[0] = node;
processHighMediumGenericsForNodes(aa, generics); processHighMediumGenericsForNodes(aa, generics);
} }
nodes = node.getElementsByTagName('a'); nodes = node.getElementsByTagName('a');
if ( nodes.length === 0 ) { continue; } if ( nodes.length !== 0 ) {
processHighMediumGenericsForNodes(nodes, generics); processHighMediumGenericsForNodes(nodes, generics);
if ( node === doc ) {
break;
} }
} }
}; };
var processHighMediumGenericsForNodes = function(nodes, generics) { var processHighMediumGenericsForNodes = function(nodes, generics) {
var i = nodes.length; var i = nodes.length,
var node, href, pos, hash, selectors, j, selector; node, href, pos, hash, selectors, j, selector,
var aa = [ '' ]; aa = [ '' ];
while ( i-- ) { while ( i-- ) {
node = nodes[i]; node = nodes[i];
href = node.getAttribute('href'); href = node.getAttribute('href');
@ -1139,7 +1141,12 @@ vAPI.executionCost.start();
// Start cosmetic filtering. // Start cosmetic filtering.
classesAndIdsFromNodeList(document.querySelectorAll('[class],[id]')); var allElems = document.all;
classesAndIdsFromNodeList(
allElems instanceof Object ?
allElems :
document.querySelectorAll('[class],[id]')
);
retrieveGenericSelectors(); retrieveGenericSelectors();
//console.debug('%f: uBlock: survey time', timer.now() - tStart); //console.debug('%f: uBlock: survey time', timer.now() - tStart);

View File

@ -503,6 +503,8 @@ var filterRequests = function(pageStore, details) {
} }
request.collapse = true; request.collapse = true;
} }
context.dispose();
return requests; return requests;
}; };

View File

@ -19,6 +19,8 @@
Home: https://github.com/gorhill/uBlock Home: https://github.com/gorhill/uBlock
*/ */
'use strict';
/******************************************************************************* /*******************************************************************************
A PageRequestStore object is used to store net requests in two ways: 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() { µBlock.PageStore = (function() {
'use strict';
/******************************************************************************/ /******************************************************************************/
var µb = µBlock; var µb = µBlock;
@ -428,14 +428,14 @@ PageStore.prototype.setFrame = function(frameId, frameURL) {
/******************************************************************************/ /******************************************************************************/
PageStore.prototype.createContextFromPage = function() { PageStore.prototype.createContextFromPage = function() {
var context = new µb.tabContextManager.createContext(this.tabId); var context = µb.tabContextManager.createContext(this.tabId);
context.pageHostname = context.rootHostname; context.pageHostname = context.rootHostname;
context.pageDomain = context.rootDomain; context.pageDomain = context.rootDomain;
return context; return context;
}; };
PageStore.prototype.createContextFromFrameId = function(frameId) { 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]; var frameStore = this.frames[frameId];
if ( frameStore ) { if ( frameStore ) {
context.pageHostname = frameStore.pageHostname; context.pageHostname = frameStore.pageHostname;
@ -448,7 +448,7 @@ PageStore.prototype.createContextFromFrameId = function(frameId) {
}; };
PageStore.prototype.createContextFromFrameHostname = function(frameHostname) { PageStore.prototype.createContextFromFrameHostname = function(frameHostname) {
var context = new µb.tabContextManager.createContext(this.tabId); var context = µb.tabContextManager.createContext(this.tabId);
context.pageHostname = frameHostname; context.pageHostname = frameHostname;
context.pageDomain = µb.URI.domainFromHostname(frameHostname) || frameHostname; context.pageDomain = µb.URI.domainFromHostname(frameHostname) || frameHostname;
return context; return context;

View File

@ -405,7 +405,11 @@ housekeep itself.
})(); })();
// Context object, typically to be used to feed filtering engines. // Context object, typically to be used to feed filtering engines.
var contextJunkyard = [];
var Context = function(tabId) { var Context = function(tabId) {
this.init(tabId);
};
Context.prototype.init = function(tabId) {
var tabContext = lookup(tabId); var tabContext = lookup(tabId);
this.rootHostname = tabContext.rootHostname; this.rootHostname = tabContext.rootHostname;
this.rootDomain = tabContext.rootDomain; this.rootDomain = tabContext.rootDomain;
@ -414,9 +418,16 @@ housekeep itself.
this.requestURL = this.requestURL =
this.requestHostname = this.requestHostname =
this.requestDomain = ''; this.requestDomain = '';
return this;
};
Context.prototype.dispose = function() {
contextJunkyard.push(this);
}; };
var createContext = function(tabId) { var createContext = function(tabId) {
if ( contextJunkyard.length ) {
return contextJunkyard.pop().init(tabId);
}
return new Context(tabId); return new Context(tabId);
}; };

View File

@ -19,7 +19,7 @@
Home: https://github.com/gorhill/uBlock Home: https://github.com/gorhill/uBlock
*/ */
/* global µBlock, vAPI */ 'use strict';
/******************************************************************************/ /******************************************************************************/
@ -27,8 +27,6 @@
µBlock.webRequest = (function() { µBlock.webRequest = (function() {
'use strict';
/******************************************************************************/ /******************************************************************************/
var exports = {}; var exports = {};
@ -115,6 +113,7 @@ var onBeforeRequest = function(details) {
if ( frameId > 0 && isFrame ) { if ( frameId > 0 && isFrame ) {
pageStore.setFrame(frameId, requestURL); pageStore.setFrame(frameId, requestURL);
} }
requestContext.dispose();
return; return;
} }
@ -141,9 +140,11 @@ var onBeforeRequest = function(details) {
requestContext.pageHostname requestContext.pageHostname
); );
} }
requestContext.dispose();
return { redirectUrl: url }; return { redirectUrl: url };
} }
requestContext.dispose();
return { cancel: true }; return { cancel: true };
}; };
@ -306,6 +307,7 @@ var onBeforeBeacon = function(details) {
context.rootHostname context.rootHostname
); );
} }
context.dispose();
if ( result !== '' ) { if ( result !== '' ) {
return { cancel: true }; return { cancel: true };
} }
@ -351,6 +353,8 @@ var onBeforeBehindTheSceneRequest = function(details) {
); );
} }
context.dispose();
// Not blocked // Not blocked
if ( µb.isAllowResult(result) ) { if ( µb.isAllowResult(result) ) {
return; return;
@ -425,6 +429,8 @@ var onRootFrameHeadersReceived = function(details) {
); );
} }
context.dispose();
// Don't block // Don't block
if ( µb.isAllowResult(result) ) { if ( µb.isAllowResult(result) ) {
return; return;
@ -471,6 +477,8 @@ var onFrameHeadersReceived = function(details) {
); );
} }
context.dispose();
// Don't block // Don't block
if ( µb.isAllowResult(result) ) { if ( µb.isAllowResult(result) ) {
return; return;