this addresses #850

This commit is contained in:
gorhill 2015-11-01 11:25:36 -05:00
parent 2dee9a4c43
commit 71b967f452
1 changed files with 44 additions and 28 deletions

View File

@ -259,7 +259,7 @@ var uBlockCollapser = (function() {
if ( node.localName === 'iframe' ) { if ( node.localName === 'iframe' ) {
addIFrame(node); addIFrame(node);
} }
var iframes = node.querySelectorAll('iframe'); var iframes = node.getElementsByTagName('iframe');
var i = iframes.length; var i = iframes.length;
while ( i-- ) { while ( i-- ) {
addIFrame(iframes[i]); addIFrame(iframes[i]);
@ -580,38 +580,47 @@ var uBlockCollapser = (function() {
// - [href^="http"] // - [href^="http"]
var processHighMediumGenerics = function(generics, out) { var processHighMediumGenerics = function(generics, out) {
var nodeList = selectNodes('a[href^="http"]'); var doc = document;
var iNode = nodeList.length; var i = contextNodes.length;
var node, href, pos, hash, selectors, selector, iSelector; var aa = [ null ];
var node, nodes;
while ( i-- ) {
node = contextNodes[i];
if ( node.localName === 'a' ) {
aa[0] = node;
processHighMediumGenericsForNodes(aa, generics, out);
}
nodes = node.getElementsByTagName('a');
if ( nodes.length === 0 ) { continue; }
processHighMediumGenericsForNodes(nodes, generics, out);
if ( node === doc ) {
break;
}
}
};
while ( iNode-- ) { var processHighMediumGenericsForNodes = function(nodes, generics, out) {
node = nodeList[iNode]; var i = nodes.length;
var node, href, pos, hash, selectors, j, selector;
var aa = [ '' ];
while ( i-- ) {
node = nodes[i];
href = node.getAttribute('href'); href = node.getAttribute('href');
if ( !href ) { continue; } if ( !href ) { continue; }
pos = href.indexOf('://'); pos = href.indexOf('://');
if ( pos === -1 ) { continue; } if ( pos === -1 ) { continue; }
hash = href.slice(pos + 3, pos + 11); hash = href.slice(pos + 3, pos + 11);
selectors = generics[hash]; selectors = generics[hash];
if ( selectors === undefined ) { continue; } if ( selectors === undefined ) { continue; }
// A string. // A string.
if ( typeof selectors === 'string' ) { if ( typeof selectors === 'string' ) {
if ( aa[0] = selectors;
href.lastIndexOf(selectors.slice(8, -2), 0) === 0 && selectors = aa;
injectedSelectors.hasOwnProperty(selectors) === false
) {
injectedSelectors[selectors] = true;
out.push(selectors);
} }
continue;
}
// An array of strings. // An array of strings.
iSelector = selectors.length; j = selectors.length;
while ( iSelector-- ) { while ( j-- ) {
selector = selectors[iSelector]; selector = selectors[j];
if ( if (
href.lastIndexOf(selector.slice(8, -2), 0) === 0 && href.lastIndexOf(selector.slice(8, -2), 0) === 0 &&
injectedSelectors.hasOwnProperty(selector) === false injectedSelectors.hasOwnProperty(selector) === false
@ -687,8 +696,7 @@ var uBlockCollapser = (function() {
while ( i-- ) { while ( i-- ) {
node = nodes[i]; node = nodes[i];
if ( node.nodeType !== 1 ) { continue; } if ( node.nodeType !== 1 ) { continue; }
// id v = node.id;
v = nodes[i].id;
if ( typeof v !== 'string' ) { continue; } if ( typeof v !== 'string' ) { continue; }
v = v.trim(); v = v.trim();
if ( v === '' ) { continue; } if ( v === '' ) { continue; }
@ -891,20 +899,27 @@ var uBlockCollapser = (function() {
/******************************************************************************/ /******************************************************************************/
// https://github.com/chrisaljoudi/uBlock/issues/7 // https://github.com/chrisaljoudi/uBlock/issues/7
// Executed only once.
// Executed only once // Preferring getElementsByTagName over querySelectorAll:
// http://jsperf.com/queryselectorall-vs-getelementsbytagname/145
(function() { (function() {
var collapser = uBlockCollapser; var collapser = uBlockCollapser;
var elems, i, elem; var elems, i, elem;
elems = document.querySelectorAll('embed, object'); elems = document.getElementsByTagName('embed');
i = elems.length; i = elems.length;
while ( i-- ) { while ( i-- ) {
collapser.add(elems[i]); collapser.add(elems[i]);
} }
elems = document.querySelectorAll('img'); elems = document.getElementsByTagName('object');
i = elems.length;
while ( i-- ) {
collapser.add(elems[i]);
}
elems = document.getElementsByTagName('img');
i = elems.length; i = elems.length;
while ( i-- ) { while ( i-- ) {
elem = elems[i]; elem = elems[i];
@ -913,11 +928,12 @@ var uBlockCollapser = (function() {
} }
} }
elems = document.querySelectorAll('iframe'); elems = document.getElementsByTagName('iframe');
i = elems.length; i = elems.length;
while ( i-- ) { while ( i-- ) {
collapser.addIFrame(elems[i]); collapser.addIFrame(elems[i]);
} }
collapser.process(0); collapser.process(0);
})(); })();