2017-12-28 19:49:02 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-10-17 16:52:34 +02:00
|
|
|
Copyright (C) 2017-present Raymond Hill
|
2017-12-28 19:49:02 +01:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
µBlock.htmlFilteringEngine = (function() {
|
2018-12-15 16:46:17 +01:00
|
|
|
const µb = µBlock;
|
|
|
|
const pselectors = new Map();
|
|
|
|
const duplicates = new Set();
|
2017-12-28 19:49:02 +01:00
|
|
|
|
2019-05-14 14:52:34 +02:00
|
|
|
let filterDB = new µb.staticExtFilteringEngine.HostnameBasedDB(2),
|
2017-12-29 19:31:37 +01:00
|
|
|
acceptedCount = 0,
|
|
|
|
discardedCount = 0,
|
2018-10-17 16:52:34 +02:00
|
|
|
docRegister;
|
2017-12-28 19:49:02 +01:00
|
|
|
|
2018-12-15 16:46:17 +01:00
|
|
|
const api = {
|
|
|
|
get acceptedCount() {
|
|
|
|
return acceptedCount;
|
|
|
|
},
|
|
|
|
get discardedCount() {
|
|
|
|
return discardedCount;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-11 19:21:23 +02:00
|
|
|
const PSelectorHasTextTask = class {
|
|
|
|
constructor(task) {
|
|
|
|
let arg0 = task[1], arg1;
|
|
|
|
if ( Array.isArray(task[1]) ) {
|
|
|
|
arg1 = arg0[1]; arg0 = arg0[0];
|
|
|
|
}
|
|
|
|
this.needle = new RegExp(arg0, arg1);
|
2017-12-31 00:55:01 +01:00
|
|
|
}
|
2019-05-11 19:21:23 +02:00
|
|
|
exec(input) {
|
|
|
|
const output = [];
|
|
|
|
for ( const node of input ) {
|
|
|
|
if ( this.needle.test(node.textContent) ) {
|
|
|
|
output.push(node);
|
|
|
|
}
|
2017-12-28 19:49:02 +01:00
|
|
|
}
|
2019-05-11 19:21:23 +02:00
|
|
|
return output;
|
2017-12-28 19:49:02 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-11 19:21:23 +02:00
|
|
|
const PSelectorIfTask = class {
|
|
|
|
constructor(task) {
|
|
|
|
this.pselector = new PSelector(task[1]);
|
2017-12-29 16:26:50 +01:00
|
|
|
}
|
2019-05-11 19:21:23 +02:00
|
|
|
exec(input) {
|
|
|
|
const output = [];
|
|
|
|
for ( const node of input ) {
|
|
|
|
if ( this.pselector.test(node) === this.target ) {
|
|
|
|
output.push(node);
|
|
|
|
}
|
2017-12-28 19:49:02 +01:00
|
|
|
}
|
2019-05-11 19:21:23 +02:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
get invalid() {
|
|
|
|
return this.pselector.invalid;
|
2017-12-28 19:49:02 +01:00
|
|
|
}
|
|
|
|
};
|
2019-05-11 19:21:23 +02:00
|
|
|
PSelectorIfTask.prototype.target = true;
|
2017-12-28 19:49:02 +01:00
|
|
|
|
2019-05-11 19:21:23 +02:00
|
|
|
const PSelectorIfNotTask = class extends PSelectorIfTask {
|
|
|
|
constructor(task) {
|
2019-06-20 20:11:54 +02:00
|
|
|
super(task);
|
2019-05-11 19:21:23 +02:00
|
|
|
this.target = false;
|
|
|
|
}
|
2017-12-28 19:49:02 +01:00
|
|
|
};
|
|
|
|
|
2019-06-20 20:11:54 +02:00
|
|
|
const PSelectorMinTextLengthTask = class {
|
|
|
|
constructor(task) {
|
|
|
|
this.min = task[1];
|
|
|
|
}
|
|
|
|
exec(input) {
|
|
|
|
const output = [];
|
|
|
|
for ( const node of input ) {
|
|
|
|
if ( node.textContent.length >= this.min ) {
|
|
|
|
output.push(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-11 19:21:23 +02:00
|
|
|
const PSelectorNthAncestorTask = class {
|
|
|
|
constructor(task) {
|
|
|
|
this.nth = task[1];
|
|
|
|
}
|
|
|
|
exec(input) {
|
|
|
|
const output = [];
|
|
|
|
for ( let node of input ) {
|
|
|
|
let nth = this.nth;
|
|
|
|
for (;;) {
|
|
|
|
node = node.parentElement;
|
|
|
|
if ( node === null ) { break; }
|
|
|
|
nth -= 1;
|
|
|
|
if ( nth !== 0 ) { continue; }
|
2017-12-28 19:49:02 +01:00
|
|
|
output.push(node);
|
2019-05-11 19:21:23 +02:00
|
|
|
break;
|
2017-12-28 19:49:02 +01:00
|
|
|
}
|
|
|
|
}
|
2019-05-11 19:21:23 +02:00
|
|
|
return output;
|
2017-12-28 19:49:02 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-11 19:21:23 +02:00
|
|
|
const PSelectorXpathTask = class {
|
|
|
|
constructor(task) {
|
|
|
|
this.xpe = task[1];
|
2017-12-28 19:49:02 +01:00
|
|
|
}
|
2019-05-11 19:21:23 +02:00
|
|
|
exec(input) {
|
|
|
|
const output = [];
|
|
|
|
const xpe = docRegister.createExpression(this.xpe, null);
|
|
|
|
let xpr = null;
|
|
|
|
for ( const node of input ) {
|
|
|
|
xpr = xpe.evaluate(
|
|
|
|
node,
|
|
|
|
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
|
|
|
|
xpr
|
|
|
|
);
|
|
|
|
let j = xpr.snapshotLength;
|
|
|
|
while ( j-- ) {
|
|
|
|
const node = xpr.snapshotItem(j);
|
|
|
|
if ( node.nodeType === 1 ) {
|
|
|
|
output.push(node);
|
|
|
|
}
|
|
|
|
}
|
2017-12-29 16:26:50 +01:00
|
|
|
}
|
2019-05-11 19:21:23 +02:00
|
|
|
return output;
|
2017-12-28 19:49:02 +01:00
|
|
|
}
|
|
|
|
};
|
2019-05-11 19:21:23 +02:00
|
|
|
|
|
|
|
const PSelector = class {
|
|
|
|
constructor(o) {
|
|
|
|
this.raw = o.raw;
|
|
|
|
this.selector = o.selector;
|
|
|
|
this.tasks = [];
|
|
|
|
if ( !o.tasks ) { return; }
|
|
|
|
for ( const task of o.tasks ) {
|
|
|
|
const ctor = this.operatorToTaskMap.get(task[0]);
|
|
|
|
if ( ctor === undefined ) {
|
|
|
|
this.invalid = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const pselector = new ctor(task);
|
|
|
|
if ( pselector instanceof PSelectorIfTask && pselector.invalid ) {
|
|
|
|
this.invalid = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
this.tasks.push(pselector);
|
|
|
|
}
|
2017-12-28 19:49:02 +01:00
|
|
|
}
|
2019-05-11 19:21:23 +02:00
|
|
|
prime(input) {
|
|
|
|
const root = input || docRegister;
|
|
|
|
if ( this.selector !== '' ) {
|
|
|
|
return root.querySelectorAll(this.selector);
|
|
|
|
}
|
|
|
|
return [ root ];
|
2017-12-28 19:49:02 +01:00
|
|
|
}
|
2019-05-11 19:21:23 +02:00
|
|
|
exec(input) {
|
|
|
|
if ( this.invalid ) { return []; }
|
|
|
|
let nodes = this.prime(input);
|
2018-12-15 16:46:17 +01:00
|
|
|
for ( const task of this.tasks ) {
|
2019-05-11 19:21:23 +02:00
|
|
|
if ( nodes.length === 0 ) { break; }
|
|
|
|
nodes = task.exec(nodes);
|
2017-12-29 16:26:50 +01:00
|
|
|
}
|
2019-05-11 19:21:23 +02:00
|
|
|
return nodes;
|
|
|
|
}
|
|
|
|
test(input) {
|
|
|
|
if ( this.invalid ) { return false; }
|
|
|
|
const nodes = this.prime(input);
|
|
|
|
const AA = [ null ];
|
|
|
|
for ( const node of nodes ) {
|
|
|
|
AA[0] = node;
|
|
|
|
let aa = AA;
|
|
|
|
for ( const task of this.tasks ) {
|
|
|
|
aa = task.exec(aa);
|
|
|
|
if ( aa.length === 0 ) { break; }
|
|
|
|
}
|
|
|
|
if ( aa.length !== 0 ) { return true; }
|
|
|
|
}
|
|
|
|
return false;
|
2017-12-29 16:26:50 +01:00
|
|
|
}
|
|
|
|
};
|
2019-05-11 19:21:23 +02:00
|
|
|
PSelector.prototype.operatorToTaskMap = new Map([
|
|
|
|
[ ':has', PSelectorIfTask ],
|
|
|
|
[ ':has-text', PSelectorHasTextTask ],
|
|
|
|
[ ':if', PSelectorIfTask ],
|
|
|
|
[ ':if-not', PSelectorIfNotTask ],
|
2019-06-20 20:11:54 +02:00
|
|
|
[ ':min-text-length', PSelectorMinTextLengthTask ],
|
2019-05-11 19:21:23 +02:00
|
|
|
[ ':not', PSelectorIfNotTask ],
|
|
|
|
[ ':nth-ancestor', PSelectorNthAncestorTask ],
|
|
|
|
[ ':xpath', PSelectorXpathTask ]
|
|
|
|
]);
|
|
|
|
PSelector.prototype.invalid = false;
|
2017-12-28 19:49:02 +01:00
|
|
|
|
2018-11-18 11:56:13 +01:00
|
|
|
const logOne = function(details, exception, selector) {
|
2018-12-13 18:30:54 +01:00
|
|
|
µBlock.filteringContext
|
|
|
|
.duplicate()
|
|
|
|
.fromTabId(details.tabId)
|
|
|
|
.setRealm('cosmetic')
|
|
|
|
.setType('dom')
|
|
|
|
.setURL(details.url)
|
|
|
|
.setDocOriginFromURL(details.url)
|
|
|
|
.setFilter({
|
2018-10-17 16:52:34 +02:00
|
|
|
source: 'cosmetic',
|
2019-05-14 14:52:34 +02:00
|
|
|
raw: `${exception === 0 ? '##' : '#@#'}^${selector}`
|
2018-12-13 18:30:54 +01:00
|
|
|
})
|
|
|
|
.toLogger();
|
2017-12-28 19:49:02 +01:00
|
|
|
};
|
|
|
|
|
2018-11-18 11:56:13 +01:00
|
|
|
const applyProceduralSelector = function(details, selector) {
|
2018-10-17 16:52:34 +02:00
|
|
|
let pselector = pselectors.get(selector);
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( pselector === undefined ) {
|
|
|
|
pselector = new PSelector(JSON.parse(selector));
|
|
|
|
pselectors.set(selector, pselector);
|
|
|
|
}
|
2018-12-15 16:46:17 +01:00
|
|
|
const nodes = pselector.exec();
|
|
|
|
let i = nodes.length,
|
2017-12-28 19:49:02 +01:00
|
|
|
modified = false;
|
|
|
|
while ( i-- ) {
|
2018-12-15 16:46:17 +01:00
|
|
|
const node = nodes[i];
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( node.parentNode !== null ) {
|
|
|
|
node.parentNode.removeChild(node);
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
}
|
2018-12-13 18:30:54 +01:00
|
|
|
if ( modified && µb.logger.enabled ) {
|
2018-10-17 16:52:34 +02:00
|
|
|
logOne(details, 0, pselector.raw);
|
2017-12-28 19:49:02 +01:00
|
|
|
}
|
|
|
|
return modified;
|
|
|
|
};
|
|
|
|
|
2018-11-18 11:56:13 +01:00
|
|
|
const applyCSSSelector = function(details, selector) {
|
2018-12-15 16:46:17 +01:00
|
|
|
const nodes = docRegister.querySelectorAll(selector);
|
|
|
|
let i = nodes.length,
|
2017-12-28 19:49:02 +01:00
|
|
|
modified = false;
|
|
|
|
while ( i-- ) {
|
2018-12-15 16:46:17 +01:00
|
|
|
const node = nodes[i];
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( node.parentNode !== null ) {
|
|
|
|
node.parentNode.removeChild(node);
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
}
|
2018-12-13 18:30:54 +01:00
|
|
|
if ( modified && µb.logger.enabled ) {
|
2018-10-17 16:52:34 +02:00
|
|
|
logOne(details, 0, selector);
|
2017-12-28 19:49:02 +01:00
|
|
|
}
|
|
|
|
return modified;
|
|
|
|
};
|
|
|
|
|
|
|
|
api.reset = function() {
|
|
|
|
filterDB.clear();
|
|
|
|
pselectors.clear();
|
|
|
|
duplicates.clear();
|
2017-12-29 19:31:37 +01:00
|
|
|
acceptedCount = 0;
|
|
|
|
discardedCount = 0;
|
2017-12-28 19:49:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
api.freeze = function() {
|
|
|
|
duplicates.clear();
|
2019-05-14 14:52:34 +02:00
|
|
|
filterDB.collectGarbage();
|
2017-12-28 19:49:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
api.compile = function(parsed, writer) {
|
2018-12-15 16:46:17 +01:00
|
|
|
const selector = parsed.suffix.slice(1).trim();
|
|
|
|
const compiled = µb.staticExtFilteringEngine.compileSelector(selector);
|
|
|
|
if ( compiled === undefined ) {
|
|
|
|
const who = writer.properties.get('assetKey') || '?';
|
|
|
|
µb.logger.writeOne({
|
2019-01-12 22:36:20 +01:00
|
|
|
realm: 'message',
|
|
|
|
type: 'error',
|
|
|
|
text: `Invalid HTML filter in ${who}: ##${selector}`
|
2018-12-15 16:46:17 +01:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2017-12-28 19:49:02 +01:00
|
|
|
|
|
|
|
// 1002 = html filtering
|
|
|
|
writer.select(1002);
|
|
|
|
|
|
|
|
// TODO: Mind negated hostnames, they are currently discarded.
|
|
|
|
|
2018-12-15 16:46:17 +01:00
|
|
|
for ( const hn of parsed.hostnames ) {
|
2018-09-09 14:10:09 +02:00
|
|
|
if ( hn.charCodeAt(0) === 0x7E /* '~' */ ) { continue; }
|
2019-05-14 14:52:34 +02:00
|
|
|
let kind = 0;
|
2018-09-09 14:10:09 +02:00
|
|
|
if ( parsed.exception ) {
|
2019-05-14 14:52:34 +02:00
|
|
|
kind |= 0b01;
|
2018-09-09 14:10:09 +02:00
|
|
|
}
|
2019-05-14 14:52:34 +02:00
|
|
|
if ( compiled.charCodeAt(0) === 0x7B /* '{' */ ) {
|
|
|
|
kind |= 0b10;
|
|
|
|
}
|
|
|
|
writer.push([ 64, hn, kind, compiled ]);
|
2017-12-28 19:49:02 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
api.fromCompiledContent = function(reader) {
|
|
|
|
// Don't bother loading filters if stream filtering is not supported.
|
2018-10-28 14:58:25 +01:00
|
|
|
if ( µb.canFilterResponseData === false ) { return; }
|
2017-12-28 19:49:02 +01:00
|
|
|
|
|
|
|
// 1002 = html filtering
|
|
|
|
reader.select(1002);
|
|
|
|
|
|
|
|
while ( reader.next() ) {
|
2017-12-29 19:31:37 +01:00
|
|
|
acceptedCount += 1;
|
2018-12-15 16:46:17 +01:00
|
|
|
const fingerprint = reader.fingerprint();
|
2017-12-29 19:31:37 +01:00
|
|
|
if ( duplicates.has(fingerprint) ) {
|
|
|
|
discardedCount += 1;
|
|
|
|
continue;
|
|
|
|
}
|
2017-12-28 19:49:02 +01:00
|
|
|
duplicates.add(fingerprint);
|
2018-12-15 16:46:17 +01:00
|
|
|
const args = reader.args();
|
2019-05-14 14:52:34 +02:00
|
|
|
filterDB.store(args[1], args[2], args[3]);
|
2017-12-28 19:49:02 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-17 16:52:34 +02:00
|
|
|
api.retrieve = function(details) {
|
2018-12-13 18:30:54 +01:00
|
|
|
const hostname = details.hostname;
|
2017-12-28 19:49:02 +01:00
|
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/2835
|
|
|
|
// Do not filter if the site is under an `allow` rule.
|
|
|
|
if (
|
|
|
|
µb.userSettings.advancedUserEnabled &&
|
|
|
|
µb.sessionFirewall.evaluateCellZY(hostname, hostname, '*') === 2
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-14 14:52:34 +02:00
|
|
|
const plains = new Set();
|
|
|
|
const procedurals = new Set();
|
|
|
|
const exceptions = new Set();
|
2017-12-28 19:49:02 +01:00
|
|
|
|
2019-05-14 14:52:34 +02:00
|
|
|
filterDB.retrieve(
|
|
|
|
hostname,
|
|
|
|
[ plains, exceptions, procedurals, exceptions ]
|
|
|
|
);
|
|
|
|
if ( details.entity !== '' ) {
|
|
|
|
filterDB.retrieve(
|
|
|
|
`${hostname.slice(0, -details.domain)}${details.entity}`,
|
|
|
|
[ plains, exceptions, procedurals, exceptions ]
|
|
|
|
);
|
2018-10-17 16:52:34 +02:00
|
|
|
}
|
2019-05-14 14:52:34 +02:00
|
|
|
|
|
|
|
if ( plains.size === 0 && procedurals.size === 0 ) { return; }
|
|
|
|
|
|
|
|
const out = { plains, procedurals };
|
2017-12-28 19:49:02 +01:00
|
|
|
|
2019-05-14 14:52:34 +02:00
|
|
|
if ( exceptions.size === 0 ) {
|
|
|
|
return out;
|
2018-10-17 16:52:34 +02:00
|
|
|
}
|
2019-05-14 14:52:34 +02:00
|
|
|
|
|
|
|
for ( const selector of exceptions ) {
|
|
|
|
if ( plains.has(selector) ) {
|
|
|
|
plains.delete(selector);
|
|
|
|
logOne(details, 1, selector);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( procedurals.has(selector) ) {
|
|
|
|
procedurals.delete(selector);
|
|
|
|
logOne(details, 1, JSON.parse(selector).raw);
|
|
|
|
continue;
|
2018-10-17 16:52:34 +02:00
|
|
|
}
|
2017-12-28 19:49:02 +01:00
|
|
|
}
|
2018-10-17 16:52:34 +02:00
|
|
|
|
2019-05-14 14:52:34 +02:00
|
|
|
if ( plains.size !== 0 || procedurals.size !== 0 ) {
|
|
|
|
return out;
|
|
|
|
}
|
2017-12-28 19:49:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
api.apply = function(doc, details) {
|
|
|
|
docRegister = doc;
|
2018-10-17 16:52:34 +02:00
|
|
|
let modified = false;
|
2019-05-14 14:52:34 +02:00
|
|
|
for ( const selector of details.selectors.plains ) {
|
|
|
|
if ( applyCSSSelector(details, selector) ) {
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for ( const selector of details.selectors.procedurals ) {
|
|
|
|
if ( applyProceduralSelector(details, selector) ) {
|
|
|
|
modified = true;
|
2017-12-28 19:49:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 16:52:34 +02:00
|
|
|
docRegister = undefined;
|
2017-12-28 19:49:02 +01:00
|
|
|
return modified;
|
|
|
|
};
|
|
|
|
|
|
|
|
api.toSelfie = function() {
|
|
|
|
return filterDB.toSelfie();
|
|
|
|
};
|
|
|
|
|
|
|
|
api.fromSelfie = function(selfie) {
|
2019-05-14 14:52:34 +02:00
|
|
|
filterDB = new µb.staticExtFilteringEngine.HostnameBasedDB(2, selfie);
|
2017-12-28 19:49:02 +01:00
|
|
|
pselectors.clear();
|
|
|
|
};
|
|
|
|
|
|
|
|
return api;
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|