2021-02-17 15:12:00 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
|
|
|
Copyright (C) 2014-present Raymond Hill
|
|
|
|
|
|
|
|
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';
|
|
|
|
|
|
|
|
if (
|
|
|
|
typeof vAPI === 'object' &&
|
|
|
|
typeof vAPI.DOMProceduralFilterer !== 'object'
|
|
|
|
) {
|
|
|
|
// >>>>>>>> start of local scope
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-02-11 18:28:15 +01:00
|
|
|
const nonVisualElements = {
|
|
|
|
script: true,
|
|
|
|
style: true,
|
|
|
|
};
|
2021-02-17 15:12:00 +01:00
|
|
|
|
2022-12-02 21:43:04 +01:00
|
|
|
const regexFromString = (s, exact = false) => {
|
|
|
|
if ( s === '' ) { return /^/; }
|
2023-01-02 03:58:14 +01:00
|
|
|
const match = /^\/(.+)\/([imu]*)$/.exec(s);
|
2022-12-02 21:43:04 +01:00
|
|
|
if ( match !== null ) {
|
|
|
|
return new RegExp(match[1], match[2] || undefined);
|
|
|
|
}
|
|
|
|
const reStr = s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
2022-12-20 20:34:54 +01:00
|
|
|
return new RegExp(exact ? `^${reStr}$` : reStr);
|
2022-12-02 21:43:04 +01:00
|
|
|
};
|
|
|
|
|
2021-02-17 15:12:00 +01:00
|
|
|
// 'P' stands for 'Procedural'
|
|
|
|
|
2022-02-11 18:28:15 +01:00
|
|
|
class PSelectorTask {
|
|
|
|
begin() {
|
|
|
|
}
|
|
|
|
end() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-26 22:46:24 +02:00
|
|
|
class PSelectorVoidTask extends PSelectorTask {
|
|
|
|
constructor(task) {
|
|
|
|
super();
|
|
|
|
console.info(`uBO: :${task[0]}() operator does not exist`);
|
|
|
|
}
|
|
|
|
transpose() {
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 18:28:15 +01:00
|
|
|
|
|
|
|
class PSelectorHasTextTask extends PSelectorTask {
|
2021-02-17 15:12:00 +01:00
|
|
|
constructor(task) {
|
2022-02-11 18:28:15 +01:00
|
|
|
super();
|
2022-12-10 17:18:24 +01:00
|
|
|
this.needle = regexFromString(task[1]);
|
2021-02-17 15:12:00 +01:00
|
|
|
}
|
|
|
|
transpose(node, output) {
|
|
|
|
if ( this.needle.test(node.textContent) ) {
|
|
|
|
output.push(node);
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 18:28:15 +01:00
|
|
|
}
|
2021-02-17 15:12:00 +01:00
|
|
|
|
2022-02-11 18:28:15 +01:00
|
|
|
class PSelectorIfTask extends PSelectorTask {
|
2021-02-17 15:12:00 +01:00
|
|
|
constructor(task) {
|
2022-02-11 18:28:15 +01:00
|
|
|
super();
|
2021-02-17 15:12:00 +01:00
|
|
|
this.pselector = new PSelector(task[1]);
|
|
|
|
}
|
|
|
|
transpose(node, output) {
|
|
|
|
if ( this.pselector.test(node) === this.target ) {
|
|
|
|
output.push(node);
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 18:28:15 +01:00
|
|
|
}
|
2021-02-17 15:12:00 +01:00
|
|
|
PSelectorIfTask.prototype.target = true;
|
|
|
|
|
2022-02-11 18:28:15 +01:00
|
|
|
class PSelectorIfNotTask extends PSelectorIfTask {
|
|
|
|
}
|
2021-02-17 15:12:00 +01:00
|
|
|
PSelectorIfNotTask.prototype.target = false;
|
|
|
|
|
2022-12-02 21:43:04 +01:00
|
|
|
class PSelectorMatchesAttrTask extends PSelectorTask {
|
|
|
|
constructor(task) {
|
|
|
|
super();
|
|
|
|
this.reAttr = regexFromString(task[1].attr, true);
|
|
|
|
this.reValue = regexFromString(task[1].value, true);
|
|
|
|
}
|
|
|
|
transpose(node, output) {
|
|
|
|
const attrs = node.getAttributeNames();
|
|
|
|
for ( const attr of attrs ) {
|
|
|
|
if ( this.reAttr.test(attr) === false ) { continue; }
|
|
|
|
if ( this.reValue.test(node.getAttribute(attr)) === false ) { continue; }
|
|
|
|
output.push(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 18:28:15 +01:00
|
|
|
class PSelectorMatchesCSSTask extends PSelectorTask {
|
2021-02-17 15:12:00 +01:00
|
|
|
constructor(task) {
|
2022-02-11 18:28:15 +01:00
|
|
|
super();
|
2021-02-17 15:12:00 +01:00
|
|
|
this.name = task[1].name;
|
2022-08-18 17:28:44 +02:00
|
|
|
this.pseudo = task[1].pseudo ? `::${task[1].pseudo}` : null;
|
2021-02-17 15:12:00 +01:00
|
|
|
let arg0 = task[1].value, arg1;
|
|
|
|
if ( Array.isArray(arg0) ) {
|
|
|
|
arg1 = arg0[1]; arg0 = arg0[0];
|
|
|
|
}
|
|
|
|
this.value = new RegExp(arg0, arg1);
|
|
|
|
}
|
|
|
|
transpose(node, output) {
|
|
|
|
const style = window.getComputedStyle(node, this.pseudo);
|
|
|
|
if ( style !== null && this.value.test(style[this.name]) ) {
|
|
|
|
output.push(node);
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 18:28:15 +01:00
|
|
|
}
|
2022-09-26 18:23:13 +02:00
|
|
|
class PSelectorMatchesCSSAfterTask extends PSelectorMatchesCSSTask {
|
|
|
|
constructor(task) {
|
|
|
|
super(task);
|
|
|
|
this.pseudo = '::after';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PSelectorMatchesCSSBeforeTask extends PSelectorMatchesCSSTask {
|
|
|
|
constructor(task) {
|
|
|
|
super(task);
|
|
|
|
this.pseudo = '::before';
|
|
|
|
}
|
|
|
|
}
|
2021-02-17 15:12:00 +01:00
|
|
|
|
2022-07-23 15:30:31 +02:00
|
|
|
class PSelectorMatchesMediaTask extends PSelectorTask {
|
2021-02-17 15:12:00 +01:00
|
|
|
constructor(task) {
|
2022-02-11 18:28:15 +01:00
|
|
|
super();
|
2022-07-23 15:30:31 +02:00
|
|
|
this.mql = window.matchMedia(task[1]);
|
|
|
|
if ( this.mql.media === 'not all' ) { return; }
|
|
|
|
this.mql.addEventListener('change', ( ) => {
|
|
|
|
if ( typeof vAPI !== 'object' ) { return; }
|
|
|
|
if ( vAPI === null ) { return; }
|
|
|
|
const filterer = vAPI.domFilterer && vAPI.domFilterer.proceduralFilterer;
|
|
|
|
if ( filterer instanceof Object === false ) { return; }
|
|
|
|
filterer.onDOMChanged([ null ]);
|
|
|
|
});
|
2021-02-17 15:12:00 +01:00
|
|
|
}
|
|
|
|
transpose(node, output) {
|
2022-07-23 15:30:31 +02:00
|
|
|
if ( this.mql.matches === false ) { return; }
|
|
|
|
output.push(node);
|
2021-02-17 15:12:00 +01:00
|
|
|
}
|
2022-02-11 18:28:15 +01:00
|
|
|
}
|
2021-02-17 15:12:00 +01:00
|
|
|
|
2022-02-11 18:28:15 +01:00
|
|
|
class PSelectorMatchesPathTask extends PSelectorTask {
|
2021-08-21 15:41:48 +02:00
|
|
|
constructor(task) {
|
2022-02-11 18:28:15 +01:00
|
|
|
super();
|
2022-12-10 17:18:24 +01:00
|
|
|
this.needle = regexFromString(task[1]);
|
2021-08-21 15:41:48 +02:00
|
|
|
}
|
|
|
|
transpose(node, output) {
|
2021-08-21 17:50:46 +02:00
|
|
|
if ( this.needle.test(self.location.pathname + self.location.search) ) {
|
2021-08-21 15:41:48 +02:00
|
|
|
output.push(node);
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 18:28:15 +01:00
|
|
|
}
|
|
|
|
|
2022-07-23 15:30:31 +02:00
|
|
|
class PSelectorMinTextLengthTask extends PSelectorTask {
|
|
|
|
constructor(task) {
|
|
|
|
super();
|
|
|
|
this.min = task[1];
|
|
|
|
}
|
|
|
|
transpose(node, output) {
|
|
|
|
if ( node.textContent.length >= this.min ) {
|
|
|
|
output.push(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 18:28:15 +01:00
|
|
|
class PSelectorOthersTask extends PSelectorTask {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.targets = new Set();
|
|
|
|
}
|
|
|
|
begin() {
|
|
|
|
this.targets.clear();
|
|
|
|
}
|
|
|
|
end(output) {
|
|
|
|
const toKeep = new Set(this.targets);
|
|
|
|
const toDiscard = new Set();
|
|
|
|
const body = document.body;
|
|
|
|
let discard = null;
|
|
|
|
for ( let keep of this.targets ) {
|
|
|
|
while ( keep !== null && keep !== body ) {
|
|
|
|
toKeep.add(keep);
|
|
|
|
toDiscard.delete(keep);
|
|
|
|
discard = keep.previousElementSibling;
|
|
|
|
while ( discard !== null ) {
|
|
|
|
if (
|
|
|
|
nonVisualElements[discard.localName] !== true &&
|
|
|
|
toKeep.has(discard) === false
|
|
|
|
) {
|
|
|
|
toDiscard.add(discard);
|
|
|
|
}
|
|
|
|
discard = discard.previousElementSibling;
|
|
|
|
}
|
|
|
|
discard = keep.nextElementSibling;
|
|
|
|
while ( discard !== null ) {
|
|
|
|
if (
|
|
|
|
nonVisualElements[discard.localName] !== true &&
|
|
|
|
toKeep.has(discard) === false
|
|
|
|
) {
|
|
|
|
toDiscard.add(discard);
|
|
|
|
}
|
|
|
|
discard = discard.nextElementSibling;
|
|
|
|
}
|
|
|
|
keep = keep.parentElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for ( discard of toDiscard ) {
|
|
|
|
output.push(discard);
|
|
|
|
}
|
|
|
|
this.targets.clear();
|
|
|
|
}
|
|
|
|
transpose(candidate) {
|
|
|
|
for ( const target of this.targets ) {
|
|
|
|
if ( target.contains(candidate) ) { return; }
|
|
|
|
if ( candidate.contains(target) ) {
|
|
|
|
this.targets.delete(target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.targets.add(candidate);
|
|
|
|
}
|
|
|
|
}
|
2021-08-21 15:41:48 +02:00
|
|
|
|
2021-07-23 16:11:07 +02:00
|
|
|
// https://github.com/AdguardTeam/ExtendedCss/issues/31#issuecomment-302391277
|
|
|
|
// Prepend `:scope ` if needed.
|
2022-02-11 18:28:15 +01:00
|
|
|
class PSelectorSpathTask extends PSelectorTask {
|
2021-02-17 15:12:00 +01:00
|
|
|
constructor(task) {
|
2022-02-11 18:28:15 +01:00
|
|
|
super();
|
2021-02-17 15:12:00 +01:00
|
|
|
this.spath = task[1];
|
|
|
|
this.nth = /^(?:\s*[+~]|:)/.test(this.spath);
|
|
|
|
if ( this.nth ) { return; }
|
|
|
|
if ( /^\s*>/.test(this.spath) ) {
|
|
|
|
this.spath = `:scope ${this.spath.trim()}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
transpose(node, output) {
|
2022-10-10 14:38:40 +02:00
|
|
|
const nodes = this.nth
|
|
|
|
? PSelectorSpathTask.qsa(node, this.spath)
|
|
|
|
: node.querySelectorAll(this.spath);
|
2021-02-17 15:12:00 +01:00
|
|
|
for ( const node of nodes ) {
|
|
|
|
output.push(node);
|
|
|
|
}
|
|
|
|
}
|
New cosmetic filter parser using CSSTree library
The new parser no longer uses the browser DOM to validate
that a cosmetic filter is valid or not, this is now done
through a JS library, CSSTree.
This means filter list authors will have to be more careful
to ensure that a cosmetic filter is really valid, as there is
no more guarantee that a cosmetic filter which works for a
given browser/version will still work properly on another
browser, or different version of the same browser.
This change has become necessary because of many reasons,
one of them being the flakiness of the previous parser as
exposed by many issues lately:
- https://github.com/uBlockOrigin/uBlock-issues/issues/2262
- https://github.com/uBlockOrigin/uBlock-issues/issues/2228
The new parser introduces breaking changes, there was no way
to do otherwise. Some current procedural cosmetic filters will
be shown as invalid with this change. This occurs because the
CSSTree library gets confused with some syntax which was
previously allowed by the previous parser because it was more
permissive.
Mainly the issue is with the arguments passed to some procedural
cosmetic filters, and these issues can be solved as follow:
Use quotes around the argument. You can use either single or
double-quotes, whichever is most convenient. If your argument
contains a single quote, use double-quotes, and vice versa.
Additionally, try to escape a quote inside an argument using
backslash. THis may work, but if not, use quotes around the
argument.
When the parser encounter quotes around an argument, it will
discard them before trying to process the argument, same with
escaped quotes inside the argument. Examples:
Breakage:
...##^script:has-text(toscr')
Fix:
...##^script:has-text(toscr\')
Breakage:
...##:xpath(//*[contains(text(),"VPN")]):upward(2)
Fix:
...##:xpath('//*[contains(text(),"VPN")]'):upward(2)
There are not many filters which break in the default set of
filter lists, so this should be workable for default lists.
Unfortunately those fixes will break the filter for previous
versions of uBO since these to not deal with quoted argument.
In such case, it may be necessary to keep the previous filter,
which will be discarded as broken on newer version of uBO.
THis was a necessary change as the old parser was becoming
more and more flaky after being constantly patched for new
cases arising, The new parser should be far more robust and
stay robist through expanding procedural cosmetic filter
syntax.
Additionally, in the MV3 version, filters are pre-compiled
using a Nodejs script, i.e. outside the browser, so validating
cosmetic filters using a live DOM no longer made sense.
This new parser will have to be tested throughly before stable
release.
2022-09-23 22:03:13 +02:00
|
|
|
// Helper method for other operators.
|
|
|
|
static qsa(node, selector) {
|
|
|
|
const parent = node.parentElement;
|
|
|
|
if ( parent === null ) { return []; }
|
|
|
|
let pos = 1;
|
|
|
|
for (;;) {
|
|
|
|
node = node.previousElementSibling;
|
|
|
|
if ( node === null ) { break; }
|
|
|
|
pos += 1;
|
|
|
|
}
|
|
|
|
return parent.querySelectorAll(
|
|
|
|
`:scope > :nth-child(${pos})${selector}`
|
|
|
|
);
|
|
|
|
}
|
2022-02-11 18:28:15 +01:00
|
|
|
}
|
2021-02-17 15:12:00 +01:00
|
|
|
|
2022-02-11 18:28:15 +01:00
|
|
|
class PSelectorUpwardTask extends PSelectorTask {
|
2021-02-17 15:12:00 +01:00
|
|
|
constructor(task) {
|
2022-02-11 18:28:15 +01:00
|
|
|
super();
|
2021-02-17 15:12:00 +01:00
|
|
|
const arg = task[1];
|
|
|
|
if ( typeof arg === 'number' ) {
|
|
|
|
this.i = arg;
|
|
|
|
} else {
|
|
|
|
this.s = arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
transpose(node, output) {
|
|
|
|
if ( this.s !== '' ) {
|
|
|
|
const parent = node.parentElement;
|
|
|
|
if ( parent === null ) { return; }
|
|
|
|
node = parent.closest(this.s);
|
|
|
|
if ( node === null ) { return; }
|
|
|
|
} else {
|
|
|
|
let nth = this.i;
|
|
|
|
for (;;) {
|
|
|
|
node = node.parentElement;
|
|
|
|
if ( node === null ) { return; }
|
|
|
|
nth -= 1;
|
|
|
|
if ( nth === 0 ) { break; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output.push(node);
|
|
|
|
}
|
2022-02-11 18:28:15 +01:00
|
|
|
}
|
2021-02-17 15:12:00 +01:00
|
|
|
PSelectorUpwardTask.prototype.i = 0;
|
|
|
|
PSelectorUpwardTask.prototype.s = '';
|
|
|
|
|
2022-02-11 18:28:15 +01:00
|
|
|
class PSelectorWatchAttrs extends PSelectorTask {
|
2021-02-17 15:12:00 +01:00
|
|
|
constructor(task) {
|
2022-02-11 18:28:15 +01:00
|
|
|
super();
|
2021-02-17 15:12:00 +01:00
|
|
|
this.observer = null;
|
|
|
|
this.observed = new WeakSet();
|
|
|
|
this.observerOptions = {
|
|
|
|
attributes: true,
|
|
|
|
subtree: true,
|
|
|
|
};
|
|
|
|
const attrs = task[1];
|
|
|
|
if ( Array.isArray(attrs) && attrs.length !== 0 ) {
|
|
|
|
this.observerOptions.attributeFilter = task[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: Is it worth trying to re-apply only the current selector?
|
|
|
|
handler() {
|
|
|
|
const filterer =
|
|
|
|
vAPI.domFilterer && vAPI.domFilterer.proceduralFilterer;
|
|
|
|
if ( filterer instanceof Object ) {
|
|
|
|
filterer.onDOMChanged([ null ]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
transpose(node, output) {
|
|
|
|
output.push(node);
|
|
|
|
if ( this.observed.has(node) ) { return; }
|
|
|
|
if ( this.observer === null ) {
|
|
|
|
this.observer = new MutationObserver(this.handler);
|
|
|
|
}
|
|
|
|
this.observer.observe(node, this.observerOptions);
|
|
|
|
this.observed.add(node);
|
|
|
|
}
|
2022-02-11 18:28:15 +01:00
|
|
|
}
|
2021-02-17 15:12:00 +01:00
|
|
|
|
2022-02-11 18:28:15 +01:00
|
|
|
class PSelectorXpathTask extends PSelectorTask {
|
2021-02-17 15:12:00 +01:00
|
|
|
constructor(task) {
|
2022-02-11 18:28:15 +01:00
|
|
|
super();
|
2021-02-17 15:12:00 +01:00
|
|
|
this.xpe = document.createExpression(task[1], null);
|
|
|
|
this.xpr = null;
|
|
|
|
}
|
|
|
|
transpose(node, output) {
|
|
|
|
this.xpr = this.xpe.evaluate(
|
|
|
|
node,
|
|
|
|
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
|
|
|
|
this.xpr
|
|
|
|
);
|
|
|
|
let j = this.xpr.snapshotLength;
|
|
|
|
while ( j-- ) {
|
|
|
|
const node = this.xpr.snapshotItem(j);
|
|
|
|
if ( node.nodeType === 1 ) {
|
|
|
|
output.push(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 18:28:15 +01:00
|
|
|
}
|
2021-02-17 15:12:00 +01:00
|
|
|
|
2022-02-11 18:28:15 +01:00
|
|
|
class PSelector {
|
2021-02-17 15:12:00 +01:00
|
|
|
constructor(o) {
|
|
|
|
this.raw = o.raw;
|
2022-02-11 21:13:25 +01:00
|
|
|
this.selector = o.selector;
|
2021-02-17 15:12:00 +01:00
|
|
|
this.tasks = [];
|
2022-02-11 18:28:15 +01:00
|
|
|
const tasks = [];
|
|
|
|
if ( Array.isArray(o.tasks) === false ) { return; }
|
|
|
|
for ( const task of o.tasks ) {
|
2022-09-26 22:46:24 +02:00
|
|
|
const ctor = this.operatorToTaskMap.get(task[0]) || PSelectorVoidTask;
|
2022-02-11 18:28:15 +01:00
|
|
|
tasks.push(new ctor(task));
|
2021-02-17 15:12:00 +01:00
|
|
|
}
|
2022-02-11 18:28:15 +01:00
|
|
|
this.tasks = tasks;
|
2021-02-17 15:12:00 +01:00
|
|
|
}
|
|
|
|
prime(input) {
|
|
|
|
const root = input || document;
|
|
|
|
if ( this.selector === '' ) { return [ root ]; }
|
2022-10-10 14:38:40 +02:00
|
|
|
if ( input !== document && /^ ?[>+~]/.test(this.selector) ) {
|
New cosmetic filter parser using CSSTree library
The new parser no longer uses the browser DOM to validate
that a cosmetic filter is valid or not, this is now done
through a JS library, CSSTree.
This means filter list authors will have to be more careful
to ensure that a cosmetic filter is really valid, as there is
no more guarantee that a cosmetic filter which works for a
given browser/version will still work properly on another
browser, or different version of the same browser.
This change has become necessary because of many reasons,
one of them being the flakiness of the previous parser as
exposed by many issues lately:
- https://github.com/uBlockOrigin/uBlock-issues/issues/2262
- https://github.com/uBlockOrigin/uBlock-issues/issues/2228
The new parser introduces breaking changes, there was no way
to do otherwise. Some current procedural cosmetic filters will
be shown as invalid with this change. This occurs because the
CSSTree library gets confused with some syntax which was
previously allowed by the previous parser because it was more
permissive.
Mainly the issue is with the arguments passed to some procedural
cosmetic filters, and these issues can be solved as follow:
Use quotes around the argument. You can use either single or
double-quotes, whichever is most convenient. If your argument
contains a single quote, use double-quotes, and vice versa.
Additionally, try to escape a quote inside an argument using
backslash. THis may work, but if not, use quotes around the
argument.
When the parser encounter quotes around an argument, it will
discard them before trying to process the argument, same with
escaped quotes inside the argument. Examples:
Breakage:
...##^script:has-text(toscr')
Fix:
...##^script:has-text(toscr\')
Breakage:
...##:xpath(//*[contains(text(),"VPN")]):upward(2)
Fix:
...##:xpath('//*[contains(text(),"VPN")]'):upward(2)
There are not many filters which break in the default set of
filter lists, so this should be workable for default lists.
Unfortunately those fixes will break the filter for previous
versions of uBO since these to not deal with quoted argument.
In such case, it may be necessary to keep the previous filter,
which will be discarded as broken on newer version of uBO.
THis was a necessary change as the old parser was becoming
more and more flaky after being constantly patched for new
cases arising, The new parser should be far more robust and
stay robist through expanding procedural cosmetic filter
syntax.
Additionally, in the MV3 version, filters are pre-compiled
using a Nodejs script, i.e. outside the browser, so validating
cosmetic filters using a live DOM no longer made sense.
This new parser will have to be tested throughly before stable
release.
2022-09-23 22:03:13 +02:00
|
|
|
return Array.from(PSelectorSpathTask.qsa(input, this.selector));
|
|
|
|
}
|
2022-10-10 14:38:40 +02:00
|
|
|
return Array.from(root.querySelectorAll(this.selector));
|
2021-02-17 15:12:00 +01:00
|
|
|
}
|
|
|
|
exec(input) {
|
|
|
|
let nodes = this.prime(input);
|
|
|
|
for ( const task of this.tasks ) {
|
|
|
|
if ( nodes.length === 0 ) { break; }
|
|
|
|
const transposed = [];
|
2022-02-11 18:28:15 +01:00
|
|
|
task.begin();
|
2021-02-17 15:12:00 +01:00
|
|
|
for ( const node of nodes ) {
|
|
|
|
task.transpose(node, transposed);
|
|
|
|
}
|
2022-02-11 18:28:15 +01:00
|
|
|
task.end(transposed);
|
2021-02-17 15:12:00 +01:00
|
|
|
nodes = transposed;
|
|
|
|
}
|
|
|
|
return nodes;
|
|
|
|
}
|
|
|
|
test(input) {
|
|
|
|
const nodes = this.prime(input);
|
|
|
|
for ( const node of nodes ) {
|
|
|
|
let output = [ node ];
|
|
|
|
for ( const task of this.tasks ) {
|
|
|
|
const transposed = [];
|
2022-02-11 18:28:15 +01:00
|
|
|
task.begin();
|
2021-02-17 15:12:00 +01:00
|
|
|
for ( const node of output ) {
|
|
|
|
task.transpose(node, transposed);
|
|
|
|
}
|
2022-02-11 18:28:15 +01:00
|
|
|
task.end(transposed);
|
2021-02-17 15:12:00 +01:00
|
|
|
output = transposed;
|
|
|
|
if ( output.length === 0 ) { break; }
|
|
|
|
}
|
|
|
|
if ( output.length !== 0 ) { return true; }
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2022-02-11 18:28:15 +01:00
|
|
|
}
|
2022-12-13 16:23:51 +01:00
|
|
|
PSelector.prototype.operatorToTaskMap = new Map([
|
|
|
|
[ 'has', PSelectorIfTask ],
|
|
|
|
[ 'has-text', PSelectorHasTextTask ],
|
|
|
|
[ 'if', PSelectorIfTask ],
|
|
|
|
[ 'if-not', PSelectorIfNotTask ],
|
|
|
|
[ 'matches-attr', PSelectorMatchesAttrTask ],
|
|
|
|
[ 'matches-css', PSelectorMatchesCSSTask ],
|
|
|
|
[ 'matches-css-after', PSelectorMatchesCSSAfterTask ],
|
|
|
|
[ 'matches-css-before', PSelectorMatchesCSSBeforeTask ],
|
|
|
|
[ 'matches-media', PSelectorMatchesMediaTask ],
|
|
|
|
[ 'matches-path', PSelectorMatchesPathTask ],
|
|
|
|
[ 'min-text-length', PSelectorMinTextLengthTask ],
|
|
|
|
[ 'not', PSelectorIfNotTask ],
|
|
|
|
[ 'others', PSelectorOthersTask ],
|
|
|
|
[ 'spath', PSelectorSpathTask ],
|
|
|
|
[ 'upward', PSelectorUpwardTask ],
|
|
|
|
[ 'watch-attr', PSelectorWatchAttrs ],
|
|
|
|
[ 'xpath', PSelectorXpathTask ],
|
|
|
|
]);
|
2021-02-17 15:12:00 +01:00
|
|
|
|
2022-02-11 18:28:15 +01:00
|
|
|
class PSelectorRoot extends PSelector {
|
2022-12-10 17:18:24 +01:00
|
|
|
constructor(o) {
|
2021-02-17 15:12:00 +01:00
|
|
|
super(o);
|
|
|
|
this.budget = 200; // I arbitrary picked a 1/5 second
|
|
|
|
this.raw = o.raw;
|
|
|
|
this.cost = 0;
|
|
|
|
this.lastAllowanceTime = 0;
|
2022-12-10 17:18:24 +01:00
|
|
|
this.action = o.action;
|
2021-02-17 15:12:00 +01:00
|
|
|
}
|
2022-07-24 17:41:08 +02:00
|
|
|
prime(input) {
|
|
|
|
try {
|
|
|
|
return super.prime(input);
|
|
|
|
} catch (ex) {
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
2022-02-11 18:28:15 +01:00
|
|
|
}
|
2021-02-17 15:12:00 +01:00
|
|
|
PSelectorRoot.prototype.hit = false;
|
|
|
|
|
2022-02-11 18:28:15 +01:00
|
|
|
class ProceduralFilterer {
|
2021-02-17 15:12:00 +01:00
|
|
|
constructor(domFilterer) {
|
|
|
|
this.domFilterer = domFilterer;
|
|
|
|
this.mustApplySelectors = false;
|
|
|
|
this.selectors = new Map();
|
|
|
|
this.masterToken = vAPI.randomToken();
|
|
|
|
this.styleTokenMap = new Map();
|
|
|
|
this.styledNodes = new Set();
|
|
|
|
if ( vAPI.domWatcher instanceof Object ) {
|
|
|
|
vAPI.domWatcher.addListener(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addProceduralSelectors(selectors) {
|
|
|
|
const addedSelectors = [];
|
2022-11-12 17:22:49 +01:00
|
|
|
let mustCommit = false;
|
2021-02-17 15:12:00 +01:00
|
|
|
for ( const selector of selectors ) {
|
|
|
|
if ( this.selectors.has(selector.raw) ) { continue; }
|
2022-12-10 17:18:24 +01:00
|
|
|
const pselector = new PSelectorRoot(selector);
|
|
|
|
this.primeProceduralSelector(pselector);
|
2021-02-17 15:12:00 +01:00
|
|
|
this.selectors.set(selector.raw, pselector);
|
|
|
|
addedSelectors.push(pselector);
|
|
|
|
mustCommit = true;
|
|
|
|
}
|
|
|
|
if ( mustCommit === false ) { return; }
|
|
|
|
this.mustApplySelectors = this.selectors.size !== 0;
|
|
|
|
this.domFilterer.commit();
|
|
|
|
if ( this.domFilterer.hasListeners() ) {
|
|
|
|
this.domFilterer.triggerListeners({
|
|
|
|
procedural: addedSelectors
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-10 17:18:24 +01:00
|
|
|
// This allows to perform potentially expensive initialization steps
|
|
|
|
// before the filters are ready to be applied.
|
|
|
|
primeProceduralSelector(pselector) {
|
|
|
|
if ( pselector.action === undefined ) {
|
|
|
|
this.styleTokenFromStyle(vAPI.hideStyle);
|
|
|
|
} else if ( pselector.action[0] === 'style' ) {
|
|
|
|
this.styleTokenFromStyle(pselector.action[1]);
|
|
|
|
}
|
|
|
|
return pselector;
|
|
|
|
}
|
|
|
|
|
2021-02-17 15:12:00 +01:00
|
|
|
commitNow() {
|
2022-11-12 17:22:49 +01:00
|
|
|
if ( this.selectors.size === 0 ) { return; }
|
2021-02-17 15:12:00 +01:00
|
|
|
|
|
|
|
this.mustApplySelectors = false;
|
|
|
|
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/341
|
|
|
|
// Be ready to unhide nodes which no longer matches any of
|
|
|
|
// the procedural selectors.
|
|
|
|
const toUnstyle = this.styledNodes;
|
|
|
|
this.styledNodes = new Set();
|
|
|
|
|
|
|
|
let t0 = Date.now();
|
|
|
|
|
|
|
|
for ( const pselector of this.selectors.values() ) {
|
|
|
|
const allowance = Math.floor((t0 - pselector.lastAllowanceTime) / 2000);
|
|
|
|
if ( allowance >= 1 ) {
|
|
|
|
pselector.budget += allowance * 50;
|
|
|
|
if ( pselector.budget > 200 ) { pselector.budget = 200; }
|
|
|
|
pselector.lastAllowanceTime = t0;
|
|
|
|
}
|
|
|
|
if ( pselector.budget <= 0 ) { continue; }
|
|
|
|
const nodes = pselector.exec();
|
|
|
|
const t1 = Date.now();
|
|
|
|
pselector.budget += t0 - t1;
|
|
|
|
if ( pselector.budget < -500 ) {
|
|
|
|
console.info('uBO: disabling %s', pselector.raw);
|
|
|
|
pselector.budget = -0x7FFFFFFF;
|
|
|
|
}
|
|
|
|
t0 = t1;
|
|
|
|
if ( nodes.length === 0 ) { continue; }
|
|
|
|
pselector.hit = true;
|
2022-12-10 17:18:24 +01:00
|
|
|
this.processNodes(nodes, pselector.action);
|
2021-02-17 15:12:00 +01:00
|
|
|
}
|
|
|
|
|
2022-12-10 17:18:24 +01:00
|
|
|
this.unprocessNodes(toUnstyle);
|
2021-02-17 15:12:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
styleTokenFromStyle(style) {
|
|
|
|
if ( style === undefined ) { return; }
|
2022-12-12 20:02:57 +01:00
|
|
|
let styleToken = this.styleTokenMap.get(style);
|
2021-02-17 15:12:00 +01:00
|
|
|
if ( styleToken !== undefined ) { return styleToken; }
|
|
|
|
styleToken = vAPI.randomToken();
|
|
|
|
this.styleTokenMap.set(style, styleToken);
|
|
|
|
this.domFilterer.addCSS(
|
|
|
|
`[${this.masterToken}][${styleToken}]\n{${style}}`,
|
|
|
|
{ silent: true, mustInject: true }
|
|
|
|
);
|
|
|
|
return styleToken;
|
|
|
|
}
|
|
|
|
|
2022-12-10 17:18:24 +01:00
|
|
|
processNodes(nodes, action) {
|
|
|
|
const op = action && action[0] || '';
|
|
|
|
const arg = op !== '' ? action[1] : '';
|
|
|
|
switch ( op ) {
|
|
|
|
case '':
|
|
|
|
/* fall through */
|
|
|
|
case 'style': {
|
|
|
|
const styleToken = this.styleTokenFromStyle(
|
|
|
|
arg === '' ? vAPI.hideStyle : arg
|
|
|
|
);
|
|
|
|
for ( const node of nodes ) {
|
|
|
|
node.setAttribute(this.masterToken, '');
|
|
|
|
node.setAttribute(styleToken, '');
|
|
|
|
this.styledNodes.add(node);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'remove': {
|
2021-02-17 15:12:00 +01:00
|
|
|
for ( const node of nodes ) {
|
|
|
|
node.remove();
|
2022-12-10 17:18:24 +01:00
|
|
|
node.textContent = '';
|
2021-02-17 15:12:00 +01:00
|
|
|
}
|
2022-12-10 17:18:24 +01:00
|
|
|
break;
|
2021-02-17 15:12:00 +01:00
|
|
|
}
|
2022-12-10 17:18:24 +01:00
|
|
|
case 'remove-attr': {
|
|
|
|
const reAttr = regexFromString(arg, true);
|
|
|
|
for ( const node of nodes ) {
|
|
|
|
for ( const name of node.getAttributeNames() ) {
|
|
|
|
if ( reAttr.test(name) === false ) { continue; }
|
|
|
|
node.removeAttribute(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'remove-class': {
|
|
|
|
const reClass = regexFromString(arg, true);
|
|
|
|
for ( const node of nodes ) {
|
|
|
|
const cl = node.classList;
|
|
|
|
for ( const name of cl.values() ) {
|
|
|
|
if ( reClass.test(name) === false ) { continue; }
|
|
|
|
cl.remove(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2021-02-17 15:12:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Current assumption is one style per hit element. Could be an
|
|
|
|
// issue if an element has multiple styling and one styling is
|
2022-07-24 17:41:08 +02:00
|
|
|
// brought back. Possibly too rare to care about this for now.
|
2022-12-10 17:18:24 +01:00
|
|
|
unprocessNodes(nodes) {
|
2021-02-17 15:12:00 +01:00
|
|
|
for ( const node of nodes ) {
|
|
|
|
if ( this.styledNodes.has(node) ) { continue; }
|
|
|
|
node.removeAttribute(this.masterToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
createProceduralFilter(o) {
|
2022-12-10 17:18:24 +01:00
|
|
|
return this.primeProceduralSelector(
|
|
|
|
new PSelectorRoot(typeof o === 'string' ? JSON.parse(o) : o)
|
|
|
|
);
|
2021-02-17 15:12:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
onDOMCreated() {
|
|
|
|
}
|
|
|
|
|
|
|
|
onDOMChanged(addedNodes, removedNodes) {
|
|
|
|
if ( this.selectors.size === 0 ) { return; }
|
|
|
|
this.mustApplySelectors =
|
|
|
|
this.mustApplySelectors ||
|
|
|
|
addedNodes.length !== 0 ||
|
|
|
|
removedNodes;
|
|
|
|
this.domFilterer.commit();
|
|
|
|
}
|
2022-02-11 18:28:15 +01:00
|
|
|
}
|
2021-02-17 15:12:00 +01:00
|
|
|
|
|
|
|
vAPI.DOMProceduralFilterer = ProceduralFilterer;
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// >>>>>>>> end of local scope
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
DO NOT:
|
|
|
|
- Remove the following code
|
|
|
|
- Add code beyond the following code
|
|
|
|
Reason:
|
|
|
|
- https://github.com/gorhill/uBlock/pull/3721
|
|
|
|
- uBO never uses the return value from injected content scripts
|
|
|
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
void 0;
|