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
|
|
|
|
2018-11-18 11:56:13 +01:00
|
|
|
let filterDB = new µb.staticExtFilteringEngine.HostnameBasedDB(),
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-18 11:56:13 +01:00
|
|
|
const PSelectorHasTextTask = function(task) {
|
2018-10-17 16:52:34 +02:00
|
|
|
let arg0 = task[1], arg1;
|
2017-12-31 00:55:01 +01:00
|
|
|
if ( Array.isArray(task[1]) ) {
|
|
|
|
arg1 = arg0[1]; arg0 = arg0[0];
|
|
|
|
}
|
|
|
|
this.needle = new RegExp(arg0, arg1);
|
2017-12-28 19:49:02 +01:00
|
|
|
};
|
|
|
|
PSelectorHasTextTask.prototype.exec = function(input) {
|
2018-12-15 16:46:17 +01:00
|
|
|
const output = [];
|
|
|
|
for ( const node of input ) {
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( this.needle.test(node.textContent) ) {
|
|
|
|
output.push(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
};
|
|
|
|
|
2018-11-18 11:56:13 +01:00
|
|
|
const PSelectorIfTask = function(task) {
|
2017-12-28 19:49:02 +01:00
|
|
|
this.pselector = new PSelector(task[1]);
|
|
|
|
};
|
|
|
|
PSelectorIfTask.prototype.target = true;
|
2017-12-29 16:26:50 +01:00
|
|
|
Object.defineProperty(PSelectorIfTask.prototype, 'invalid', {
|
|
|
|
get: function() {
|
|
|
|
return this.pselector.invalid;
|
|
|
|
}
|
|
|
|
});
|
2017-12-28 19:49:02 +01:00
|
|
|
PSelectorIfTask.prototype.exec = function(input) {
|
2018-12-15 16:46:17 +01:00
|
|
|
const output = [];
|
|
|
|
for ( const node of input ) {
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( this.pselector.test(node) === this.target ) {
|
|
|
|
output.push(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
};
|
|
|
|
|
2018-11-18 11:56:13 +01:00
|
|
|
const PSelectorIfNotTask = function(task) {
|
2017-12-28 19:49:02 +01:00
|
|
|
PSelectorIfTask.call(this, task);
|
|
|
|
this.target = false;
|
|
|
|
};
|
|
|
|
PSelectorIfNotTask.prototype = Object.create(PSelectorIfTask.prototype);
|
|
|
|
PSelectorIfNotTask.prototype.constructor = PSelectorIfNotTask;
|
|
|
|
|
2018-11-18 11:56:13 +01:00
|
|
|
const PSelectorXpathTask = function(task) {
|
2017-12-28 19:49:02 +01:00
|
|
|
this.xpe = task[1];
|
|
|
|
};
|
|
|
|
PSelectorXpathTask.prototype.exec = function(input) {
|
2018-12-15 16:46:17 +01:00
|
|
|
const output = [];
|
|
|
|
const xpe = docRegister.createExpression(this.xpe, null);
|
|
|
|
let xpr = null;
|
|
|
|
for ( const node of input ) {
|
2017-12-28 19:49:02 +01:00
|
|
|
xpr = xpe.evaluate(
|
|
|
|
node,
|
|
|
|
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
|
|
|
|
xpr
|
|
|
|
);
|
2018-10-17 16:52:34 +02:00
|
|
|
let j = xpr.snapshotLength;
|
2017-12-28 19:49:02 +01:00
|
|
|
while ( j-- ) {
|
2018-12-15 16:46:17 +01:00
|
|
|
const node = xpr.snapshotItem(j);
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( node.nodeType === 1 ) {
|
|
|
|
output.push(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
};
|
|
|
|
|
2018-11-18 11:56:13 +01:00
|
|
|
const PSelector = function(o) {
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( PSelector.prototype.operatorToTaskMap === undefined ) {
|
|
|
|
PSelector.prototype.operatorToTaskMap = new Map([
|
2017-12-31 22:05:23 +01:00
|
|
|
[ ':has', PSelectorIfTask ],
|
2017-12-28 19:49:02 +01:00
|
|
|
[ ':has-text', PSelectorHasTextTask ],
|
|
|
|
[ ':if', PSelectorIfTask ],
|
|
|
|
[ ':if-not', PSelectorIfNotTask ],
|
2018-12-15 16:46:17 +01:00
|
|
|
[ ':not', PSelectorIfNotTask ],
|
2017-12-28 19:49:02 +01:00
|
|
|
[ ':xpath', PSelectorXpathTask ]
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
this.raw = o.raw;
|
|
|
|
this.selector = o.selector;
|
|
|
|
this.tasks = [];
|
2018-10-17 16:52:34 +02:00
|
|
|
if ( !o.tasks ) { return; }
|
2018-12-15 16:46:17 +01:00
|
|
|
for ( const task of o.tasks ) {
|
|
|
|
const ctor = this.operatorToTaskMap.get(task[0]);
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( ctor === undefined ) {
|
|
|
|
this.invalid = true;
|
|
|
|
break;
|
|
|
|
}
|
2018-12-15 16:46:17 +01:00
|
|
|
const pselector = new ctor(task);
|
2017-12-29 16:26:50 +01:00
|
|
|
if ( pselector instanceof PSelectorIfTask && pselector.invalid ) {
|
|
|
|
this.invalid = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
this.tasks.push(pselector);
|
2017-12-28 19:49:02 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
PSelector.prototype.operatorToTaskMap = undefined;
|
2017-12-29 16:26:50 +01:00
|
|
|
PSelector.prototype.invalid = false;
|
2017-12-28 19:49:02 +01:00
|
|
|
PSelector.prototype.prime = function(input) {
|
2018-12-15 16:46:17 +01:00
|
|
|
const root = input || docRegister;
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( this.selector !== '' ) {
|
|
|
|
return root.querySelectorAll(this.selector);
|
|
|
|
}
|
|
|
|
return [ root ];
|
|
|
|
};
|
|
|
|
PSelector.prototype.exec = function(input) {
|
2017-12-29 15:02:26 +01:00
|
|
|
if ( this.invalid ) { return []; }
|
2018-10-17 16:52:34 +02:00
|
|
|
let nodes = this.prime(input);
|
2018-12-15 16:46:17 +01:00
|
|
|
for ( const task of this.tasks ) {
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( nodes.length === 0 ) { break; }
|
|
|
|
nodes = task.exec(nodes);
|
|
|
|
}
|
|
|
|
return nodes;
|
|
|
|
};
|
2017-12-29 16:26:50 +01:00
|
|
|
PSelector.prototype.test = function(input) {
|
|
|
|
if ( this.invalid ) { return false; }
|
2018-12-15 16:46:17 +01:00
|
|
|
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 ) {
|
2017-12-29 16:26:50 +01:00
|
|
|
aa = task.exec(aa);
|
|
|
|
if ( aa.length === 0 ) { break; }
|
|
|
|
}
|
|
|
|
if ( aa.length !== 0 ) { return true; }
|
|
|
|
}
|
|
|
|
return 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',
|
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
|
|
|
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; }
|
|
|
|
let hash = µb.staticExtFilteringEngine.compileHostnameToHash(hn);
|
|
|
|
if ( parsed.exception ) {
|
|
|
|
hash |= 0b0001;
|
|
|
|
}
|
2017-12-28 19:49:02 +01:00
|
|
|
writer.push([
|
|
|
|
compiled.charCodeAt(0) !== 0x7B /* '{' */ ? 64 : 65,
|
2018-09-09 14:10:09 +02:00
|
|
|
hash,
|
|
|
|
hn,
|
2017-12-28 19:49:02 +01:00
|
|
|
compiled
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
2017-12-28 19:49:02 +01:00
|
|
|
filterDB.add(args[1], {
|
|
|
|
type: args[0],
|
|
|
|
hostname: args[2],
|
|
|
|
selector: args[3]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const toRemoveArray = [];
|
|
|
|
const domainHash = µb.staticExtFilteringEngine.makeHash(details.domain);
|
2018-09-09 14:10:09 +02:00
|
|
|
if ( domainHash !== 0 ) {
|
2018-10-17 16:52:34 +02:00
|
|
|
filterDB.retrieve(domainHash, hostname, toRemoveArray);
|
2018-09-09 14:10:09 +02:00
|
|
|
}
|
2018-12-13 18:30:54 +01:00
|
|
|
const entity = details.entity;
|
|
|
|
const entityHash = µb.staticExtFilteringEngine.makeHash(entity);
|
2018-09-09 14:10:09 +02:00
|
|
|
if ( entityHash !== 0 ) {
|
2018-10-17 16:52:34 +02:00
|
|
|
filterDB.retrieve(entityHash, entity, toRemoveArray);
|
2017-12-28 19:49:02 +01:00
|
|
|
}
|
2018-10-17 16:52:34 +02:00
|
|
|
filterDB.retrieve(0, hostname, toRemoveArray);
|
|
|
|
if ( toRemoveArray.length === 0 ) { return; }
|
2017-12-28 19:49:02 +01:00
|
|
|
|
2018-10-17 16:52:34 +02:00
|
|
|
let notToRemoveArray = [];
|
|
|
|
if ( domainHash !== 0 ) {
|
|
|
|
filterDB.retrieve(domainHash | 0b0001, hostname, notToRemoveArray);
|
|
|
|
}
|
|
|
|
if ( entityHash !== 0 ) {
|
|
|
|
filterDB.retrieve(entityHash | 0b0001, entity, notToRemoveArray);
|
|
|
|
}
|
|
|
|
filterDB.retrieve(0 | 0b0001, hostname, notToRemoveArray);
|
|
|
|
if ( notToRemoveArray.length === 0 ) {
|
|
|
|
return toRemoveArray;
|
|
|
|
}
|
2017-12-28 19:49:02 +01:00
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const toRemoveMap = new Map();
|
|
|
|
for ( const entry of toRemoveArray ) {
|
2018-10-17 16:52:34 +02:00
|
|
|
toRemoveMap.set(entry.selector, entry);
|
|
|
|
}
|
2018-12-13 18:30:54 +01:00
|
|
|
for ( const entry of notToRemoveArray ) {
|
2018-10-17 16:52:34 +02:00
|
|
|
if ( toRemoveMap.has(entry.selector) === false ) { continue; }
|
|
|
|
toRemoveMap.delete(entry.selector);
|
2018-12-13 18:30:54 +01:00
|
|
|
if ( µb.logger.enabled === false ) { continue; }
|
2018-10-17 16:52:34 +02:00
|
|
|
let selector = entry.selector;
|
|
|
|
if ( entry.type === 65 ) {
|
|
|
|
selector = JSON.parse(selector).raw;
|
|
|
|
}
|
|
|
|
logOne(details, 1, selector);
|
2017-12-28 19:49:02 +01:00
|
|
|
}
|
2018-10-17 16:52:34 +02:00
|
|
|
|
|
|
|
if ( toRemoveMap.size === 0 ) { return; }
|
2018-11-18 11:56:13 +01:00
|
|
|
return Array.from(toRemoveMap.values());
|
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;
|
2018-12-15 16:46:17 +01:00
|
|
|
for ( const entry of details.selectors ) {
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( entry.type === 64 ) {
|
|
|
|
if ( applyCSSSelector(details, entry.selector) ) {
|
|
|
|
modified = true;
|
|
|
|
}
|
2018-10-17 16:52:34 +02:00
|
|
|
} else /* if ( entry.type === 65 ) */ {
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( applyProceduralSelector(details, entry.selector) ) {
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
filterDB = new µb.staticExtFilteringEngine.HostnameBasedDB(selfie);
|
|
|
|
pselectors.clear();
|
|
|
|
};
|
|
|
|
|
|
|
|
return api;
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|