2014-06-24 00:42:43 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-12-26 16:45:19 +01:00
|
|
|
Copyright (C) 2014-present Raymond Hill
|
2014-06-24 00:42:43 +02: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
|
|
|
|
*/
|
|
|
|
|
2016-06-28 01:09:04 +02:00
|
|
|
'use strict';
|
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
+--> domCollapser
|
|
|
|
|
|
|
|
|
|
|
|
|
|
domWatcher--+
|
|
|
|
| +-- domSurveyor
|
|
|
|
| |
|
|
|
|
+--> domFilterer --+-- domLogger
|
|
|
|
|
|
|
|
|
+-- domInspector
|
2016-08-12 14:55:35 +02:00
|
|
|
|
|
|
|
domWatcher:
|
|
|
|
Watches for changes in the DOM, and notify the other components about these
|
|
|
|
changes.
|
|
|
|
|
|
|
|
domCollapser:
|
|
|
|
Enforces the collapsing of DOM elements for which a corresponding
|
|
|
|
resource was blocked through network filtering.
|
|
|
|
|
|
|
|
domFilterer:
|
|
|
|
Enforces the filtering of DOM elements, by feeding it cosmetic filters.
|
|
|
|
|
|
|
|
domSurveyor:
|
|
|
|
Surveys the DOM to find new cosmetic filters to apply to the current page.
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
domLogger:
|
|
|
|
Surveys the page to find and report the injected cosmetic filters blocking
|
|
|
|
actual elements on the current page. This component is dynamically loaded
|
|
|
|
IF AND ONLY IF uBO's logger is opened.
|
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
If page is whitelisted:
|
|
|
|
- domWatcher: off
|
|
|
|
- domCollapser: off
|
|
|
|
- domFilterer: off
|
|
|
|
- domSurveyor: off
|
2017-10-21 19:43:46 +02:00
|
|
|
- domLogger: off
|
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
I verified that the code in this file is completely flushed out of memory
|
|
|
|
when a page is whitelisted.
|
|
|
|
|
|
|
|
If cosmetic filtering is disabled:
|
|
|
|
- domWatcher: on
|
|
|
|
- domCollapser: on
|
|
|
|
- domFilterer: off
|
|
|
|
- domSurveyor: off
|
2017-10-21 19:43:46 +02:00
|
|
|
- domLogger: off
|
2016-08-12 14:55:35 +02:00
|
|
|
|
|
|
|
If generic cosmetic filtering is disabled:
|
|
|
|
- domWatcher: on
|
|
|
|
- domCollapser: on
|
|
|
|
- domFilterer: on
|
|
|
|
- domSurveyor: off
|
2017-10-21 19:43:46 +02:00
|
|
|
- domLogger: on if uBO logger is opened
|
|
|
|
|
|
|
|
If generic cosmetic filtering is enabled:
|
|
|
|
- domWatcher: on
|
|
|
|
- domCollapser: on
|
|
|
|
- domFilterer: on
|
|
|
|
- domSurveyor: on
|
|
|
|
- domLogger: on if uBO logger is opened
|
2016-08-12 14:55:35 +02:00
|
|
|
|
|
|
|
Additionally, the domSurveyor can turn itself off once it decides that
|
|
|
|
it has become pointless (repeatedly not finding new cosmetic filters).
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
The domFilterer makes use of platform-dependent user stylesheets[1].
|
|
|
|
|
2016-12-16 22:25:36 +01:00
|
|
|
At time of writing, only modern Firefox provides a custom implementation,
|
|
|
|
which makes for solid, reliable and low overhead cosmetic filtering on
|
|
|
|
Firefox.
|
2017-10-21 19:43:46 +02:00
|
|
|
|
2016-12-16 22:25:36 +01:00
|
|
|
The generic implementation[2] performs as best as can be, but won't ever be
|
2017-10-21 19:43:46 +02:00
|
|
|
as reliable and accurate as real user stylesheets.
|
|
|
|
|
|
|
|
[1] "user stylesheets" refer to local CSS rules which have priority over,
|
|
|
|
and can't be overriden by a web page's own CSS rules.
|
2016-12-16 22:25:36 +01:00
|
|
|
[2] below, see platformUserCSS / platformHideNode / platformUnhideNode
|
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
*/
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2017-08-17 14:25:02 +02:00
|
|
|
// Abort execution if our global vAPI object does not exist.
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/456
|
|
|
|
// https://github.com/gorhill/uBlock/issues/2029
|
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
// >>>>>>>> start of HUGE-IF-BLOCK
|
|
|
|
if ( typeof vAPI === 'object' && !vAPI.contentScript ) {
|
2017-10-27 20:22:45 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
vAPI.contentScript = true;
|
2017-08-17 14:25:02 +02:00
|
|
|
|
2016-06-28 01:09:04 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
2017-10-24 22:38:51 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
The purpose of SafeAnimationFrame is to take advantage of the behavior of
|
|
|
|
window.requestAnimationFrame[1]. If we use an animation frame as a timer,
|
|
|
|
then this timer is described as follow:
|
|
|
|
|
|
|
|
- time events are throttled by the browser when the viewport is not visible --
|
|
|
|
there is no point for uBO to play with the DOM if the document is not
|
|
|
|
visible.
|
|
|
|
- time events are micro tasks[2].
|
|
|
|
- time events are synchronized to monitor refresh, meaning that they can fire
|
|
|
|
at most 1/60 (typically).
|
|
|
|
|
|
|
|
If a delay value is provided, a plain timer is first used. Plain timers are
|
|
|
|
macro-tasks, so this is good when uBO wants to yield to more important tasks
|
|
|
|
on a page. Once the plain timer elapse, an animation frame is used to trigger
|
|
|
|
the next time at which to execute the job.
|
|
|
|
|
|
|
|
[1] https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame
|
|
|
|
[2] https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/
|
|
|
|
|
|
|
|
*/
|
2016-06-28 01:09:04 +02:00
|
|
|
|
2016-11-12 19:38:41 +01:00
|
|
|
// https://github.com/gorhill/uBlock/issues/2147
|
|
|
|
|
|
|
|
vAPI.SafeAnimationFrame = function(callback) {
|
2018-12-31 17:50:40 +01:00
|
|
|
this.fid = this.tid = undefined;
|
2016-11-12 19:38:41 +01:00
|
|
|
this.callback = callback;
|
|
|
|
};
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
vAPI.SafeAnimationFrame.prototype = {
|
|
|
|
start: function(delay) {
|
2019-09-29 18:26:58 +02:00
|
|
|
if ( vAPI instanceof Object === false ) { return; }
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( delay === undefined ) {
|
2018-12-31 17:50:40 +01:00
|
|
|
if ( this.fid === undefined ) {
|
|
|
|
this.fid = requestAnimationFrame(( ) => { this.onRAF(); } );
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
2018-12-31 17:50:40 +01:00
|
|
|
if ( this.tid === undefined ) {
|
|
|
|
this.tid = vAPI.setTimeout(( ) => { this.onSTO(); }, 20000);
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
2017-10-24 22:38:51 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-12-31 17:50:40 +01:00
|
|
|
if ( this.fid === undefined && this.tid === undefined ) {
|
|
|
|
this.tid = vAPI.setTimeout(( ) => { this.macroToMicro(); }, delay);
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
clear: function() {
|
2018-12-31 17:50:40 +01:00
|
|
|
if ( this.fid !== undefined ) {
|
|
|
|
cancelAnimationFrame(this.fid);
|
|
|
|
this.fid = undefined;
|
|
|
|
}
|
|
|
|
if ( this.tid !== undefined ) {
|
|
|
|
clearTimeout(this.tid);
|
|
|
|
this.tid = undefined;
|
|
|
|
}
|
2017-10-24 22:38:51 +02:00
|
|
|
},
|
|
|
|
macroToMicro: function() {
|
2018-12-31 17:50:40 +01:00
|
|
|
this.tid = undefined;
|
2017-10-24 22:38:51 +02:00
|
|
|
this.start();
|
2018-12-31 17:50:40 +01:00
|
|
|
},
|
|
|
|
onRAF: function() {
|
|
|
|
if ( this.tid !== undefined ) {
|
|
|
|
clearTimeout(this.tid);
|
|
|
|
this.tid = undefined;
|
|
|
|
}
|
|
|
|
this.fid = undefined;
|
|
|
|
this.callback();
|
|
|
|
},
|
|
|
|
onSTO: function() {
|
|
|
|
if ( this.fid !== undefined ) {
|
|
|
|
cancelAnimationFrame(this.fid);
|
|
|
|
this.fid = undefined;
|
|
|
|
}
|
|
|
|
this.tid = undefined;
|
|
|
|
this.callback();
|
|
|
|
},
|
2017-08-21 18:04:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-05-11 16:40:34 +02:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/552
|
|
|
|
// Listen and report CSP violations so that blocked resources through CSP
|
|
|
|
// are properly reported in the logger.
|
|
|
|
|
|
|
|
{
|
2019-06-24 17:40:14 +02:00
|
|
|
const newEvents = new Set();
|
|
|
|
const allEvents = new Set();
|
2019-05-11 16:40:34 +02:00
|
|
|
let timer;
|
|
|
|
|
|
|
|
const send = function() {
|
2019-09-17 21:15:01 +02:00
|
|
|
vAPI.messaging.send('scriptlets', {
|
|
|
|
what: 'securityPolicyViolation',
|
|
|
|
type: 'net',
|
|
|
|
docURL: document.location.href,
|
|
|
|
violations: Array.from(newEvents),
|
|
|
|
}).then(response => {
|
|
|
|
if ( response === true ) { return; }
|
|
|
|
stop();
|
|
|
|
});
|
2019-06-24 17:40:14 +02:00
|
|
|
for ( const event of newEvents ) {
|
|
|
|
allEvents.add(event);
|
|
|
|
}
|
|
|
|
newEvents.clear();
|
2019-05-11 16:40:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const sendAsync = function() {
|
|
|
|
if ( timer !== undefined ) { return; }
|
|
|
|
timer = self.requestIdleCallback(
|
|
|
|
( ) => { timer = undefined; send(); },
|
2019-09-17 21:15:01 +02:00
|
|
|
{ timeout: 2063 }
|
2019-05-11 16:40:34 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const listener = function(ev) {
|
|
|
|
if ( ev.isTrusted !== true ) { return; }
|
|
|
|
if ( ev.disposition !== 'enforce' ) { return; }
|
2019-06-24 17:40:14 +02:00
|
|
|
const json = JSON.stringify({
|
2019-05-11 16:40:34 +02:00
|
|
|
url: ev.blockedURL || ev.blockedURI,
|
|
|
|
policy: ev.originalPolicy,
|
|
|
|
directive: ev.effectiveDirective || ev.violatedDirective,
|
2019-06-24 17:40:14 +02:00
|
|
|
});
|
|
|
|
if ( allEvents.has(json) ) { return; }
|
|
|
|
newEvents.add(json);
|
2019-05-11 16:40:34 +02:00
|
|
|
sendAsync();
|
|
|
|
};
|
|
|
|
|
|
|
|
const stop = function() {
|
2019-06-24 17:40:14 +02:00
|
|
|
newEvents.clear();
|
|
|
|
allEvents.clear();
|
2019-05-11 16:40:34 +02:00
|
|
|
if ( timer !== undefined ) {
|
|
|
|
self.cancelIdleCallback(timer);
|
|
|
|
timer = undefined;
|
|
|
|
}
|
|
|
|
document.removeEventListener('securitypolicyviolation', listener);
|
|
|
|
vAPI.shutdown.remove(stop);
|
|
|
|
};
|
|
|
|
|
|
|
|
document.addEventListener('securitypolicyviolation', listener);
|
|
|
|
vAPI.shutdown.add(stop);
|
|
|
|
|
|
|
|
// We need to call at least once to find out whether we really need to
|
|
|
|
// listen to CSP violations.
|
|
|
|
sendAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
vAPI.domWatcher = (function() {
|
2017-07-23 15:56:43 +02:00
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const addedNodeLists = [];
|
|
|
|
const removedNodeLists = [];
|
|
|
|
const addedNodes = [];
|
|
|
|
const ignoreTags = new Set([ 'br', 'head', 'link', 'meta', 'script', 'style' ]);
|
|
|
|
const listeners = [];
|
|
|
|
|
|
|
|
let domIsReady = false,
|
2017-10-21 19:43:46 +02:00
|
|
|
domLayoutObserver,
|
|
|
|
listenerIterator = [], listenerIteratorDirty = false,
|
|
|
|
removedNodes = false,
|
|
|
|
safeObserverHandlerTimer;
|
2016-12-16 22:25:36 +01:00
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const safeObserverHandler = function() {
|
2017-10-23 18:21:37 +02:00
|
|
|
//console.time('dom watcher/safe observer handler');
|
2018-12-26 16:45:19 +01:00
|
|
|
let i = addedNodeLists.length,
|
|
|
|
j = addedNodes.length;
|
2017-10-21 19:43:46 +02:00
|
|
|
while ( i-- ) {
|
2018-12-26 16:45:19 +01:00
|
|
|
const nodeList = addedNodeLists[i];
|
|
|
|
let iNode = nodeList.length;
|
2017-10-21 19:43:46 +02:00
|
|
|
while ( iNode-- ) {
|
2018-12-26 16:45:19 +01:00
|
|
|
const node = nodeList[iNode];
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( node.nodeType !== 1 ) { continue; }
|
|
|
|
if ( ignoreTags.has(node.localName) ) { continue; }
|
|
|
|
if ( node.parentElement === null ) { continue; }
|
|
|
|
addedNodes[j++] = node;
|
2016-12-16 22:25:36 +01:00
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
|
|
|
addedNodeLists.length = 0;
|
|
|
|
i = removedNodeLists.length;
|
|
|
|
while ( i-- && removedNodes === false ) {
|
2018-12-26 16:45:19 +01:00
|
|
|
const nodeList = removedNodeLists[i];
|
|
|
|
let iNode = nodeList.length;
|
2017-10-21 19:43:46 +02:00
|
|
|
while ( iNode-- ) {
|
|
|
|
if ( nodeList[iNode].nodeType !== 1 ) { continue; }
|
|
|
|
removedNodes = true;
|
|
|
|
break;
|
2016-12-16 22:25:36 +01:00
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
|
|
|
removedNodeLists.length = 0;
|
2017-10-23 18:21:37 +02:00
|
|
|
//console.timeEnd('dom watcher/safe observer handler');
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( addedNodes.length === 0 && removedNodes === false ) { return; }
|
2018-12-26 16:45:19 +01:00
|
|
|
for ( const listener of getListenerIterator() ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
listener.onDOMChanged(addedNodes, removedNodes);
|
|
|
|
}
|
|
|
|
addedNodes.length = 0;
|
|
|
|
removedNodes = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/205
|
|
|
|
// Do not handle added node directly from within mutation observer.
|
2018-12-26 16:45:19 +01:00
|
|
|
const observerHandler = function(mutations) {
|
2017-10-23 18:21:37 +02:00
|
|
|
//console.time('dom watcher/observer handler');
|
2018-12-26 16:45:19 +01:00
|
|
|
let i = mutations.length;
|
2017-10-21 19:43:46 +02:00
|
|
|
while ( i-- ) {
|
2018-12-26 16:45:19 +01:00
|
|
|
const mutation = mutations[i];
|
|
|
|
let nodeList = mutation.addedNodes;
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( nodeList.length !== 0 ) {
|
|
|
|
addedNodeLists.push(nodeList);
|
2016-12-16 22:25:36 +01:00
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
nodeList = mutation.removedNodes;
|
|
|
|
if ( nodeList.length !== 0 ) {
|
|
|
|
removedNodeLists.push(nodeList);
|
2016-12-16 22:25:36 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( addedNodeLists.length !== 0 || removedNodes ) {
|
2017-12-10 21:03:03 +01:00
|
|
|
safeObserverHandlerTimer.start(
|
|
|
|
addedNodeLists.length < 100 ? 1 : undefined
|
|
|
|
);
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
2017-10-23 18:21:37 +02:00
|
|
|
//console.timeEnd('dom watcher/observer handler');
|
2016-12-16 22:25:36 +01:00
|
|
|
};
|
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const startMutationObserver = function() {
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( domLayoutObserver !== undefined || !domIsReady ) { return; }
|
|
|
|
domLayoutObserver = new MutationObserver(observerHandler);
|
|
|
|
domLayoutObserver.observe(document.documentElement, {
|
|
|
|
//attributeFilter: [ 'class', 'id' ],
|
|
|
|
//attributes: true,
|
|
|
|
childList: true,
|
|
|
|
subtree: true
|
|
|
|
});
|
|
|
|
safeObserverHandlerTimer = new vAPI.SafeAnimationFrame(safeObserverHandler);
|
|
|
|
vAPI.shutdown.add(cleanup);
|
|
|
|
};
|
2016-12-15 16:47:32 +01:00
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const stopMutationObserver = function() {
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( domLayoutObserver === undefined ) { return; }
|
|
|
|
cleanup();
|
|
|
|
vAPI.shutdown.remove(cleanup);
|
|
|
|
};
|
2016-12-15 16:47:32 +01:00
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const getListenerIterator = function() {
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( listenerIteratorDirty ) {
|
|
|
|
listenerIterator = listeners.slice();
|
|
|
|
listenerIteratorDirty = false;
|
2017-03-12 15:22:46 +01:00
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
return listenerIterator;
|
2016-12-15 16:47:32 +01:00
|
|
|
};
|
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const addListener = function(listener) {
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( listeners.indexOf(listener) !== -1 ) { return; }
|
|
|
|
listeners.push(listener);
|
|
|
|
listenerIteratorDirty = true;
|
2017-10-25 17:42:18 +02:00
|
|
|
if ( domIsReady !== true ) { return; }
|
|
|
|
listener.onDOMCreated();
|
|
|
|
startMutationObserver();
|
2016-12-15 16:47:32 +01:00
|
|
|
};
|
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const removeListener = function(listener) {
|
|
|
|
const pos = listeners.indexOf(listener);
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( pos === -1 ) { return; }
|
|
|
|
listeners.splice(pos, 1);
|
|
|
|
listenerIteratorDirty = true;
|
|
|
|
if ( listeners.length === 0 ) {
|
|
|
|
stopMutationObserver();
|
2017-03-12 15:22:46 +01:00
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
};
|
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const cleanup = function() {
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( domLayoutObserver !== undefined ) {
|
|
|
|
domLayoutObserver.disconnect();
|
|
|
|
domLayoutObserver = null;
|
2016-12-15 16:47:32 +01:00
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( safeObserverHandlerTimer !== undefined ) {
|
|
|
|
safeObserverHandlerTimer.clear();
|
|
|
|
safeObserverHandlerTimer = undefined;
|
2016-12-15 16:47:32 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const start = function() {
|
2017-10-21 19:43:46 +02:00
|
|
|
domIsReady = true;
|
2018-12-26 16:45:19 +01:00
|
|
|
for ( const listener of getListenerIterator() ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
listener.onDOMCreated();
|
2016-12-15 16:47:32 +01:00
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
startMutationObserver();
|
|
|
|
};
|
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
return { start, addListener, removeListener };
|
2016-12-15 16:47:32 +01:00
|
|
|
})();
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
2016-12-15 16:47:32 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-05-11 16:40:34 +02:00
|
|
|
vAPI.matchesProp = (( ) => {
|
2018-12-26 16:45:19 +01:00
|
|
|
const docElem = document.documentElement;
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( typeof docElem.matches !== 'function' ) {
|
|
|
|
if ( typeof docElem.mozMatchesSelector === 'function' ) {
|
|
|
|
return 'mozMatchesSelector';
|
|
|
|
} else if ( typeof docElem.webkitMatchesSelector === 'function' ) {
|
|
|
|
return 'webkitMatchesSelector';
|
|
|
|
} else if ( typeof docElem.msMatchesSelector === 'function' ) {
|
|
|
|
return 'msMatchesSelector';
|
2016-12-25 22:56:39 +01:00
|
|
|
}
|
2016-08-06 18:09:18 +02:00
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
return 'matches';
|
|
|
|
})();
|
2016-08-06 18:09:18 +02:00
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
2016-08-06 18:09:18 +02:00
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
vAPI.injectScriptlet = function(doc, text) {
|
|
|
|
if ( !doc ) { return; }
|
2018-05-18 16:19:14 +02:00
|
|
|
let script;
|
2017-10-21 19:43:46 +02:00
|
|
|
try {
|
2018-05-18 16:19:14 +02:00
|
|
|
script = doc.createElement('script');
|
2017-10-21 19:43:46 +02:00
|
|
|
script.appendChild(doc.createTextNode(text));
|
|
|
|
(doc.head || doc.documentElement).appendChild(script);
|
|
|
|
} catch (ex) {
|
2016-08-06 18:09:18 +02:00
|
|
|
}
|
2018-06-02 00:41:27 +02:00
|
|
|
if ( script ) {
|
|
|
|
if ( script.parentNode ) {
|
|
|
|
script.parentNode.removeChild(script);
|
|
|
|
}
|
|
|
|
script.textContent = '';
|
2018-05-18 16:19:14 +02:00
|
|
|
}
|
2016-08-06 18:09:18 +02:00
|
|
|
};
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
/*******************************************************************************
|
2016-08-06 18:09:18 +02:00
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
The DOM filterer is the heart of uBO's cosmetic filtering.
|
2016-12-25 22:56:39 +01:00
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
DOMBaseFilterer: platform-specific
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+---- DOMFilterer: adds procedural cosmetic filtering
|
2016-12-25 22:56:39 +01:00
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
*/
|
2016-12-25 22:56:39 +01:00
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
vAPI.DOMFilterer = (function() {
|
2016-08-06 18:09:18 +02:00
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
// 'P' stands for 'Procedural'
|
2016-08-06 18:09:18 +02:00
|
|
|
|
2019-04-30 15:02:14 +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-06-23 14:05:53 +02:00
|
|
|
transpose(node, output) {
|
|
|
|
if ( this.needle.test(node.textContent) ) {
|
|
|
|
output.push(node);
|
2016-07-10 01:21:46 +02:00
|
|
|
}
|
2016-06-28 01:09:04 +02:00
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
};
|
2016-06-29 01:45:11 +02:00
|
|
|
|
2019-04-30 15:02:14 +02:00
|
|
|
const PSelectorIfTask = class {
|
|
|
|
constructor(task) {
|
|
|
|
this.pselector = new PSelector(task[1]);
|
|
|
|
}
|
2019-06-23 14:05:53 +02:00
|
|
|
transpose(node, output) {
|
|
|
|
if ( this.pselector.test(node) === this.target ) {
|
|
|
|
output.push(node);
|
2016-07-10 01:21:46 +02:00
|
|
|
}
|
2016-06-29 01:45:11 +02:00
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
};
|
2019-04-30 15:02:14 +02:00
|
|
|
PSelectorIfTask.prototype.target = true;
|
2016-06-29 01:45:11 +02:00
|
|
|
|
2019-04-30 15:02:14 +02:00
|
|
|
const PSelectorIfNotTask = class extends PSelectorIfTask {
|
2017-10-21 19:43:46 +02:00
|
|
|
};
|
2019-06-23 14:05:53 +02:00
|
|
|
PSelectorIfNotTask.prototype.target = false;
|
2019-04-30 15:02:14 +02:00
|
|
|
|
|
|
|
const PSelectorMatchesCSSTask = class {
|
|
|
|
constructor(task) {
|
|
|
|
this.name = task[1].name;
|
|
|
|
let arg0 = task[1].value, arg1;
|
|
|
|
if ( Array.isArray(arg0) ) {
|
|
|
|
arg1 = arg0[1]; arg0 = arg0[0];
|
|
|
|
}
|
|
|
|
this.value = new RegExp(arg0, arg1);
|
|
|
|
}
|
2019-06-23 14:05:53 +02:00
|
|
|
transpose(node, output) {
|
|
|
|
const style = window.getComputedStyle(node, this.pseudo);
|
|
|
|
if ( style !== null && this.value.test(style[this.name]) ) {
|
|
|
|
output.push(node);
|
2016-09-22 18:18:01 +02:00
|
|
|
}
|
2016-07-12 19:29:30 +02:00
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
};
|
2019-04-30 15:02:14 +02:00
|
|
|
PSelectorMatchesCSSTask.prototype.pseudo = null;
|
2016-07-12 19:29:30 +02:00
|
|
|
|
2019-04-30 15:02:14 +02:00
|
|
|
const PSelectorMatchesCSSAfterTask = class extends PSelectorMatchesCSSTask {
|
2017-10-21 19:43:46 +02:00
|
|
|
};
|
2019-06-23 14:05:53 +02:00
|
|
|
PSelectorMatchesCSSAfterTask.prototype.pseudo = ':after';
|
2016-09-24 20:42:31 +02:00
|
|
|
|
2019-04-30 15:02:14 +02:00
|
|
|
const PSelectorMatchesCSSBeforeTask = class extends PSelectorMatchesCSSTask {
|
2017-10-21 19:43:46 +02:00
|
|
|
};
|
2019-06-23 14:05:53 +02:00
|
|
|
PSelectorMatchesCSSBeforeTask.prototype.pseudo = ':before';
|
2016-06-29 01:45:11 +02:00
|
|
|
|
2019-06-20 20:11:54 +02:00
|
|
|
const PSelectorMinTextLengthTask = class {
|
|
|
|
constructor(task) {
|
|
|
|
this.min = task[1];
|
|
|
|
}
|
2019-06-23 14:05:53 +02:00
|
|
|
transpose(node, output) {
|
|
|
|
if ( node.textContent.length >= this.min ) {
|
|
|
|
output.push(node);
|
2019-06-20 20:11:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-30 15:02:14 +02:00
|
|
|
const PSelectorNthAncestorTask = class {
|
|
|
|
constructor(task) {
|
|
|
|
this.nth = task[1];
|
|
|
|
}
|
2019-06-23 14:05:53 +02:00
|
|
|
transpose(node, output) {
|
|
|
|
let nth = this.nth;
|
|
|
|
for (;;) {
|
|
|
|
node = node.parentElement;
|
|
|
|
if ( node === null ) { return; }
|
|
|
|
nth -= 1;
|
|
|
|
if ( nth === 0 ) { break; }
|
2018-12-26 16:45:19 +01:00
|
|
|
}
|
2019-06-23 14:05:53 +02:00
|
|
|
output.push(node);
|
2018-12-26 16:45:19 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-30 15:02:14 +02:00
|
|
|
const PSelectorSpathTask = class {
|
|
|
|
constructor(task) {
|
|
|
|
this.spath = task[1];
|
|
|
|
}
|
2019-06-23 14:05:53 +02:00
|
|
|
transpose(node, output) {
|
|
|
|
const parent = node.parentElement;
|
|
|
|
if ( parent === null ) { return; }
|
|
|
|
let pos = 1;
|
|
|
|
for (;;) {
|
|
|
|
node = node.previousElementSibling;
|
|
|
|
if ( node === null ) { break; }
|
|
|
|
pos += 1;
|
|
|
|
}
|
|
|
|
const nodes = parent.querySelectorAll(
|
|
|
|
`:scope > :nth-child(${pos})${this.spath}`
|
|
|
|
);
|
|
|
|
for ( const node of nodes ) {
|
|
|
|
output.push(node);
|
2019-04-30 15:02:14 +02:00
|
|
|
}
|
2018-12-26 16:45:19 +01:00
|
|
|
}
|
|
|
|
};
|
2019-04-30 15:02:14 +02:00
|
|
|
|
|
|
|
const PSelectorWatchAttrs = class {
|
|
|
|
constructor(task) {
|
|
|
|
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];
|
|
|
|
}
|
2018-12-26 16:45:19 +01:00
|
|
|
}
|
2019-04-30 15:02:14 +02:00
|
|
|
// 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 ]);
|
|
|
|
}
|
2018-12-26 16:45:19 +01:00
|
|
|
}
|
2019-06-23 14:05:53 +02:00
|
|
|
transpose(node, output) {
|
|
|
|
output.push(node);
|
|
|
|
if ( this.observed.has(node) ) { return; }
|
2019-04-30 15:02:14 +02:00
|
|
|
if ( this.observer === null ) {
|
|
|
|
this.observer = new MutationObserver(this.handler);
|
|
|
|
}
|
2019-06-23 14:05:53 +02:00
|
|
|
this.observer.observe(node, this.observerOptions);
|
|
|
|
this.observed.add(node);
|
2018-12-26 16:45:19 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-30 15:02:14 +02:00
|
|
|
const PSelectorXpathTask = class {
|
|
|
|
constructor(task) {
|
|
|
|
this.xpe = document.createExpression(task[1], null);
|
|
|
|
this.xpr = null;
|
|
|
|
}
|
2019-06-23 14:05:53 +02:00
|
|
|
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);
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-29 01:45:11 +02:00
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
};
|
2016-06-29 01:45:11 +02:00
|
|
|
|
2019-04-30 15:02:14 +02:00
|
|
|
const PSelector = class {
|
|
|
|
constructor(o) {
|
|
|
|
if ( PSelector.prototype.operatorToTaskMap === undefined ) {
|
|
|
|
PSelector.prototype.operatorToTaskMap = new Map([
|
|
|
|
[ ':has', PSelectorIfTask ],
|
|
|
|
[ ':has-text', PSelectorHasTextTask ],
|
|
|
|
[ ':if', PSelectorIfTask ],
|
|
|
|
[ ':if-not', PSelectorIfNotTask ],
|
|
|
|
[ ':matches-css', PSelectorMatchesCSSTask ],
|
|
|
|
[ ':matches-css-after', PSelectorMatchesCSSAfterTask ],
|
|
|
|
[ ':matches-css-before', PSelectorMatchesCSSBeforeTask ],
|
2019-06-20 20:11:54 +02:00
|
|
|
[ ':min-text-length', PSelectorMinTextLengthTask ],
|
2019-04-30 15:02:14 +02:00
|
|
|
[ ':not', PSelectorIfNotTask ],
|
|
|
|
[ ':nth-ancestor', PSelectorNthAncestorTask ],
|
|
|
|
[ ':spath', PSelectorSpathTask ],
|
2019-06-23 14:05:53 +02:00
|
|
|
[ ':watch-attr', PSelectorWatchAttrs ],
|
2019-04-30 15:02:14 +02:00
|
|
|
[ ':xpath', PSelectorXpathTask ],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
this.budget = 200; // I arbitrary picked a 1/5 second
|
|
|
|
this.raw = o.raw;
|
|
|
|
this.cost = 0;
|
|
|
|
this.lastAllowanceTime = 0;
|
|
|
|
this.selector = o.selector;
|
|
|
|
this.tasks = [];
|
|
|
|
const tasks = o.tasks;
|
|
|
|
if ( !tasks ) { return; }
|
|
|
|
for ( const task of tasks ) {
|
|
|
|
this.tasks.push(new (this.operatorToTaskMap.get(task[0]))(task));
|
|
|
|
}
|
2016-08-14 14:51:52 +02:00
|
|
|
}
|
2019-04-30 15:02:14 +02:00
|
|
|
prime(input) {
|
|
|
|
const root = input || document;
|
2019-06-29 20:07:54 +02:00
|
|
|
if ( this.selector === '' ) { return [ root ]; }
|
|
|
|
return root.querySelectorAll(this.selector);
|
2016-07-10 01:21:46 +02:00
|
|
|
}
|
2019-04-30 15:02:14 +02:00
|
|
|
exec(input) {
|
|
|
|
let nodes = this.prime(input);
|
2018-12-14 23:55:22 +01:00
|
|
|
for ( const task of this.tasks ) {
|
2019-04-30 15:02:14 +02:00
|
|
|
if ( nodes.length === 0 ) { break; }
|
2019-06-23 14:05:53 +02:00
|
|
|
const transposed = [];
|
|
|
|
for ( const node of nodes ) {
|
|
|
|
task.transpose(node, transposed);
|
|
|
|
}
|
|
|
|
nodes = transposed;
|
2019-04-30 15:02:14 +02:00
|
|
|
}
|
|
|
|
return nodes;
|
|
|
|
}
|
|
|
|
test(input) {
|
|
|
|
const nodes = this.prime(input);
|
|
|
|
for ( const node of nodes ) {
|
2019-06-23 14:05:53 +02:00
|
|
|
let output = [ node ];
|
2019-04-30 15:02:14 +02:00
|
|
|
for ( const task of this.tasks ) {
|
2019-06-23 14:05:53 +02:00
|
|
|
const transposed = [];
|
|
|
|
for ( const node of output ) {
|
|
|
|
task.transpose(node, transposed);
|
|
|
|
}
|
|
|
|
output = transposed;
|
|
|
|
if ( output.length === 0 ) { break; }
|
2019-04-30 15:02:14 +02:00
|
|
|
}
|
2019-06-23 14:05:53 +02:00
|
|
|
if ( output.length !== 0 ) { return true; }
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
2019-04-30 15:02:14 +02:00
|
|
|
return false;
|
2016-07-10 01:21:46 +02:00
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
};
|
2019-04-30 15:02:14 +02:00
|
|
|
PSelector.prototype.operatorToTaskMap = undefined;
|
2016-11-12 19:38:41 +01:00
|
|
|
|
2019-05-16 23:22:20 +02:00
|
|
|
const DOMProceduralFilterer = class {
|
|
|
|
constructor(domFilterer) {
|
|
|
|
this.domFilterer = domFilterer;
|
|
|
|
this.domIsReady = false;
|
|
|
|
this.domIsWatched = false;
|
|
|
|
this.mustApplySelectors = false;
|
|
|
|
this.selectors = new Map();
|
|
|
|
this.hiddenNodes = new Set();
|
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
|
2019-05-16 23:22:20 +02:00
|
|
|
addProceduralSelectors(aa) {
|
2018-12-14 23:55:22 +01:00
|
|
|
const addedSelectors = [];
|
|
|
|
let mustCommit = this.domIsWatched;
|
|
|
|
for ( let i = 0, n = aa.length; i < n; i++ ) {
|
|
|
|
const raw = aa[i];
|
|
|
|
const o = JSON.parse(raw);
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( o.style ) {
|
|
|
|
this.domFilterer.addCSSRule(o.style[0], o.style[1]);
|
|
|
|
mustCommit = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( o.pseudoclass ) {
|
|
|
|
this.domFilterer.addCSSRule(
|
|
|
|
o.raw,
|
2017-10-23 15:01:00 +02:00
|
|
|
'display:none!important;'
|
2017-10-21 19:43:46 +02:00
|
|
|
);
|
|
|
|
mustCommit = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( o.tasks ) {
|
|
|
|
if ( this.selectors.has(raw) === false ) {
|
2018-12-14 23:55:22 +01:00
|
|
|
const pselector = new PSelector(o);
|
2017-10-21 19:43:46 +02:00
|
|
|
this.selectors.set(raw, pselector);
|
2018-12-14 23:55:22 +01:00
|
|
|
addedSelectors.push(pselector);
|
2017-10-21 19:43:46 +02:00
|
|
|
mustCommit = true;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( mustCommit === false ) { return; }
|
2018-12-14 23:55:22 +01:00
|
|
|
this.mustApplySelectors = this.selectors.size !== 0;
|
2017-10-21 19:43:46 +02:00
|
|
|
this.domFilterer.commit();
|
2017-11-14 21:03:20 +01:00
|
|
|
if ( this.domFilterer.hasListeners() ) {
|
|
|
|
this.domFilterer.triggerListeners({
|
2018-12-14 23:55:22 +01:00
|
|
|
procedural: addedSelectors
|
2017-11-14 21:03:20 +01:00
|
|
|
});
|
|
|
|
}
|
2019-05-16 23:22:20 +02:00
|
|
|
}
|
2015-01-13 21:52:15 +01:00
|
|
|
|
2019-05-16 23:22:20 +02:00
|
|
|
commitNow() {
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( this.selectors.size === 0 || this.domIsReady === false ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2018-12-14 23:55:22 +01:00
|
|
|
this.mustApplySelectors = false;
|
2016-06-28 01:09:04 +02:00
|
|
|
|
2017-10-31 11:47:39 +01:00
|
|
|
//console.time('procedural selectors/dom layout changed');
|
2016-06-28 01:09:04 +02:00
|
|
|
|
2018-12-14 23:55:22 +01:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/341
|
|
|
|
// Be ready to unhide nodes which no longer matches any of
|
|
|
|
// the procedural selectors.
|
|
|
|
const toRemove = this.hiddenNodes;
|
|
|
|
this.hiddenNodes = new Set();
|
2016-06-28 01:09:04 +02:00
|
|
|
|
2018-12-14 23:55:22 +01:00
|
|
|
let t0 = Date.now();
|
2016-06-28 01:09:04 +02:00
|
|
|
|
2018-12-14 23:55:22 +01:00
|
|
|
for ( const entry of this.selectors ) {
|
|
|
|
const pselector = entry[1];
|
|
|
|
const allowance = Math.floor((t0 - pselector.lastAllowanceTime) / 2000);
|
2017-12-10 21:03:03 +01:00
|
|
|
if ( allowance >= 1 ) {
|
|
|
|
pselector.budget += allowance * 50;
|
|
|
|
if ( pselector.budget > 200 ) { pselector.budget = 200; }
|
|
|
|
pselector.lastAllowanceTime = t0;
|
|
|
|
}
|
2017-10-24 22:38:51 +02:00
|
|
|
if ( pselector.budget <= 0 ) { continue; }
|
2018-12-14 23:55:22 +01:00
|
|
|
const nodes = pselector.exec();
|
|
|
|
const t1 = Date.now();
|
2017-12-10 21:03:03 +01:00
|
|
|
pselector.budget += t0 - t1;
|
|
|
|
if ( pselector.budget < -500 ) {
|
2017-12-13 14:02:55 +01:00
|
|
|
console.info('uBO: disabling %s', pselector.raw);
|
2017-12-10 21:03:03 +01:00
|
|
|
pselector.budget = -0x7FFFFFFF;
|
2017-10-24 22:38:51 +02:00
|
|
|
}
|
2017-12-10 21:03:03 +01:00
|
|
|
t0 = t1;
|
2018-12-26 16:45:19 +01:00
|
|
|
for ( const node of nodes ) {
|
|
|
|
this.domFilterer.hideNode(node);
|
|
|
|
this.hiddenNodes.add(node);
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
2016-07-10 01:21:46 +02:00
|
|
|
}
|
2015-01-02 02:58:19 +01:00
|
|
|
|
2018-12-14 23:55:22 +01:00
|
|
|
for ( const node of toRemove ) {
|
|
|
|
if ( this.hiddenNodes.has(node) ) { continue; }
|
|
|
|
this.domFilterer.unhideNode(node);
|
|
|
|
}
|
2017-10-31 11:47:39 +01:00
|
|
|
//console.timeEnd('procedural selectors/dom layout changed');
|
2019-05-16 23:22:20 +02:00
|
|
|
}
|
2016-06-28 01:09:04 +02:00
|
|
|
|
2019-05-16 23:22:20 +02:00
|
|
|
createProceduralFilter(o) {
|
2017-10-21 19:43:46 +02:00
|
|
|
return new PSelector(o);
|
2019-05-16 23:22:20 +02:00
|
|
|
}
|
2016-06-28 01:09:04 +02:00
|
|
|
|
2019-05-16 23:22:20 +02:00
|
|
|
onDOMCreated() {
|
2017-10-21 19:43:46 +02:00
|
|
|
this.domIsReady = true;
|
2017-10-23 18:21:37 +02:00
|
|
|
this.domFilterer.commitNow();
|
2019-05-16 23:22:20 +02:00
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2019-05-16 23:22:20 +02:00
|
|
|
onDOMChanged(addedNodes, removedNodes) {
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( this.selectors.size === 0 ) { return; }
|
2018-12-14 23:55:22 +01:00
|
|
|
this.mustApplySelectors =
|
|
|
|
this.mustApplySelectors ||
|
|
|
|
addedNodes.length !== 0 ||
|
|
|
|
removedNodes;
|
2017-10-21 19:43:46 +02:00
|
|
|
this.domFilterer.commit();
|
|
|
|
}
|
|
|
|
};
|
2016-08-12 14:55:35 +02:00
|
|
|
|
2019-05-16 19:44:49 +02:00
|
|
|
const DOMFilterer = class extends vAPI.DOMFilterer {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.exceptions = [];
|
|
|
|
this.proceduralFilterer = new DOMProceduralFilterer(this);
|
|
|
|
this.hideNodeAttr = undefined;
|
|
|
|
this.hideNodeStyleSheetInjected = false;
|
|
|
|
if ( vAPI.domWatcher instanceof Object ) {
|
|
|
|
vAPI.domWatcher.addListener(this);
|
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 19:44:49 +02:00
|
|
|
commitNow() {
|
|
|
|
super.commitNow();
|
|
|
|
this.proceduralFilterer.commitNow();
|
|
|
|
}
|
2016-11-12 19:38:41 +01:00
|
|
|
|
2019-05-16 19:44:49 +02:00
|
|
|
addProceduralSelectors(aa) {
|
|
|
|
this.proceduralFilterer.addProceduralSelectors(aa);
|
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
|
2019-05-16 19:44:49 +02:00
|
|
|
createProceduralFilter(o) {
|
|
|
|
return this.proceduralFilterer.createProceduralFilter(o);
|
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
|
2019-05-16 19:44:49 +02:00
|
|
|
getAllSelectors() {
|
|
|
|
const out = super.getAllSelectors();
|
|
|
|
out.procedural = Array.from(this.proceduralFilterer.selectors.values());
|
|
|
|
return out;
|
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
|
2019-05-16 19:44:49 +02:00
|
|
|
getAllExceptionSelectors() {
|
|
|
|
return this.exceptions.join(',\n');
|
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
|
2019-05-16 19:44:49 +02:00
|
|
|
onDOMCreated() {
|
|
|
|
if ( super.onDOMCreated instanceof Function ) {
|
|
|
|
super.onDOMCreated();
|
|
|
|
}
|
|
|
|
this.proceduralFilterer.onDOMCreated();
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 19:44:49 +02:00
|
|
|
onDOMChanged() {
|
|
|
|
if ( super.onDOMChanged instanceof Function ) {
|
2019-05-16 23:22:20 +02:00
|
|
|
super.onDOMChanged.apply(this, arguments);
|
2019-05-16 19:44:49 +02:00
|
|
|
}
|
|
|
|
this.proceduralFilterer.onDOMChanged.apply(
|
|
|
|
this.proceduralFilterer,
|
|
|
|
arguments
|
|
|
|
);
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
};
|
2017-10-21 19:43:46 +02:00
|
|
|
|
2019-05-16 19:44:49 +02:00
|
|
|
return DOMFilterer;
|
2016-08-12 14:55:35 +02:00
|
|
|
})();
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
vAPI.domFilterer = new vAPI.DOMFilterer();
|
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
vAPI.domCollapser = (function() {
|
2018-12-26 16:45:19 +01:00
|
|
|
const messaging = vAPI.messaging;
|
|
|
|
const toCollapse = new Map();
|
|
|
|
const src1stProps = {
|
|
|
|
embed: 'src',
|
|
|
|
iframe: 'src',
|
|
|
|
img: 'src',
|
|
|
|
object: 'data'
|
2015-03-29 18:13:28 +02:00
|
|
|
};
|
2018-12-26 16:45:19 +01:00
|
|
|
const src2ndProps = {
|
|
|
|
img: 'srcset'
|
2015-06-04 17:17:02 +02:00
|
|
|
};
|
2018-12-26 16:45:19 +01:00
|
|
|
const tagToTypeMap = {
|
2017-08-03 16:18:05 +02:00
|
|
|
embed: 'object',
|
|
|
|
iframe: 'sub_frame',
|
|
|
|
img: 'image',
|
|
|
|
object: 'object'
|
|
|
|
};
|
2017-08-16 20:10:41 +02:00
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
let resquestIdGenerator = 1,
|
|
|
|
processTimer,
|
|
|
|
cachedBlockedSet,
|
|
|
|
cachedBlockedSetHash,
|
|
|
|
cachedBlockedSetTimer,
|
2018-12-27 16:17:08 +01:00
|
|
|
toProcess = [],
|
|
|
|
toFilter = [],
|
2018-12-26 16:45:19 +01:00
|
|
|
netSelectorCacheCount = 0;
|
|
|
|
|
|
|
|
const cachedBlockedSetClear = function() {
|
2017-08-03 16:18:05 +02:00
|
|
|
cachedBlockedSet =
|
|
|
|
cachedBlockedSetHash =
|
|
|
|
cachedBlockedSetTimer = undefined;
|
2015-03-29 18:13:28 +02:00
|
|
|
};
|
|
|
|
|
2017-08-03 16:18:05 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/174
|
|
|
|
// Do not remove fragment from src URL
|
2018-12-26 16:45:19 +01:00
|
|
|
const onProcessed = function(response) {
|
2019-09-17 21:15:01 +02:00
|
|
|
// This happens if uBO is disabled or restarted.
|
|
|
|
if ( response instanceof Object === false ) {
|
2017-08-03 16:18:05 +02:00
|
|
|
toCollapse.clear();
|
2016-03-21 15:33:40 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-08-03 16:18:05 +02:00
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const targets = toCollapse.get(response.id);
|
2017-08-03 16:18:05 +02:00
|
|
|
if ( targets === undefined ) { return; }
|
|
|
|
toCollapse.delete(response.id);
|
|
|
|
if ( cachedBlockedSetHash !== response.hash ) {
|
|
|
|
cachedBlockedSet = new Set(response.blockedResources);
|
|
|
|
cachedBlockedSetHash = response.hash;
|
|
|
|
if ( cachedBlockedSetTimer !== undefined ) {
|
|
|
|
clearTimeout(cachedBlockedSetTimer);
|
|
|
|
}
|
|
|
|
cachedBlockedSetTimer = vAPI.setTimeout(cachedBlockedSetClear, 30000);
|
2015-03-29 18:13:28 +02:00
|
|
|
}
|
2017-08-16 20:10:41 +02:00
|
|
|
if ( cachedBlockedSet === undefined || cachedBlockedSet.size === 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-26 16:45:19 +01:00
|
|
|
const selectors = [];
|
|
|
|
const iframeLoadEventPatch = vAPI.iframeLoadEventPatch;
|
|
|
|
let netSelectorCacheCountMax = response.netSelectorCacheCountMax;
|
2017-08-03 16:18:05 +02:00
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
for ( const target of targets ) {
|
|
|
|
const tag = target.localName;
|
|
|
|
let prop = src1stProps[tag];
|
2017-08-03 16:18:05 +02:00
|
|
|
if ( prop === undefined ) { continue; }
|
2018-12-26 16:45:19 +01:00
|
|
|
let src = target[prop];
|
2017-08-03 16:18:05 +02:00
|
|
|
if ( typeof src !== 'string' || src.length === 0 ) {
|
|
|
|
prop = src2ndProps[tag];
|
|
|
|
if ( prop === undefined ) { continue; }
|
|
|
|
src = target[prop];
|
|
|
|
if ( typeof src !== 'string' || src.length === 0 ) { continue; }
|
2015-03-29 18:13:28 +02:00
|
|
|
}
|
2017-08-03 16:18:05 +02:00
|
|
|
if ( cachedBlockedSet.has(tagToTypeMap[tag] + ' ' + src) === false ) {
|
2015-03-29 18:13:28 +02:00
|
|
|
continue;
|
|
|
|
}
|
2017-08-03 16:18:05 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/399
|
|
|
|
// Never remove elements from the DOM, just hide them
|
|
|
|
target.style.setProperty('display', 'none', 'important');
|
|
|
|
target.hidden = true;
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/1048
|
|
|
|
// Use attribute to construct CSS rule
|
2018-12-26 16:45:19 +01:00
|
|
|
if ( netSelectorCacheCount <= netSelectorCacheCountMax ) {
|
|
|
|
const value = target.getAttribute(prop);
|
|
|
|
if ( value ) {
|
|
|
|
selectors.push(tag + '[' + prop + '="' + value + '"]');
|
|
|
|
netSelectorCacheCount += 1;
|
|
|
|
}
|
2016-08-08 15:53:35 +02:00
|
|
|
}
|
2017-08-03 16:18:05 +02:00
|
|
|
if ( iframeLoadEventPatch !== undefined ) {
|
|
|
|
iframeLoadEventPatch(target);
|
2015-03-29 18:13:28 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-22 14:59:29 +02:00
|
|
|
|
2015-03-29 18:13:28 +02:00
|
|
|
if ( selectors.length !== 0 ) {
|
2019-09-17 21:15:01 +02:00
|
|
|
messaging.send('contentscript', {
|
|
|
|
what: 'cosmeticFiltersInjected',
|
|
|
|
type: 'net',
|
|
|
|
hostname: window.location.hostname,
|
|
|
|
selectors,
|
|
|
|
});
|
2015-03-29 18:13:28 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const send = function() {
|
2017-08-03 16:18:05 +02:00
|
|
|
processTimer = undefined;
|
|
|
|
toCollapse.set(resquestIdGenerator, toProcess);
|
2019-09-17 21:15:01 +02:00
|
|
|
messaging.send('contentscript', {
|
2017-08-03 16:18:05 +02:00
|
|
|
what: 'getCollapsibleBlockedRequests',
|
|
|
|
id: resquestIdGenerator,
|
2017-10-01 13:56:28 +02:00
|
|
|
frameURL: window.location.href,
|
2017-08-03 16:18:05 +02:00
|
|
|
resources: toFilter,
|
2019-09-17 21:15:01 +02:00
|
|
|
hash: cachedBlockedSetHash,
|
|
|
|
}).then(response => {
|
|
|
|
onProcessed(response);
|
|
|
|
});
|
2018-12-27 16:17:08 +01:00
|
|
|
toProcess = [];
|
|
|
|
toFilter = [];
|
2017-08-03 16:18:05 +02:00
|
|
|
resquestIdGenerator += 1;
|
2015-03-29 18:13:28 +02:00
|
|
|
};
|
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const process = function(delay) {
|
2017-08-03 16:18:05 +02:00
|
|
|
if ( toProcess.length === 0 ) { return; }
|
2015-03-29 18:13:28 +02:00
|
|
|
if ( delay === 0 ) {
|
2017-08-03 16:18:05 +02:00
|
|
|
if ( processTimer !== undefined ) {
|
|
|
|
clearTimeout(processTimer);
|
|
|
|
}
|
2015-03-29 18:13:28 +02:00
|
|
|
send();
|
2017-08-03 16:18:05 +02:00
|
|
|
} else if ( processTimer === undefined ) {
|
|
|
|
processTimer = vAPI.setTimeout(send, delay || 20);
|
2015-03-29 18:13:28 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const add = function(target) {
|
2017-08-03 16:18:05 +02:00
|
|
|
toProcess[toProcess.length] = target;
|
2015-03-29 18:13:28 +02:00
|
|
|
};
|
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const addMany = function(targets) {
|
|
|
|
for ( const target of targets ) {
|
|
|
|
add(target);
|
2016-02-04 01:15:28 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const iframeSourceModified = function(mutations) {
|
|
|
|
for ( const mutation of mutations ) {
|
|
|
|
addIFrame(mutation.target, true);
|
2015-05-02 01:06:52 +02:00
|
|
|
}
|
|
|
|
process();
|
|
|
|
};
|
2018-12-26 16:45:19 +01:00
|
|
|
const iframeSourceObserver = new MutationObserver(iframeSourceModified);
|
|
|
|
const iframeSourceObserverOptions = {
|
2015-05-02 01:06:52 +02:00
|
|
|
attributes: true,
|
|
|
|
attributeFilter: [ 'src' ]
|
|
|
|
};
|
|
|
|
|
2018-05-20 12:49:12 +02:00
|
|
|
// The injected scriptlets are those which were injected in the current
|
|
|
|
// document, from within `bootstrapPhase1`, and which scriptlets are
|
|
|
|
// selectively looked-up from:
|
|
|
|
// https://github.com/uBlockOrigin/uAssets/blob/master/filters/resources.txt
|
2018-12-26 16:45:19 +01:00
|
|
|
const primeLocalIFrame = function(iframe) {
|
2016-01-21 15:33:54 +01:00
|
|
|
if ( vAPI.injectedScripts ) {
|
2017-08-21 18:04:35 +02:00
|
|
|
vAPI.injectScriptlet(iframe.contentDocument, vAPI.injectedScripts);
|
2016-01-21 15:33:54 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-20 12:49:12 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/162
|
|
|
|
// Be prepared to deal with possible change of src attribute.
|
2018-12-26 16:45:19 +01:00
|
|
|
const addIFrame = function(iframe, dontObserve) {
|
2015-05-02 01:06:52 +02:00
|
|
|
if ( dontObserve !== true ) {
|
|
|
|
iframeSourceObserver.observe(iframe, iframeSourceObserverOptions);
|
|
|
|
}
|
2018-12-26 16:45:19 +01:00
|
|
|
const src = iframe.src;
|
2015-03-29 18:13:28 +02:00
|
|
|
if ( src === '' || typeof src !== 'string' ) {
|
2016-01-21 15:33:54 +01:00
|
|
|
primeLocalIFrame(iframe);
|
2015-03-29 18:13:28 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-05-20 12:49:12 +02:00
|
|
|
if ( src.startsWith('http') === false ) { return; }
|
2018-12-27 16:17:08 +01:00
|
|
|
toFilter.push({ type: 'sub_frame', url: iframe.src });
|
2017-08-03 16:18:05 +02:00
|
|
|
add(iframe);
|
2015-03-29 18:13:28 +02:00
|
|
|
};
|
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const addIFrames = function(iframes) {
|
|
|
|
for ( const iframe of iframes ) {
|
|
|
|
addIFrame(iframe);
|
2016-02-04 00:47:30 +01:00
|
|
|
}
|
2016-02-04 01:15:28 +01:00
|
|
|
};
|
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const onResourceFailed = function(ev) {
|
2017-08-03 16:18:05 +02:00
|
|
|
if ( tagToTypeMap[ev.target.localName] !== undefined ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
add(ev.target);
|
|
|
|
process();
|
2017-08-03 16:18:05 +02:00
|
|
|
}
|
2016-08-13 22:42:58 +02:00
|
|
|
};
|
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const domWatcherInterface = {
|
2017-10-21 19:43:46 +02:00
|
|
|
onDOMCreated: function() {
|
|
|
|
if ( vAPI instanceof Object === false ) { return; }
|
|
|
|
if ( vAPI.domCollapser instanceof Object === false ) {
|
|
|
|
if ( vAPI.domWatcher instanceof Object ) {
|
|
|
|
vAPI.domWatcher.removeListener(domWatcherInterface);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Listener to collapse blocked resources.
|
|
|
|
// - Future requests not blocked yet
|
|
|
|
// - Elements dynamically added to the page
|
|
|
|
// - Elements which resource URL changes
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/7
|
|
|
|
// Preferring getElementsByTagName over querySelectorAll:
|
|
|
|
// http://jsperf.com/queryselectorall-vs-getelementsbytagname/145
|
2018-12-26 16:45:19 +01:00
|
|
|
const elems = document.images ||
|
|
|
|
document.getElementsByTagName('img');
|
|
|
|
for ( const elem of elems ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( elem.complete ) {
|
|
|
|
add(elem);
|
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
addMany(document.embeds || document.getElementsByTagName('embed'));
|
|
|
|
addMany(document.getElementsByTagName('object'));
|
|
|
|
addIFrames(document.getElementsByTagName('iframe'));
|
|
|
|
process(0);
|
|
|
|
|
|
|
|
document.addEventListener('error', onResourceFailed, true);
|
|
|
|
|
|
|
|
vAPI.shutdown.add(function() {
|
|
|
|
document.removeEventListener('error', onResourceFailed, true);
|
|
|
|
if ( processTimer !== undefined ) {
|
|
|
|
clearTimeout(processTimer);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onDOMChanged: function(addedNodes) {
|
2018-12-26 16:45:19 +01:00
|
|
|
if ( addedNodes.length === 0 ) { return; }
|
|
|
|
for ( const node of addedNodes ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( node.localName === 'iframe' ) {
|
|
|
|
addIFrame(node);
|
|
|
|
}
|
|
|
|
if ( node.childElementCount === 0 ) { continue; }
|
2018-12-26 16:45:19 +01:00
|
|
|
const iframes = node.getElementsByTagName('iframe');
|
2016-08-12 14:55:35 +02:00
|
|
|
if ( iframes.length !== 0 ) {
|
|
|
|
addIFrames(iframes);
|
|
|
|
}
|
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
process();
|
2016-06-29 01:45:11 +02:00
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
};
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( vAPI.domWatcher instanceof Object ) {
|
|
|
|
vAPI.domWatcher.addListener(domWatcherInterface);
|
|
|
|
}
|
2015-03-29 18:13:28 +02:00
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
return { add, addMany, addIFrame, addIFrames, process };
|
2015-03-29 18:13:28 +02:00
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2016-06-28 01:09:04 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
vAPI.domSurveyor = (function() {
|
2018-12-26 16:45:19 +01:00
|
|
|
const messaging = vAPI.messaging;
|
|
|
|
const queriedIds = new Set();
|
|
|
|
const queriedClasses = new Set();
|
2018-12-31 17:50:40 +01:00
|
|
|
const maxSurveyNodes = 65536;
|
|
|
|
const maxSurveyTimeSlice = 4;
|
|
|
|
const maxSurveyBuffer = 64;
|
2018-12-26 16:45:19 +01:00
|
|
|
|
|
|
|
let domFilterer,
|
2017-10-25 17:42:18 +02:00
|
|
|
hostname = '',
|
2016-10-06 16:49:46 +02:00
|
|
|
surveyCost = 0;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2018-12-31 17:50:40 +01:00
|
|
|
const pendingNodes = {
|
|
|
|
nodeLists: [],
|
|
|
|
buffer: [
|
|
|
|
null, null, null, null, null, null, null, null,
|
|
|
|
null, null, null, null, null, null, null, null,
|
|
|
|
null, null, null, null, null, null, null, null,
|
|
|
|
null, null, null, null, null, null, null, null,
|
|
|
|
null, null, null, null, null, null, null, null,
|
|
|
|
null, null, null, null, null, null, null, null,
|
|
|
|
null, null, null, null, null, null, null, null,
|
|
|
|
null, null, null, null, null, null, null, null,
|
|
|
|
],
|
|
|
|
j: 0,
|
|
|
|
accepted: 0,
|
|
|
|
iterated: 0,
|
|
|
|
stopped: false,
|
|
|
|
add: function(nodes) {
|
|
|
|
if ( nodes.length === 0 || this.accepted >= maxSurveyNodes ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.nodeLists.push(nodes);
|
|
|
|
this.accepted += nodes.length;
|
|
|
|
},
|
|
|
|
next: function() {
|
|
|
|
if ( this.nodeLists.length === 0 || this.stopped ) { return 0; }
|
|
|
|
const nodeLists = this.nodeLists;
|
|
|
|
let ib = 0;
|
|
|
|
do {
|
|
|
|
const nodeList = nodeLists[0];
|
|
|
|
let j = this.j;
|
|
|
|
let n = j + maxSurveyBuffer - ib;
|
|
|
|
if ( n > nodeList.length ) {
|
|
|
|
n = nodeList.length;
|
|
|
|
}
|
|
|
|
for ( let i = j; i < n; i++ ) {
|
|
|
|
this.buffer[ib++] = nodeList[j++];
|
|
|
|
}
|
|
|
|
if ( j !== nodeList.length ) {
|
|
|
|
this.j = j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
this.j = 0;
|
|
|
|
this.nodeLists.shift();
|
|
|
|
} while ( ib < maxSurveyBuffer && nodeLists.length !== 0 );
|
|
|
|
this.iterated += ib;
|
|
|
|
if ( this.iterated >= maxSurveyNodes ) {
|
|
|
|
this.nodeLists = [];
|
|
|
|
this.stopped = true;
|
|
|
|
//console.info(`domSurveyor> Surveyed a total of ${this.iterated} nodes. Enough.`);
|
|
|
|
}
|
|
|
|
return ib;
|
|
|
|
},
|
|
|
|
hasNodes: function() {
|
|
|
|
return this.nodeLists.length !== 0;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// Extract all classes/ids: these will be passed to the cosmetic
|
|
|
|
// filtering engine, and in return we will obtain only the relevant
|
|
|
|
// CSS selectors.
|
|
|
|
const reWhitespace = /\s/;
|
|
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/672
|
|
|
|
// http://www.w3.org/TR/2014/REC-html5-20141028/infrastructure.html#space-separated-tokens
|
|
|
|
// http://jsperf.com/enumerate-classes/6
|
|
|
|
|
|
|
|
const surveyPhase1 = function() {
|
|
|
|
//console.time('dom surveyor/surveying');
|
|
|
|
const t0 = performance.now();
|
|
|
|
const rews = reWhitespace;
|
|
|
|
const ids = [];
|
|
|
|
const classes = [];
|
|
|
|
const nodes = pendingNodes.buffer;
|
|
|
|
const deadline = t0 + maxSurveyTimeSlice;
|
|
|
|
let qids = queriedIds;
|
|
|
|
let qcls = queriedClasses;
|
|
|
|
let processed = 0;
|
|
|
|
for (;;) {
|
|
|
|
const n = pendingNodes.next();
|
|
|
|
if ( n === 0 ) { break; }
|
|
|
|
for ( let i = 0; i < n; i++ ) {
|
|
|
|
const node = nodes[i]; nodes[i] = null;
|
|
|
|
let v = node.id;
|
|
|
|
if ( typeof v === 'string' && v.length !== 0 ) {
|
|
|
|
v = v.trim();
|
|
|
|
if ( qids.has(v) === false && v.length !== 0 ) {
|
|
|
|
ids.push(v); qids.add(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let vv = node.className;
|
|
|
|
if ( typeof vv === 'string' && vv.length !== 0 ) {
|
|
|
|
if ( rews.test(vv) === false ) {
|
|
|
|
if ( qcls.has(vv) === false ) {
|
|
|
|
classes.push(vv); qcls.add(vv);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vv = node.classList;
|
|
|
|
let j = vv.length;
|
|
|
|
while ( j-- ) {
|
|
|
|
const v = vv[j];
|
|
|
|
if ( qcls.has(v) === false ) {
|
|
|
|
classes.push(v); qcls.add(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
processed += n;
|
|
|
|
if ( performance.now() >= deadline ) { break; }
|
|
|
|
}
|
|
|
|
const t1 = performance.now();
|
|
|
|
surveyCost += t1 - t0;
|
|
|
|
//console.info(`domSurveyor> Surveyed ${processed} nodes in ${(t1-t0).toFixed(2)} ms`);
|
|
|
|
// Phase 2: Ask main process to lookup relevant cosmetic filters.
|
|
|
|
if ( ids.length !== 0 || classes.length !== 0 ) {
|
2019-09-17 21:15:01 +02:00
|
|
|
messaging.send('contentscript', {
|
|
|
|
what: 'retrieveGenericCosmeticSelectors',
|
|
|
|
hostname,
|
|
|
|
ids,
|
|
|
|
classes,
|
|
|
|
exceptions: domFilterer.exceptions,
|
|
|
|
cost: surveyCost,
|
|
|
|
}).then(response => {
|
|
|
|
surveyPhase3(response);
|
|
|
|
});
|
2018-12-31 17:50:40 +01:00
|
|
|
} else {
|
|
|
|
surveyPhase3(null);
|
|
|
|
}
|
|
|
|
//console.timeEnd('dom surveyor/surveying');
|
|
|
|
};
|
|
|
|
|
|
|
|
const surveyTimer = new vAPI.SafeAnimationFrame(surveyPhase1);
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
// This is to shutdown the surveyor if result of surveying keeps being
|
2017-10-24 22:38:51 +02:00
|
|
|
// fruitless. This is useful on long-lived web page. I arbitrarily
|
|
|
|
// picked 5 minutes before the surveyor is allowed to shutdown. I also
|
|
|
|
// arbitrarily picked 256 misses before the surveyor is allowed to
|
|
|
|
// shutdown.
|
2018-12-26 16:45:19 +01:00
|
|
|
let canShutdownAfter = Date.now() + 300000,
|
2017-10-24 22:38:51 +02:00
|
|
|
surveyingMissCount = 0;
|
2017-10-21 19:43:46 +02:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
// Handle main process' response.
|
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const surveyPhase3 = function(response) {
|
|
|
|
const result = response && response.result;
|
|
|
|
let mustCommit = false;
|
2015-04-08 01:10:03 +02:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
if ( result ) {
|
2018-12-26 16:45:19 +01:00
|
|
|
let selectors = result.simple;
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( Array.isArray(selectors) && selectors.length !== 0 ) {
|
|
|
|
domFilterer.addCSSRule(
|
|
|
|
selectors,
|
2017-10-23 15:01:00 +02:00
|
|
|
'display:none!important;',
|
2017-10-21 19:43:46 +02:00
|
|
|
{ type: 'simple' }
|
|
|
|
);
|
|
|
|
mustCommit = true;
|
2014-08-12 18:19:54 +02:00
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
selectors = result.complex;
|
|
|
|
if ( Array.isArray(selectors) && selectors.length !== 0 ) {
|
|
|
|
domFilterer.addCSSRule(
|
|
|
|
selectors,
|
2017-10-23 15:01:00 +02:00
|
|
|
'display:none!important;',
|
2017-10-21 19:43:46 +02:00
|
|
|
{ type: 'complex' }
|
|
|
|
);
|
|
|
|
mustCommit = true;
|
2014-08-12 18:19:54 +02:00
|
|
|
}
|
2017-10-26 12:18:03 +02:00
|
|
|
selectors = result.injected;
|
|
|
|
if ( typeof selectors === 'string' && selectors.length !== 0 ) {
|
|
|
|
domFilterer.addCSSRule(
|
|
|
|
selectors,
|
|
|
|
'display:none!important;',
|
|
|
|
{ injected: true }
|
|
|
|
);
|
|
|
|
mustCommit = true;
|
|
|
|
}
|
2019-05-16 19:44:49 +02:00
|
|
|
selectors = result.excepted;
|
|
|
|
if ( Array.isArray(selectors) && selectors.length !== 0 ) {
|
|
|
|
domFilterer.exceptCSSRules(selectors);
|
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
}
|
2016-07-12 19:29:30 +02:00
|
|
|
|
2018-12-31 17:50:40 +01:00
|
|
|
if ( pendingNodes.stopped === false ) {
|
|
|
|
if ( pendingNodes.hasNodes() ) {
|
|
|
|
surveyTimer.start(1);
|
|
|
|
}
|
|
|
|
if ( mustCommit ) {
|
|
|
|
surveyingMissCount = 0;
|
|
|
|
canShutdownAfter = Date.now() + 300000;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
surveyingMissCount += 1;
|
|
|
|
if ( surveyingMissCount < 256 || Date.now() < canShutdownAfter ) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-24 22:38:51 +02:00
|
|
|
}
|
2014-07-04 22:47:34 +02:00
|
|
|
|
2017-12-13 14:02:55 +01:00
|
|
|
//console.info('dom surveyor shutting down: too many misses');
|
2014-09-17 01:16:18 +02:00
|
|
|
|
2017-10-24 22:38:51 +02:00
|
|
|
surveyTimer.clear();
|
2017-10-21 19:43:46 +02:00
|
|
|
vAPI.domWatcher.removeListener(domWatcherInterface);
|
|
|
|
vAPI.domSurveyor = null;
|
2016-08-12 14:55:35 +02:00
|
|
|
};
|
2014-07-04 22:47:34 +02:00
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const domWatcherInterface = {
|
2017-10-21 19:43:46 +02:00
|
|
|
onDOMCreated: function() {
|
|
|
|
if (
|
|
|
|
vAPI instanceof Object === false ||
|
2017-10-24 22:38:51 +02:00
|
|
|
vAPI.domSurveyor instanceof Object === false ||
|
2017-10-21 19:43:46 +02:00
|
|
|
vAPI.domFilterer instanceof Object === false
|
|
|
|
) {
|
|
|
|
if ( vAPI instanceof Object ) {
|
|
|
|
if ( vAPI.domWatcher instanceof Object ) {
|
|
|
|
vAPI.domWatcher.removeListener(domWatcherInterface);
|
|
|
|
}
|
2017-10-24 22:38:51 +02:00
|
|
|
vAPI.domSurveyor = null;
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2017-10-23 18:21:37 +02:00
|
|
|
//console.time('dom surveyor/dom layout created');
|
2017-10-21 19:43:46 +02:00
|
|
|
domFilterer = vAPI.domFilterer;
|
2018-12-31 17:50:40 +01:00
|
|
|
pendingNodes.add(document.querySelectorAll('[id],[class]'));
|
2017-10-21 19:43:46 +02:00
|
|
|
surveyTimer.start();
|
2017-10-23 18:21:37 +02:00
|
|
|
//console.timeEnd('dom surveyor/dom layout created');
|
2017-10-21 19:43:46 +02:00
|
|
|
},
|
|
|
|
onDOMChanged: function(addedNodes) {
|
|
|
|
if ( addedNodes.length === 0 ) { return; }
|
2017-10-23 18:21:37 +02:00
|
|
|
//console.time('dom surveyor/dom layout changed');
|
2018-12-26 16:45:19 +01:00
|
|
|
let i = addedNodes.length;
|
2017-10-21 19:43:46 +02:00
|
|
|
while ( i-- ) {
|
2018-12-26 16:45:19 +01:00
|
|
|
const node = addedNodes[i];
|
2018-12-31 17:50:40 +01:00
|
|
|
pendingNodes.add([ node ]);
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( node.childElementCount === 0 ) { continue; }
|
2018-12-31 17:50:40 +01:00
|
|
|
pendingNodes.add(node.querySelectorAll('[id],[class]'));
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
2018-12-31 17:50:40 +01:00
|
|
|
if ( pendingNodes.hasNodes() ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
surveyTimer.start(1);
|
|
|
|
}
|
2017-10-23 18:21:37 +02:00
|
|
|
//console.timeEnd('dom surveyor/dom layout changed');
|
2016-06-28 15:06:14 +02:00
|
|
|
}
|
2014-09-16 21:39:21 +02:00
|
|
|
};
|
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const start = function(details) {
|
2017-10-25 17:42:18 +02:00
|
|
|
if ( vAPI.domWatcher instanceof Object === false ) { return; }
|
|
|
|
hostname = details.hostname;
|
2017-10-21 19:43:46 +02:00
|
|
|
vAPI.domWatcher.addListener(domWatcherInterface);
|
2017-10-25 17:42:18 +02:00
|
|
|
};
|
2015-04-08 01:10:03 +02:00
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
return { start };
|
2015-04-08 01:10:03 +02:00
|
|
|
})();
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
2014-09-14 22:20:40 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
// Bootstrapping allows all components of the content script to be launched
|
|
|
|
// if/when needed.
|
2016-08-14 03:45:01 +02:00
|
|
|
|
2019-01-27 23:07:40 +01:00
|
|
|
vAPI.bootstrap = (function() {
|
2017-10-21 19:43:46 +02:00
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const bootstrapPhase2 = function() {
|
2017-10-21 19:43:46 +02:00
|
|
|
// This can happen on Firefox. For instance:
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1893
|
|
|
|
if ( window.location === null ) { return; }
|
2018-12-26 16:45:19 +01:00
|
|
|
if ( vAPI instanceof Object === false ) { return; }
|
2017-10-25 17:42:18 +02:00
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
vAPI.messaging.send('contentscript', {
|
|
|
|
what: 'shouldRenderNoscriptTags',
|
|
|
|
});
|
2018-09-01 00:47:02 +02:00
|
|
|
|
2017-10-25 17:42:18 +02:00
|
|
|
if ( vAPI.domWatcher instanceof Object ) {
|
2017-10-21 19:43:46 +02:00
|
|
|
vAPI.domWatcher.start();
|
2016-08-13 22:42:58 +02:00
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
|
2017-10-25 17:42:18 +02:00
|
|
|
// Element picker works only in top window for now.
|
|
|
|
if (
|
|
|
|
window !== window.top ||
|
|
|
|
vAPI.domFilterer instanceof Object === false
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
|
2019-09-18 18:17:45 +02:00
|
|
|
// To be used by element picker/zapper.
|
|
|
|
vAPI.mouseClick = { x: -1, y: -1 };
|
2017-10-21 19:43:46 +02:00
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const onMouseClick = function(ev) {
|
2019-09-18 18:17:45 +02:00
|
|
|
if ( ev.isTrusted === false ) { return; }
|
|
|
|
vAPI.mouseClick.x = ev.clientX;
|
|
|
|
vAPI.mouseClick.y = ev.clientY;
|
|
|
|
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/1143
|
|
|
|
// Find a link under the mouse, to try to avoid confusing new tabs
|
|
|
|
// as nuisance popups.
|
2018-12-26 16:45:19 +01:00
|
|
|
let elem = ev.target;
|
2017-10-21 19:43:46 +02:00
|
|
|
while ( elem !== null && elem.localName !== 'a' ) {
|
|
|
|
elem = elem.parentElement;
|
2016-08-13 22:42:58 +02:00
|
|
|
}
|
2019-09-18 18:17:45 +02:00
|
|
|
if ( elem === null ) { return; }
|
2019-09-17 21:15:01 +02:00
|
|
|
vAPI.messaging.send('contentscript', {
|
2019-09-18 18:17:45 +02:00
|
|
|
what: 'maybeGoodPopup',
|
|
|
|
url: elem.href || '',
|
2019-09-17 21:15:01 +02:00
|
|
|
});
|
2017-10-21 19:43:46 +02:00
|
|
|
};
|
2016-08-13 22:42:58 +02:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
document.addEventListener('mousedown', onMouseClick, true);
|
2015-01-02 03:14:53 +01:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
// https://github.com/gorhill/uMatrix/issues/144
|
|
|
|
vAPI.shutdown.add(function() {
|
|
|
|
document.removeEventListener('mousedown', onMouseClick, true);
|
|
|
|
});
|
2017-10-21 19:43:46 +02:00
|
|
|
};
|
|
|
|
|
2019-01-27 23:07:40 +01:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/403
|
|
|
|
// If there was a spurious port disconnection -- in which case the
|
|
|
|
// response is expressly set to `null`, rather than undefined or
|
|
|
|
// an object -- let's stay around, we may be given the opportunity
|
|
|
|
// to try bootstrapping again later.
|
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
const bootstrapPhase1 = function(response) {
|
2019-09-29 18:26:58 +02:00
|
|
|
if ( response instanceof Object === false ) { return; }
|
|
|
|
|
2019-01-27 23:07:40 +01:00
|
|
|
vAPI.bootstrap = undefined;
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
// cosmetic filtering engine aka 'cfe'
|
2018-12-26 16:45:19 +01:00
|
|
|
const cfeDetails = response && response.specificCosmeticFilters;
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( !cfeDetails || !cfeDetails.ready ) {
|
|
|
|
vAPI.domWatcher = vAPI.domCollapser = vAPI.domFilterer =
|
|
|
|
vAPI.domSurveyor = vAPI.domIsLoaded = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( response.noCosmeticFiltering ) {
|
|
|
|
vAPI.domFilterer = null;
|
|
|
|
vAPI.domSurveyor = null;
|
|
|
|
} else {
|
2018-12-26 16:45:19 +01:00
|
|
|
const domFilterer = vAPI.domFilterer;
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( response.noGenericCosmeticFiltering || cfeDetails.noDOMSurveying ) {
|
|
|
|
vAPI.domSurveyor = null;
|
|
|
|
}
|
|
|
|
domFilterer.exceptions = cfeDetails.exceptionFilters;
|
2017-10-23 18:21:37 +02:00
|
|
|
domFilterer.hideNodeAttr = cfeDetails.hideNodeAttr;
|
|
|
|
domFilterer.hideNodeStyleSheetInjected =
|
|
|
|
cfeDetails.hideNodeStyleSheetInjected === true;
|
2017-10-21 19:43:46 +02:00
|
|
|
domFilterer.addCSSRule(
|
|
|
|
cfeDetails.declarativeFilters,
|
2017-10-23 18:21:37 +02:00
|
|
|
'display:none!important;'
|
2017-10-21 19:43:46 +02:00
|
|
|
);
|
|
|
|
domFilterer.addCSSRule(
|
|
|
|
cfeDetails.highGenericHideSimple,
|
2017-10-23 15:01:00 +02:00
|
|
|
'display:none!important;',
|
2017-10-23 18:21:37 +02:00
|
|
|
{ type: 'simple', lazy: true }
|
2017-10-21 19:43:46 +02:00
|
|
|
);
|
|
|
|
domFilterer.addCSSRule(
|
|
|
|
cfeDetails.highGenericHideComplex,
|
2017-10-23 15:01:00 +02:00
|
|
|
'display:none!important;',
|
2017-10-23 18:21:37 +02:00
|
|
|
{ type: 'complex', lazy: true }
|
|
|
|
);
|
|
|
|
domFilterer.addCSSRule(
|
|
|
|
cfeDetails.injectedHideFilters,
|
|
|
|
'display:none!important;',
|
|
|
|
{ injected: true }
|
2017-10-21 19:43:46 +02:00
|
|
|
);
|
|
|
|
domFilterer.addProceduralSelectors(cfeDetails.proceduralFilters);
|
2019-05-16 19:44:49 +02:00
|
|
|
domFilterer.exceptCSSRules(cfeDetails.exceptedFilters);
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
|
|
|
|
2017-10-23 18:21:37 +02:00
|
|
|
if ( cfeDetails.networkFilters.length !== 0 ) {
|
2017-10-22 14:59:29 +02:00
|
|
|
vAPI.userStylesheet.add(
|
2017-10-23 18:21:37 +02:00
|
|
|
cfeDetails.networkFilters + '\n{display:none!important;}');
|
2017-10-22 14:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
vAPI.userStylesheet.apply();
|
|
|
|
|
2017-10-23 15:01:00 +02:00
|
|
|
// Library of resources is located at:
|
|
|
|
// https://github.com/gorhill/uBlock/blob/master/assets/ublock/resources.txt
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( response.scriptlets ) {
|
|
|
|
vAPI.injectScriptlet(document, response.scriptlets);
|
|
|
|
vAPI.injectedScripts = response.scriptlets;
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
|
|
|
|
2017-10-25 17:42:18 +02:00
|
|
|
if ( vAPI.domSurveyor instanceof Object ) {
|
|
|
|
vAPI.domSurveyor.start(cfeDetails);
|
|
|
|
}
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/587
|
|
|
|
// If no filters were found, maybe the script was injected before
|
|
|
|
// uBlock's process was fully initialized. When this happens, pages
|
|
|
|
// won't be cleaned right after browser launch.
|
|
|
|
if (
|
|
|
|
typeof document.readyState === 'string' &&
|
|
|
|
document.readyState !== 'loading'
|
|
|
|
) {
|
|
|
|
bootstrapPhase2();
|
|
|
|
} else {
|
2018-12-26 16:45:19 +01:00
|
|
|
document.addEventListener(
|
|
|
|
'DOMContentLoaded',
|
|
|
|
bootstrapPhase2,
|
|
|
|
{ once: true }
|
|
|
|
);
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-27 23:07:40 +01:00
|
|
|
return function() {
|
2019-09-17 21:15:01 +02:00
|
|
|
vAPI.messaging.send('contentscript', {
|
|
|
|
what: 'retrieveContentScriptParameters',
|
|
|
|
url: window.location.href,
|
|
|
|
isRootFrame: window === window.top,
|
|
|
|
charset: document.characterSet,
|
|
|
|
}).then(response => {
|
|
|
|
bootstrapPhase1(response);
|
|
|
|
});
|
2019-01-27 23:07:40 +01:00
|
|
|
};
|
2017-10-21 19:43:46 +02:00
|
|
|
})();
|
2015-01-02 03:14:53 +01:00
|
|
|
|
2019-01-27 23:07:40 +01:00
|
|
|
// This starts bootstrap process.
|
|
|
|
vAPI.bootstrap();
|
|
|
|
|
2015-01-02 03:14:53 +01:00
|
|
|
/******************************************************************************/
|
2016-06-28 01:09:04 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
2017-08-17 14:25:02 +02:00
|
|
|
|
2018-12-26 16:45:19 +01:00
|
|
|
}
|
|
|
|
// <<<<<<<< end of HUGE-IF-BLOCK
|