mirror of https://github.com/gorhill/uBlock.git
This commit is contained in:
parent
28cb2e4f4d
commit
ba32c6a473
|
@ -112,18 +112,18 @@ var fromNetFilter = function(details) {
|
||||||
// FilterContainer.fromCompiledContent() is our reference code to create
|
// FilterContainer.fromCompiledContent() is our reference code to create
|
||||||
// the various compiled versions.
|
// the various compiled versions.
|
||||||
|
|
||||||
var fromCosmeticFilter = function(details) {
|
let fromCosmeticFilter = function(details) {
|
||||||
var match = /^#@?#\^?/.exec(details.rawFilter),
|
let match = /^#@?#\^?/.exec(details.rawFilter),
|
||||||
prefix = match[0],
|
prefix = match[0],
|
||||||
exception = prefix.charAt(1) === '@',
|
exception = prefix.charAt(1) === '@',
|
||||||
selector = details.rawFilter.slice(prefix.length);
|
selector = details.rawFilter.slice(prefix.length);
|
||||||
|
|
||||||
// The longer the needle, the lower the number of false positives.
|
// The longer the needle, the lower the number of false positives.
|
||||||
var needle = selector.match(/\w+/g).reduce(function(a, b) {
|
let needle = selector.match(/\w+/g).reduce(function(a, b) {
|
||||||
return a.length > b.length ? a : b;
|
return a.length > b.length ? a : b;
|
||||||
});
|
});
|
||||||
|
|
||||||
var reHostname = new RegExp(
|
let reHostname = new RegExp(
|
||||||
'^' +
|
'^' +
|
||||||
details.hostname.split('.').reduce(
|
details.hostname.split('.').reduce(
|
||||||
function(acc, item) {
|
function(acc, item) {
|
||||||
|
@ -136,7 +136,7 @@ var fromCosmeticFilter = function(details) {
|
||||||
'$'
|
'$'
|
||||||
);
|
);
|
||||||
|
|
||||||
var reEntity,
|
let reEntity,
|
||||||
domain = details.domain,
|
domain = details.domain,
|
||||||
pos = domain.indexOf('.');
|
pos = domain.indexOf('.');
|
||||||
if ( pos !== -1 ) {
|
if ( pos !== -1 ) {
|
||||||
|
@ -154,24 +154,28 @@ var fromCosmeticFilter = function(details) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
var response = Object.create(null),
|
let hostnameMatches = hn => {
|
||||||
assetKey, entry, content,
|
return hn === '' ||
|
||||||
found, beg, end,
|
reHostname.test(hn) ||
|
||||||
fargs, isProcedural;
|
reEntity !== undefined && reEntity.test(hn);
|
||||||
|
};
|
||||||
|
|
||||||
for ( assetKey in listEntries ) {
|
let response = Object.create(null);
|
||||||
entry = listEntries[assetKey];
|
|
||||||
|
for ( let assetKey in listEntries ) {
|
||||||
|
let entry = listEntries[assetKey];
|
||||||
if ( entry === undefined ) { continue; }
|
if ( entry === undefined ) { continue; }
|
||||||
content = extractBlocks(entry.content, 1000, 2000);
|
let content = extractBlocks(entry.content, 1000, 2000),
|
||||||
pos = 0;
|
isProcedural,
|
||||||
found = undefined;
|
found;
|
||||||
|
let pos = 0;
|
||||||
while ( (pos = content.indexOf(needle, pos)) !== -1 ) {
|
while ( (pos = content.indexOf(needle, pos)) !== -1 ) {
|
||||||
beg = content.lastIndexOf('\n', pos);
|
let beg = content.lastIndexOf('\n', pos);
|
||||||
if ( beg === -1 ) { beg = 0; }
|
if ( beg === -1 ) { beg = 0; }
|
||||||
end = content.indexOf('\n', pos);
|
let end = content.indexOf('\n', pos);
|
||||||
if ( end === -1 ) { end = content.length; }
|
if ( end === -1 ) { end = content.length; }
|
||||||
pos = end;
|
pos = end;
|
||||||
fargs = JSON.parse(content.slice(beg, end));
|
let fargs = JSON.parse(content.slice(beg, end));
|
||||||
|
|
||||||
// https://github.com/gorhill/uBlock/issues/2763
|
// https://github.com/gorhill/uBlock/issues/2763
|
||||||
if ( fargs[0] >= 0 && fargs[0] <= 5 && details.ignoreGeneric ) {
|
if ( fargs[0] >= 0 && fargs[0] <= 5 && details.ignoreGeneric ) {
|
||||||
|
@ -179,7 +183,8 @@ var fromCosmeticFilter = function(details) {
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ( fargs[0] ) {
|
switch ( fargs[0] ) {
|
||||||
case 0: // id-based
|
// Lowly generic cosmetic filters
|
||||||
|
case 0: // simple id-based
|
||||||
if (
|
if (
|
||||||
fargs[1] === selector.slice(1) &&
|
fargs[1] === selector.slice(1) &&
|
||||||
selector.charAt(0) === '#'
|
selector.charAt(0) === '#'
|
||||||
|
@ -187,7 +192,7 @@ var fromCosmeticFilter = function(details) {
|
||||||
found = prefix + selector;
|
found = prefix + selector;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 2: // class-based
|
case 2: // simple class-based
|
||||||
if (
|
if (
|
||||||
fargs[1] === selector.slice(1) &&
|
fargs[1] === selector.slice(1) &&
|
||||||
selector.charAt(0) === '.'
|
selector.charAt(0) === '.'
|
||||||
|
@ -195,19 +200,21 @@ var fromCosmeticFilter = function(details) {
|
||||||
found = prefix + selector;
|
found = prefix + selector;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 1: // id-based
|
case 1: // complex id-based
|
||||||
case 3: // class-based
|
case 3: // complex class-based
|
||||||
if ( fargs[2] === selector ) {
|
if ( fargs[2] === selector ) {
|
||||||
found = prefix + selector;
|
found = prefix + selector;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 4:
|
// Highly generic cosmetic filters
|
||||||
case 5:
|
case 4: // simple highly generic
|
||||||
case 7:
|
case 5: // complex highly generic
|
||||||
|
case 7: // generic exception
|
||||||
if ( fargs[1] === selector ) {
|
if ( fargs[1] === selector ) {
|
||||||
found = prefix + selector;
|
found = prefix + selector;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
// Specific cosmetic filtering
|
||||||
case 8:
|
case 8:
|
||||||
if ( exception !== ((fargs[1] & 0b01) !== 0) ) { break; }
|
if ( exception !== ((fargs[1] & 0b01) !== 0) ) { break; }
|
||||||
isProcedural = (fargs[1] & 0b10) !== 0;
|
isProcedural = (fargs[1] & 0b10) !== 0;
|
||||||
|
@ -217,24 +224,29 @@ var fromCosmeticFilter = function(details) {
|
||||||
) {
|
) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (
|
if ( hostnameMatches(fargs[2]) ) {
|
||||||
fargs[2] === '' ||
|
|
||||||
reHostname.test(fargs[2]) === true ||
|
|
||||||
reEntity !== undefined && reEntity.test(fargs[2]) === true
|
|
||||||
) {
|
|
||||||
found = fargs[2] + prefix + selector;
|
found = fargs[2] + prefix + selector;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
// Scriptlet injection
|
||||||
case 32:
|
case 32:
|
||||||
case 64:
|
|
||||||
case 65:
|
|
||||||
if ( exception !== (fargs[1].charAt(0) === '!') ) { break; }
|
if ( exception !== (fargs[1].charAt(0) === '!') ) { break; }
|
||||||
if ( fargs[3] !== selector ) { break; }
|
if ( fargs[3] !== selector ) { break; }
|
||||||
|
if ( hostnameMatches(fargs[2]) ) {
|
||||||
|
found = fargs[2] + prefix + selector;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
// HTML filtering
|
||||||
|
case 64: // CSS selector
|
||||||
|
case 65: // procdural
|
||||||
|
if ( exception !== (fargs[1].charAt(0) === '!') ) { break; }
|
||||||
if (
|
if (
|
||||||
fargs[2] === '' ||
|
fargs[0] === 64 && fargs[3] !== selector ||
|
||||||
reHostname.test(fargs[2]) === true ||
|
fargs[0] === 65 && JSON.parse(fargs[3]).raw !== selector
|
||||||
reEntity !== undefined && reEntity.test(fargs[2]) === true
|
|
||||||
) {
|
) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if ( hostnameMatches(fargs[2]) ) {
|
||||||
found = fargs[2] + prefix + selector;
|
found = fargs[2] + prefix + selector;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in New Issue