1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-09-02 00:59:38 +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.
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);

View File

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

View File

@ -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;

View File

@ -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);
};

View File

@ -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;