2014-06-23 16:42:43 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2016-03-06 08:51:06 -07:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-12-26 08:45:19 -07:00
|
|
|
Copyright (C) 2014-present Raymond Hill
|
2014-06-23 16:42:43 -06: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-27 17:09:04 -06:00
|
|
|
'use strict';
|
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2017-10-21 11:43:46 -06:00
|
|
|
+--> domCollapser
|
|
|
|
|
|
|
|
|
|
|
|
|
|
domWatcher--+
|
|
|
|
| +-- domSurveyor
|
|
|
|
| |
|
2021-02-20 05:05:19 -07:00
|
|
|
+--> domFilterer --+-- [domLogger]
|
2021-02-19 06:38:07 -07:00
|
|
|
| |
|
2021-02-20 05:05:19 -07:00
|
|
|
| +-- [domInspector]
|
2021-02-19 06:38:07 -07:00
|
|
|
|
|
|
|
|
[domProceduralFilterer]
|
2016-08-12 06:55:35 -06: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.
|
|
|
|
|
2021-02-19 06:38:07 -07:00
|
|
|
domProceduralFilterer:
|
|
|
|
Enforce the filtering of DOM elements through procedural cosmetic filters.
|
|
|
|
Loaded on demand, only when needed.
|
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
domSurveyor:
|
|
|
|
Surveys the DOM to find new cosmetic filters to apply to the current page.
|
|
|
|
|
2017-10-21 11:43:46 -06: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 06:55:35 -06:00
|
|
|
If page is whitelisted:
|
|
|
|
- domWatcher: off
|
|
|
|
- domCollapser: off
|
|
|
|
- domFilterer: off
|
|
|
|
- domSurveyor: off
|
2017-10-21 11:43:46 -06:00
|
|
|
- domLogger: off
|
|
|
|
|
2016-08-12 06:55:35 -06: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 11:43:46 -06:00
|
|
|
- domLogger: off
|
2016-08-12 06:55:35 -06:00
|
|
|
|
|
|
|
If generic cosmetic filtering is disabled:
|
|
|
|
- domWatcher: on
|
|
|
|
- domCollapser: on
|
|
|
|
- domFilterer: on
|
|
|
|
- domSurveyor: off
|
2017-10-21 11:43:46 -06: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 06:55:35 -06:00
|
|
|
|
|
|
|
Additionally, the domSurveyor can turn itself off once it decides that
|
|
|
|
it has become pointless (repeatedly not finding new cosmetic filters).
|
2014-06-23 16:42:43 -06:00
|
|
|
|
2017-10-21 11:43:46 -06:00
|
|
|
The domFilterer makes use of platform-dependent user stylesheets[1].
|
|
|
|
|
|
|
|
[1] "user stylesheets" refer to local CSS rules which have priority over,
|
2022-03-12 11:50:46 -07:00
|
|
|
and can't be overridden by a web page's own CSS rules.
|
2016-12-16 14:25:36 -07:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
*/
|
2014-06-23 16:42:43 -06:00
|
|
|
|
2017-08-17 06:25:02 -06: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 08:45:19 -07:00
|
|
|
// >>>>>>>> start of HUGE-IF-BLOCK
|
|
|
|
if ( typeof vAPI === 'object' && !vAPI.contentScript ) {
|
2017-10-27 12:22:45 -06:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
vAPI.contentScript = true;
|
2017-08-17 06:25:02 -06:00
|
|
|
|
2020-07-22 08:21:16 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2020-07-24 16:50:12 -06:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/688#issuecomment-663657508
|
|
|
|
{
|
2020-07-29 05:38:49 -06:00
|
|
|
let context = self;
|
|
|
|
try {
|
|
|
|
while (
|
|
|
|
context !== self.top &&
|
2021-01-08 11:12:48 -07:00
|
|
|
context.location.href.startsWith('about:blank') &&
|
|
|
|
context.parent.location.href
|
2020-07-29 05:38:49 -06:00
|
|
|
) {
|
|
|
|
context = context.parent;
|
2020-07-24 16:50:12 -06:00
|
|
|
}
|
2020-07-29 05:38:49 -06:00
|
|
|
} catch(ex) {
|
2020-07-24 16:50:12 -06:00
|
|
|
}
|
2020-07-29 05:38:49 -06:00
|
|
|
vAPI.effectiveSelf = context;
|
2020-07-24 16:50:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2020-07-22 08:21:16 -06:00
|
|
|
vAPI.userStylesheet = {
|
|
|
|
added: new Set(),
|
|
|
|
removed: new Set(),
|
|
|
|
apply: function(callback) {
|
|
|
|
if ( this.added.size === 0 && this.removed.size === 0 ) { return; }
|
|
|
|
vAPI.messaging.send('vapi', {
|
|
|
|
what: 'userCSS',
|
|
|
|
add: Array.from(this.added),
|
|
|
|
remove: Array.from(this.removed),
|
|
|
|
}).then(( ) => {
|
|
|
|
if ( callback instanceof Function === false ) { return; }
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
this.added.clear();
|
|
|
|
this.removed.clear();
|
|
|
|
},
|
|
|
|
add: function(cssText, now) {
|
|
|
|
if ( cssText === '' ) { return; }
|
|
|
|
this.added.add(cssText);
|
|
|
|
if ( now ) { this.apply(); }
|
|
|
|
},
|
|
|
|
remove: function(cssText, now) {
|
|
|
|
if ( cssText === '' ) { return; }
|
|
|
|
this.removed.add(cssText);
|
|
|
|
if ( now ) { this.apply(); }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-27 17:09:04 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
2017-10-24 14:38:51 -06: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-27 17:09:04 -06:00
|
|
|
|
2016-11-12 11:38:41 -07:00
|
|
|
// https://github.com/gorhill/uBlock/issues/2147
|
|
|
|
|
2020-07-22 08:21:16 -06:00
|
|
|
vAPI.SafeAnimationFrame = class {
|
|
|
|
constructor(callback) {
|
|
|
|
this.fid = this.tid = undefined;
|
|
|
|
this.callback = callback;
|
|
|
|
}
|
|
|
|
start(delay) {
|
2019-09-30 05:50:35 -06:00
|
|
|
if ( self.vAPI instanceof Object === false ) { return; }
|
2017-10-21 11:43:46 -06:00
|
|
|
if ( delay === undefined ) {
|
2018-12-31 09:50:40 -07:00
|
|
|
if ( this.fid === undefined ) {
|
|
|
|
this.fid = requestAnimationFrame(( ) => { this.onRAF(); } );
|
2017-10-21 11:43:46 -06:00
|
|
|
}
|
2018-12-31 09:50:40 -07:00
|
|
|
if ( this.tid === undefined ) {
|
|
|
|
this.tid = vAPI.setTimeout(( ) => { this.onSTO(); }, 20000);
|
2017-10-21 11:43:46 -06:00
|
|
|
}
|
2017-10-24 14:38:51 -06:00
|
|
|
return;
|
|
|
|
}
|
2018-12-31 09:50:40 -07:00
|
|
|
if ( this.fid === undefined && this.tid === undefined ) {
|
|
|
|
this.tid = vAPI.setTimeout(( ) => { this.macroToMicro(); }, delay);
|
2017-10-21 11:43:46 -06:00
|
|
|
}
|
2020-07-22 08:21:16 -06:00
|
|
|
}
|
|
|
|
clear() {
|
2018-12-31 09:50:40 -07:00
|
|
|
if ( this.fid !== undefined ) {
|
|
|
|
cancelAnimationFrame(this.fid);
|
|
|
|
this.fid = undefined;
|
|
|
|
}
|
|
|
|
if ( this.tid !== undefined ) {
|
|
|
|
clearTimeout(this.tid);
|
|
|
|
this.tid = undefined;
|
|
|
|
}
|
2020-07-22 08:21:16 -06:00
|
|
|
}
|
|
|
|
macroToMicro() {
|
2018-12-31 09:50:40 -07:00
|
|
|
this.tid = undefined;
|
2017-10-24 14:38:51 -06:00
|
|
|
this.start();
|
2020-07-22 08:21:16 -06:00
|
|
|
}
|
|
|
|
onRAF() {
|
2018-12-31 09:50:40 -07:00
|
|
|
if ( this.tid !== undefined ) {
|
|
|
|
clearTimeout(this.tid);
|
|
|
|
this.tid = undefined;
|
|
|
|
}
|
|
|
|
this.fid = undefined;
|
|
|
|
this.callback();
|
2020-07-22 08:21:16 -06:00
|
|
|
}
|
|
|
|
onSTO() {
|
2018-12-31 09:50:40 -07:00
|
|
|
if ( this.fid !== undefined ) {
|
|
|
|
cancelAnimationFrame(this.fid);
|
|
|
|
this.fid = undefined;
|
|
|
|
}
|
|
|
|
this.tid = undefined;
|
|
|
|
this.callback();
|
2020-07-22 08:21:16 -06:00
|
|
|
}
|
2017-08-21 10:04:35 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-05-11 08:40:34 -06: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 09:40:14 -06:00
|
|
|
const newEvents = new Set();
|
|
|
|
const allEvents = new Set();
|
2019-05-11 08:40:34 -06:00
|
|
|
let timer;
|
|
|
|
|
|
|
|
const send = function() {
|
2019-09-17 13:15:01 -06: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 09:40:14 -06:00
|
|
|
for ( const event of newEvents ) {
|
|
|
|
allEvents.add(event);
|
|
|
|
}
|
|
|
|
newEvents.clear();
|
2019-05-11 08:40:34 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
const sendAsync = function() {
|
|
|
|
if ( timer !== undefined ) { return; }
|
|
|
|
timer = self.requestIdleCallback(
|
|
|
|
( ) => { timer = undefined; send(); },
|
2019-09-17 13:15:01 -06:00
|
|
|
{ timeout: 2063 }
|
2019-05-11 08:40:34 -06:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const listener = function(ev) {
|
|
|
|
if ( ev.isTrusted !== true ) { return; }
|
|
|
|
if ( ev.disposition !== 'enforce' ) { return; }
|
2019-06-24 09:40:14 -06:00
|
|
|
const json = JSON.stringify({
|
2019-05-11 08:40:34 -06:00
|
|
|
url: ev.blockedURL || ev.blockedURI,
|
|
|
|
policy: ev.originalPolicy,
|
|
|
|
directive: ev.effectiveDirective || ev.violatedDirective,
|
2019-06-24 09:40:14 -06:00
|
|
|
});
|
|
|
|
if ( allEvents.has(json) ) { return; }
|
|
|
|
newEvents.add(json);
|
2019-05-11 08:40:34 -06:00
|
|
|
sendAsync();
|
|
|
|
};
|
|
|
|
|
|
|
|
const stop = function() {
|
2019-06-24 09:40:14 -06:00
|
|
|
newEvents.clear();
|
|
|
|
allEvents.clear();
|
2019-05-11 08:40:34 -06: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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2020-07-22 08:21:16 -06:00
|
|
|
// vAPI.domWatcher
|
|
|
|
|
|
|
|
{
|
2019-10-31 09:07:11 -06:00
|
|
|
vAPI.domMutationTime = Date.now();
|
2017-07-23 07:56:43 -06:00
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
const addedNodeLists = [];
|
|
|
|
const removedNodeLists = [];
|
|
|
|
const addedNodes = [];
|
|
|
|
const ignoreTags = new Set([ 'br', 'head', 'link', 'meta', 'script', 'style' ]);
|
|
|
|
const listeners = [];
|
|
|
|
|
2020-07-29 05:38:49 -06:00
|
|
|
let domLayoutObserver;
|
|
|
|
let listenerIterator = [];
|
|
|
|
let listenerIteratorDirty = false;
|
|
|
|
let removedNodes = false;
|
|
|
|
let safeObserverHandlerTimer;
|
2016-12-16 14:25:36 -07:00
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
const safeObserverHandler = function() {
|
2019-10-31 09:07:11 -06:00
|
|
|
let i = addedNodeLists.length;
|
2017-10-21 11:43:46 -06:00
|
|
|
while ( i-- ) {
|
2018-12-26 08:45:19 -07:00
|
|
|
const nodeList = addedNodeLists[i];
|
|
|
|
let iNode = nodeList.length;
|
2017-10-21 11:43:46 -06:00
|
|
|
while ( iNode-- ) {
|
2018-12-26 08:45:19 -07:00
|
|
|
const node = nodeList[iNode];
|
2017-10-21 11:43:46 -06:00
|
|
|
if ( node.nodeType !== 1 ) { continue; }
|
|
|
|
if ( ignoreTags.has(node.localName) ) { continue; }
|
|
|
|
if ( node.parentElement === null ) { continue; }
|
2019-10-31 09:07:11 -06:00
|
|
|
addedNodes.push(node);
|
2016-12-16 14:25:36 -07:00
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
}
|
|
|
|
addedNodeLists.length = 0;
|
|
|
|
i = removedNodeLists.length;
|
|
|
|
while ( i-- && removedNodes === false ) {
|
2018-12-26 08:45:19 -07:00
|
|
|
const nodeList = removedNodeLists[i];
|
|
|
|
let iNode = nodeList.length;
|
2017-10-21 11:43:46 -06:00
|
|
|
while ( iNode-- ) {
|
|
|
|
if ( nodeList[iNode].nodeType !== 1 ) { continue; }
|
|
|
|
removedNodes = true;
|
|
|
|
break;
|
2016-12-16 14:25:36 -07:00
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
}
|
|
|
|
removedNodeLists.length = 0;
|
|
|
|
if ( addedNodes.length === 0 && removedNodes === false ) { return; }
|
2018-12-26 08:45:19 -07:00
|
|
|
for ( const listener of getListenerIterator() ) {
|
2019-09-30 16:21:24 -06:00
|
|
|
try { listener.onDOMChanged(addedNodes, removedNodes); }
|
|
|
|
catch (ex) { }
|
2017-10-21 11:43:46 -06:00
|
|
|
}
|
|
|
|
addedNodes.length = 0;
|
|
|
|
removedNodes = false;
|
2019-10-31 09:07:11 -06:00
|
|
|
vAPI.domMutationTime = Date.now();
|
2017-10-21 11:43:46 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/205
|
2020-12-10 10:51:26 -07:00
|
|
|
// Do not handle added node directly from within mutation observer.
|
2018-12-26 08:45:19 -07:00
|
|
|
const observerHandler = function(mutations) {
|
|
|
|
let i = mutations.length;
|
2017-10-21 11:43:46 -06:00
|
|
|
while ( i-- ) {
|
2018-12-26 08:45:19 -07:00
|
|
|
const mutation = mutations[i];
|
|
|
|
let nodeList = mutation.addedNodes;
|
2017-10-21 11:43:46 -06:00
|
|
|
if ( nodeList.length !== 0 ) {
|
|
|
|
addedNodeLists.push(nodeList);
|
2016-12-16 14:25:36 -07:00
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
nodeList = mutation.removedNodes;
|
|
|
|
if ( nodeList.length !== 0 ) {
|
|
|
|
removedNodeLists.push(nodeList);
|
2016-12-16 14:25:36 -07:00
|
|
|
}
|
|
|
|
}
|
2020-12-10 10:51:26 -07:00
|
|
|
if ( addedNodeLists.length !== 0 || removedNodeLists.length !== 0 ) {
|
2017-12-10 13:03:03 -07:00
|
|
|
safeObserverHandlerTimer.start(
|
|
|
|
addedNodeLists.length < 100 ? 1 : undefined
|
|
|
|
);
|
2017-10-21 11:43:46 -06:00
|
|
|
}
|
2016-12-16 14:25:36 -07:00
|
|
|
};
|
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
const startMutationObserver = function() {
|
2020-07-29 05:38:49 -06:00
|
|
|
if ( domLayoutObserver !== undefined ) { return; }
|
2017-10-21 11:43:46 -06:00
|
|
|
domLayoutObserver = new MutationObserver(observerHandler);
|
2021-10-14 07:08:08 -06:00
|
|
|
domLayoutObserver.observe(document, {
|
2017-10-21 11:43:46 -06:00
|
|
|
//attributeFilter: [ 'class', 'id' ],
|
|
|
|
//attributes: true,
|
|
|
|
childList: true,
|
|
|
|
subtree: true
|
|
|
|
});
|
|
|
|
safeObserverHandlerTimer = new vAPI.SafeAnimationFrame(safeObserverHandler);
|
|
|
|
vAPI.shutdown.add(cleanup);
|
|
|
|
};
|
2016-12-15 08:47:32 -07:00
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
const stopMutationObserver = function() {
|
2017-10-21 11:43:46 -06:00
|
|
|
if ( domLayoutObserver === undefined ) { return; }
|
|
|
|
cleanup();
|
|
|
|
vAPI.shutdown.remove(cleanup);
|
|
|
|
};
|
2016-12-15 08:47:32 -07:00
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
const getListenerIterator = function() {
|
2017-10-21 11:43:46 -06:00
|
|
|
if ( listenerIteratorDirty ) {
|
|
|
|
listenerIterator = listeners.slice();
|
|
|
|
listenerIteratorDirty = false;
|
2017-03-12 08:22:46 -06:00
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
return listenerIterator;
|
2016-12-15 08:47:32 -07:00
|
|
|
};
|
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
const addListener = function(listener) {
|
2017-10-21 11:43:46 -06:00
|
|
|
if ( listeners.indexOf(listener) !== -1 ) { return; }
|
|
|
|
listeners.push(listener);
|
|
|
|
listenerIteratorDirty = true;
|
2020-07-29 05:38:49 -06:00
|
|
|
if ( domLayoutObserver === undefined ) { return; }
|
2019-09-30 16:21:24 -06:00
|
|
|
try { listener.onDOMCreated(); }
|
|
|
|
catch (ex) { }
|
2017-10-25 09:42:18 -06:00
|
|
|
startMutationObserver();
|
2016-12-15 08:47:32 -07:00
|
|
|
};
|
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
const removeListener = function(listener) {
|
|
|
|
const pos = listeners.indexOf(listener);
|
2017-10-21 11:43:46 -06:00
|
|
|
if ( pos === -1 ) { return; }
|
|
|
|
listeners.splice(pos, 1);
|
|
|
|
listenerIteratorDirty = true;
|
|
|
|
if ( listeners.length === 0 ) {
|
|
|
|
stopMutationObserver();
|
2017-03-12 08:22:46 -06:00
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
};
|
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
const cleanup = function() {
|
2017-10-21 11:43:46 -06:00
|
|
|
if ( domLayoutObserver !== undefined ) {
|
|
|
|
domLayoutObserver.disconnect();
|
2020-10-31 08:18:42 -06:00
|
|
|
domLayoutObserver = undefined;
|
2016-12-15 08:47:32 -07:00
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
if ( safeObserverHandlerTimer !== undefined ) {
|
|
|
|
safeObserverHandlerTimer.clear();
|
|
|
|
safeObserverHandlerTimer = undefined;
|
2016-12-15 08:47:32 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
const start = function() {
|
|
|
|
for ( const listener of getListenerIterator() ) {
|
2019-09-30 16:21:24 -06:00
|
|
|
try { listener.onDOMCreated(); }
|
|
|
|
catch (ex) { }
|
2016-12-15 08:47:32 -07:00
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
startMutationObserver();
|
|
|
|
};
|
|
|
|
|
2020-07-22 08:21:16 -06:00
|
|
|
vAPI.domWatcher = { start, addListener, removeListener };
|
|
|
|
}
|
2016-12-15 08:47:32 -07:00
|
|
|
|
2017-10-21 11:43:46 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
2016-12-15 08:47:32 -07:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-10-21 11:43:46 -06:00
|
|
|
vAPI.injectScriptlet = function(doc, text) {
|
|
|
|
if ( !doc ) { return; }
|
2018-05-18 08:19:14 -06:00
|
|
|
let script;
|
2017-10-21 11:43:46 -06:00
|
|
|
try {
|
2018-05-18 08:19:14 -06:00
|
|
|
script = doc.createElement('script');
|
2017-10-21 11:43:46 -06:00
|
|
|
script.appendChild(doc.createTextNode(text));
|
2021-10-14 07:08:08 -06:00
|
|
|
(doc.head || doc.documentElement || doc).appendChild(script);
|
2017-10-21 11:43:46 -06:00
|
|
|
} catch (ex) {
|
2016-08-06 10:09:18 -06:00
|
|
|
}
|
2018-06-01 16:41:27 -06:00
|
|
|
if ( script ) {
|
2021-10-14 07:22:36 -06:00
|
|
|
script.remove();
|
2018-06-01 16:41:27 -06:00
|
|
|
script.textContent = '';
|
2018-05-18 08:19:14 -06:00
|
|
|
}
|
2016-08-06 10:09:18 -06:00
|
|
|
};
|
|
|
|
|
2017-10-21 11:43:46 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
/*******************************************************************************
|
2016-08-06 10:09:18 -06:00
|
|
|
|
2017-10-21 11:43:46 -06:00
|
|
|
The DOM filterer is the heart of uBO's cosmetic filtering.
|
2016-12-25 14:56:39 -07:00
|
|
|
|
2020-07-22 08:21:16 -06:00
|
|
|
DOMFilterer: adds procedural cosmetic filtering
|
2016-12-25 14:56:39 -07:00
|
|
|
|
2017-10-21 11:43:46 -06:00
|
|
|
*/
|
2016-12-25 14:56:39 -07:00
|
|
|
|
2021-02-17 07:12:00 -07:00
|
|
|
vAPI.hideStyle = 'display:none!important;';
|
2016-06-27 17:09:04 -06:00
|
|
|
|
2021-02-17 07:12:00 -07:00
|
|
|
vAPI.DOMFilterer = class {
|
|
|
|
constructor() {
|
|
|
|
this.commitTimer = new vAPI.SafeAnimationFrame(
|
|
|
|
( ) => { this.commitNow(); }
|
|
|
|
);
|
|
|
|
this.domIsReady = document.readyState !== 'loading';
|
|
|
|
this.disabled = false;
|
|
|
|
this.listeners = [];
|
|
|
|
this.stylesheets = [];
|
|
|
|
this.exceptedCSSRules = [];
|
|
|
|
this.exceptions = [];
|
|
|
|
this.proceduralFilterer = null;
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/167
|
|
|
|
// By the time the DOMContentLoaded is fired, the content script might
|
|
|
|
// have been disconnected from the background page. Unclear why this
|
|
|
|
// would happen, so far seems to be a Chromium-specific behavior at
|
|
|
|
// launch time.
|
|
|
|
if ( this.domIsReady !== true ) {
|
|
|
|
document.addEventListener('DOMContentLoaded', ( ) => {
|
|
|
|
if ( vAPI instanceof Object === false ) { return; }
|
|
|
|
this.domIsReady = true;
|
|
|
|
this.commit();
|
|
|
|
});
|
2019-05-16 15:22:20 -06:00
|
|
|
}
|
2021-02-17 07:12:00 -07:00
|
|
|
}
|
2014-06-23 16:42:43 -06:00
|
|
|
|
2021-02-17 07:12:00 -07:00
|
|
|
explodeCSS(css) {
|
|
|
|
const out = [];
|
|
|
|
const reBlock = /^\{(.*)\}$/m;
|
|
|
|
const blocks = css.trim().split(/\n\n+/);
|
|
|
|
for ( const block of blocks ) {
|
|
|
|
const match = reBlock.exec(block);
|
|
|
|
out.push([ block.slice(0, match.index).trim(), match[1] ]);
|
2017-10-21 11:43:46 -06:00
|
|
|
}
|
2021-02-17 07:12:00 -07:00
|
|
|
return out;
|
|
|
|
}
|
2016-08-12 06:55:35 -06:00
|
|
|
|
2021-02-17 07:12:00 -07:00
|
|
|
addCSS(css, details = {}) {
|
|
|
|
if ( typeof css !== 'string' || css.length === 0 ) { return; }
|
|
|
|
if ( this.stylesheets.includes(css) ) { return; }
|
|
|
|
this.stylesheets.push(css);
|
|
|
|
if ( details.mustInject && this.disabled === false ) {
|
|
|
|
vAPI.userStylesheet.add(css);
|
2020-07-22 08:21:16 -06:00
|
|
|
}
|
2021-02-17 07:12:00 -07:00
|
|
|
if ( this.hasListeners() === false ) { return; }
|
|
|
|
if ( details.silent ) { return; }
|
|
|
|
this.triggerListeners({ declarative: this.explodeCSS(css) });
|
|
|
|
}
|
2020-07-22 08:21:16 -06:00
|
|
|
|
2021-02-17 07:12:00 -07:00
|
|
|
exceptCSSRules(exceptions) {
|
|
|
|
if ( exceptions.length === 0 ) { return; }
|
|
|
|
this.exceptedCSSRules.push(...exceptions);
|
|
|
|
if ( this.hasListeners() ) {
|
|
|
|
this.triggerListeners({ exceptions });
|
2020-07-22 08:21:16 -06:00
|
|
|
}
|
2021-02-17 07:12:00 -07:00
|
|
|
}
|
2020-07-22 08:21:16 -06:00
|
|
|
|
2021-02-17 07:12:00 -07:00
|
|
|
addListener(listener) {
|
|
|
|
if ( this.listeners.indexOf(listener) !== -1 ) { return; }
|
|
|
|
this.listeners.push(listener);
|
|
|
|
}
|
2020-07-22 08:21:16 -06:00
|
|
|
|
2021-02-17 07:12:00 -07:00
|
|
|
removeListener(listener) {
|
|
|
|
const pos = this.listeners.indexOf(listener);
|
|
|
|
if ( pos === -1 ) { return; }
|
|
|
|
this.listeners.splice(pos, 1);
|
|
|
|
}
|
2020-07-22 08:21:16 -06:00
|
|
|
|
2021-02-17 07:12:00 -07:00
|
|
|
hasListeners() {
|
|
|
|
return this.listeners.length !== 0;
|
|
|
|
}
|
2020-07-22 08:21:16 -06:00
|
|
|
|
2021-02-17 07:12:00 -07:00
|
|
|
triggerListeners(changes) {
|
|
|
|
for ( const listener of this.listeners ) {
|
|
|
|
listener.onFiltersetChanged(changes);
|
2020-07-22 08:21:16 -06:00
|
|
|
}
|
2021-02-17 07:12:00 -07:00
|
|
|
}
|
2020-07-22 08:21:16 -06:00
|
|
|
|
2021-02-17 07:12:00 -07:00
|
|
|
toggle(state, callback) {
|
|
|
|
if ( state === undefined ) { state = this.disabled; }
|
|
|
|
if ( state !== this.disabled ) { return; }
|
|
|
|
this.disabled = !state;
|
|
|
|
const uss = vAPI.userStylesheet;
|
|
|
|
for ( const css of this.stylesheets ) {
|
|
|
|
if ( this.disabled ) {
|
|
|
|
uss.remove(css);
|
|
|
|
} else {
|
|
|
|
uss.add(css);
|
2020-07-22 08:21:16 -06:00
|
|
|
}
|
|
|
|
}
|
2021-02-17 07:12:00 -07:00
|
|
|
uss.apply(callback);
|
|
|
|
}
|
2020-07-22 08:21:16 -06:00
|
|
|
|
2021-02-17 07:12:00 -07:00
|
|
|
// Here we will deal with:
|
|
|
|
// - Injecting low priority user styles;
|
|
|
|
// - Notifying listeners about changed filterset.
|
|
|
|
// https://www.reddit.com/r/uBlockOrigin/comments/9jj0y1/no_longer_blocking_ads/
|
|
|
|
// Ensure vAPI is still valid -- it can go away by the time we are
|
|
|
|
// called, since the port could be force-disconnected from the main
|
|
|
|
// process. Another approach would be to have vAPI.SafeAnimationFrame
|
|
|
|
// register a shutdown job: to evaluate. For now I will keep the fix
|
|
|
|
// trivial.
|
|
|
|
commitNow() {
|
|
|
|
this.commitTimer.clear();
|
|
|
|
if ( vAPI instanceof Object === false ) { return; }
|
|
|
|
vAPI.userStylesheet.apply();
|
|
|
|
if ( this.proceduralFilterer instanceof Object ) {
|
|
|
|
this.proceduralFilterer.commitNow();
|
2020-07-22 08:21:16 -06:00
|
|
|
}
|
2021-02-17 07:12:00 -07:00
|
|
|
}
|
2020-07-22 08:21:16 -06:00
|
|
|
|
2021-02-17 07:12:00 -07:00
|
|
|
commit(commitNow) {
|
|
|
|
if ( commitNow ) {
|
2020-07-22 08:21:16 -06:00
|
|
|
this.commitTimer.clear();
|
2021-02-17 07:12:00 -07:00
|
|
|
this.commitNow();
|
|
|
|
} else {
|
|
|
|
this.commitTimer.start();
|
2019-05-16 11:44:49 -06:00
|
|
|
}
|
2021-02-17 07:12:00 -07:00
|
|
|
}
|
2016-11-12 11:38:41 -07:00
|
|
|
|
2021-02-17 07:12:00 -07:00
|
|
|
proceduralFiltererInstance() {
|
|
|
|
if ( this.proceduralFilterer instanceof Object === false ) {
|
|
|
|
if ( vAPI.DOMProceduralFilterer instanceof Object === false ) {
|
|
|
|
return null;
|
2020-07-22 08:21:16 -06:00
|
|
|
}
|
2021-02-17 07:12:00 -07:00
|
|
|
this.proceduralFilterer = new vAPI.DOMProceduralFilterer(this);
|
2020-07-22 08:21:16 -06:00
|
|
|
}
|
2021-02-17 07:12:00 -07:00
|
|
|
return this.proceduralFilterer;
|
|
|
|
}
|
2020-07-22 08:21:16 -06:00
|
|
|
|
2021-02-17 07:12:00 -07:00
|
|
|
addProceduralSelectors(selectors) {
|
|
|
|
if ( Array.isArray(selectors) === false || selectors.length === 0 ) {
|
|
|
|
return;
|
2020-07-24 17:08:48 -06:00
|
|
|
}
|
2021-02-17 07:12:00 -07:00
|
|
|
const procedurals = [];
|
|
|
|
for ( const raw of selectors ) {
|
|
|
|
procedurals.push(JSON.parse(raw));
|
2019-05-16 11:44:49 -06:00
|
|
|
}
|
2021-02-17 07:12:00 -07:00
|
|
|
if ( procedurals.length === 0 ) { return; }
|
|
|
|
const pfilterer = this.proceduralFiltererInstance();
|
|
|
|
if ( pfilterer !== null ) {
|
|
|
|
pfilterer.addProceduralSelectors(procedurals);
|
2019-05-16 11:44:49 -06:00
|
|
|
}
|
2021-02-17 07:12:00 -07:00
|
|
|
}
|
2016-08-12 06:55:35 -06:00
|
|
|
|
2021-02-17 07:12:00 -07:00
|
|
|
createProceduralFilter(o) {
|
|
|
|
const pfilterer = this.proceduralFiltererInstance();
|
|
|
|
if ( pfilterer === null ) { return; }
|
|
|
|
return pfilterer.createProceduralFilter(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
getAllSelectors(bits = 0) {
|
|
|
|
const out = {
|
|
|
|
declarative: [],
|
|
|
|
exceptions: this.exceptedCSSRules,
|
|
|
|
};
|
|
|
|
const hasProcedural = this.proceduralFilterer instanceof Object;
|
|
|
|
const includePrivateSelectors = (bits & 0b01) !== 0;
|
|
|
|
const masterToken = hasProcedural
|
|
|
|
? `[${this.proceduralFilterer.masterToken}]`
|
|
|
|
: undefined;
|
|
|
|
for ( const css of this.stylesheets ) {
|
|
|
|
const blocks = this.explodeCSS(css);
|
|
|
|
for ( const block of blocks ) {
|
2020-09-07 06:28:01 -06:00
|
|
|
if (
|
|
|
|
includePrivateSelectors === false &&
|
|
|
|
masterToken !== undefined &&
|
2021-02-17 07:12:00 -07:00
|
|
|
block[0].startsWith(masterToken)
|
2020-09-07 06:28:01 -06:00
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-02-17 07:12:00 -07:00
|
|
|
out.declarative.push([ block[0], block[1] ]);
|
2020-09-07 06:28:01 -06:00
|
|
|
}
|
2019-05-16 11:44:49 -06:00
|
|
|
}
|
2021-02-17 07:12:00 -07:00
|
|
|
const excludeProcedurals = (bits & 0b10) !== 0;
|
|
|
|
if ( excludeProcedurals !== true ) {
|
|
|
|
out.procedural = hasProcedural
|
|
|
|
? Array.from(this.proceduralFilterer.selectors.values())
|
|
|
|
: [];
|
2019-05-16 11:44:49 -06:00
|
|
|
}
|
2021-02-17 07:12:00 -07:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
getAllExceptionSelectors() {
|
|
|
|
return this.exceptions.join(',\n');
|
|
|
|
}
|
|
|
|
};
|
2017-10-21 11:43:46 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2020-07-22 08:21:16 -06:00
|
|
|
// vAPI.domCollapser
|
|
|
|
|
|
|
|
{
|
2018-12-26 08:45:19 -07:00
|
|
|
const messaging = vAPI.messaging;
|
|
|
|
const toCollapse = new Map();
|
|
|
|
const src1stProps = {
|
2020-10-05 07:13:07 -06:00
|
|
|
audio: 'currentSrc',
|
2018-12-26 08:45:19 -07:00
|
|
|
embed: 'src',
|
|
|
|
iframe: 'src',
|
2020-10-05 07:13:07 -06:00
|
|
|
img: 'currentSrc',
|
|
|
|
object: 'data',
|
|
|
|
video: 'currentSrc',
|
2015-03-29 10:13:28 -06:00
|
|
|
};
|
2018-12-26 08:45:19 -07:00
|
|
|
const src2ndProps = {
|
2020-10-05 07:13:07 -06:00
|
|
|
audio: 'src',
|
|
|
|
img: 'src',
|
|
|
|
video: 'src',
|
2015-06-04 09:17:02 -06:00
|
|
|
};
|
2018-12-26 08:45:19 -07:00
|
|
|
const tagToTypeMap = {
|
2020-10-05 07:13:07 -06:00
|
|
|
audio: 'media',
|
2017-08-03 08:18:05 -06:00
|
|
|
embed: 'object',
|
|
|
|
iframe: 'sub_frame',
|
|
|
|
img: 'image',
|
2020-10-05 07:13:07 -06:00
|
|
|
object: 'object',
|
|
|
|
video: 'media',
|
2017-08-03 08:18:05 -06:00
|
|
|
};
|
2018-12-26 08:45:19 -07:00
|
|
|
let resquestIdGenerator = 1,
|
|
|
|
processTimer,
|
|
|
|
cachedBlockedSet,
|
|
|
|
cachedBlockedSetHash,
|
|
|
|
cachedBlockedSetTimer,
|
2018-12-27 08:17:08 -07:00
|
|
|
toProcess = [],
|
|
|
|
toFilter = [],
|
2018-12-26 08:45:19 -07:00
|
|
|
netSelectorCacheCount = 0;
|
|
|
|
|
|
|
|
const cachedBlockedSetClear = function() {
|
2017-08-03 08:18:05 -06:00
|
|
|
cachedBlockedSet =
|
|
|
|
cachedBlockedSetHash =
|
|
|
|
cachedBlockedSetTimer = undefined;
|
2015-03-29 10:13:28 -06:00
|
|
|
};
|
|
|
|
|
2020-10-05 07:13:07 -06:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/399
|
|
|
|
// https://github.com/gorhill/uBlock/issues/2848
|
|
|
|
// Use a user stylesheet to collapse placeholders.
|
|
|
|
const getCollapseToken = ( ) => {
|
|
|
|
if ( collapseToken === undefined ) {
|
|
|
|
collapseToken = vAPI.randomToken();
|
|
|
|
vAPI.userStylesheet.add(
|
2020-10-05 12:20:43 -06:00
|
|
|
`[${collapseToken}]\n{display:none!important;}`,
|
2020-10-05 07:13:07 -06:00
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return collapseToken;
|
|
|
|
};
|
|
|
|
let collapseToken;
|
|
|
|
|
2017-08-03 08:18:05 -06:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/174
|
|
|
|
// Do not remove fragment from src URL
|
2018-12-26 08:45:19 -07:00
|
|
|
const onProcessed = function(response) {
|
2019-09-17 13:15:01 -06:00
|
|
|
// This happens if uBO is disabled or restarted.
|
|
|
|
if ( response instanceof Object === false ) {
|
2017-08-03 08:18:05 -06:00
|
|
|
toCollapse.clear();
|
2016-03-21 08:33:40 -06:00
|
|
|
return;
|
|
|
|
}
|
2017-08-03 08:18:05 -06:00
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
const targets = toCollapse.get(response.id);
|
2017-08-03 08:18:05 -06:00
|
|
|
if ( targets === undefined ) { return; }
|
2020-10-05 07:13:07 -06:00
|
|
|
|
2017-08-03 08:18:05 -06:00
|
|
|
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 10:13:28 -06:00
|
|
|
}
|
2017-08-16 12:10:41 -06:00
|
|
|
if ( cachedBlockedSet === undefined || cachedBlockedSet.size === 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-05 07:13:07 -06:00
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
const selectors = [];
|
|
|
|
let netSelectorCacheCountMax = response.netSelectorCacheCountMax;
|
2017-08-03 08:18:05 -06:00
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
for ( const target of targets ) {
|
|
|
|
const tag = target.localName;
|
|
|
|
let prop = src1stProps[tag];
|
2017-08-03 08:18:05 -06:00
|
|
|
if ( prop === undefined ) { continue; }
|
2018-12-26 08:45:19 -07:00
|
|
|
let src = target[prop];
|
2017-08-03 08:18:05 -06: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 10:13:28 -06:00
|
|
|
}
|
2017-08-03 08:18:05 -06:00
|
|
|
if ( cachedBlockedSet.has(tagToTypeMap[tag] + ' ' + src) === false ) {
|
2015-03-29 10:13:28 -06:00
|
|
|
continue;
|
|
|
|
}
|
2020-10-05 07:13:07 -06:00
|
|
|
target.setAttribute(getCollapseToken(), '');
|
2017-08-03 08:18:05 -06:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/1048
|
2020-10-05 07:13:07 -06:00
|
|
|
// Use attribute to construct CSS rule
|
|
|
|
if ( netSelectorCacheCount > netSelectorCacheCountMax ) { continue; }
|
|
|
|
const value = target.getAttribute(prop);
|
|
|
|
if ( value ) {
|
|
|
|
selectors.push(`${tag}[${prop}="${CSS.escape(value)}"]`);
|
|
|
|
netSelectorCacheCount += 1;
|
2015-03-29 10:13:28 -06:00
|
|
|
}
|
|
|
|
}
|
2017-10-22 06:59:29 -06:00
|
|
|
|
2020-10-05 07:13:07 -06:00
|
|
|
if ( selectors.length === 0 ) { return; }
|
|
|
|
messaging.send('contentscript', {
|
|
|
|
what: 'cosmeticFiltersInjected',
|
|
|
|
type: 'net',
|
|
|
|
hostname: window.location.hostname,
|
|
|
|
selectors,
|
|
|
|
});
|
2015-03-29 10:13:28 -06:00
|
|
|
};
|
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
const send = function() {
|
2017-08-03 08:18:05 -06:00
|
|
|
processTimer = undefined;
|
|
|
|
toCollapse.set(resquestIdGenerator, toProcess);
|
2019-09-17 13:15:01 -06:00
|
|
|
messaging.send('contentscript', {
|
2017-08-03 08:18:05 -06:00
|
|
|
what: 'getCollapsibleBlockedRequests',
|
|
|
|
id: resquestIdGenerator,
|
2017-10-01 05:56:28 -06:00
|
|
|
frameURL: window.location.href,
|
2017-08-03 08:18:05 -06:00
|
|
|
resources: toFilter,
|
2019-09-17 13:15:01 -06:00
|
|
|
hash: cachedBlockedSetHash,
|
|
|
|
}).then(response => {
|
|
|
|
onProcessed(response);
|
|
|
|
});
|
2018-12-27 08:17:08 -07:00
|
|
|
toProcess = [];
|
|
|
|
toFilter = [];
|
2017-08-03 08:18:05 -06:00
|
|
|
resquestIdGenerator += 1;
|
2015-03-29 10:13:28 -06:00
|
|
|
};
|
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
const process = function(delay) {
|
2017-08-03 08:18:05 -06:00
|
|
|
if ( toProcess.length === 0 ) { return; }
|
2015-03-29 10:13:28 -06:00
|
|
|
if ( delay === 0 ) {
|
2017-08-03 08:18:05 -06:00
|
|
|
if ( processTimer !== undefined ) {
|
|
|
|
clearTimeout(processTimer);
|
|
|
|
}
|
2015-03-29 10:13:28 -06:00
|
|
|
send();
|
2017-08-03 08:18:05 -06:00
|
|
|
} else if ( processTimer === undefined ) {
|
|
|
|
processTimer = vAPI.setTimeout(send, delay || 20);
|
2015-03-29 10:13:28 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
const add = function(target) {
|
2017-08-03 08:18:05 -06:00
|
|
|
toProcess[toProcess.length] = target;
|
2015-03-29 10:13:28 -06:00
|
|
|
};
|
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
const addMany = function(targets) {
|
|
|
|
for ( const target of targets ) {
|
|
|
|
add(target);
|
2016-02-03 17:15:28 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
const iframeSourceModified = function(mutations) {
|
|
|
|
for ( const mutation of mutations ) {
|
|
|
|
addIFrame(mutation.target, true);
|
2015-05-01 17:06:52 -06:00
|
|
|
}
|
|
|
|
process();
|
|
|
|
};
|
2018-12-26 08:45:19 -07:00
|
|
|
const iframeSourceObserver = new MutationObserver(iframeSourceModified);
|
|
|
|
const iframeSourceObserverOptions = {
|
2015-05-01 17:06:52 -06:00
|
|
|
attributes: true,
|
|
|
|
attributeFilter: [ 'src' ]
|
|
|
|
};
|
|
|
|
|
2018-05-20 04:49:12 -06:00
|
|
|
// https://github.com/gorhill/uBlock/issues/162
|
2020-07-19 07:06:19 -06:00
|
|
|
// Be prepared to deal with possible change of src attribute.
|
2018-12-26 08:45:19 -07:00
|
|
|
const addIFrame = function(iframe, dontObserve) {
|
2015-05-01 17:06:52 -06:00
|
|
|
if ( dontObserve !== true ) {
|
|
|
|
iframeSourceObserver.observe(iframe, iframeSourceObserverOptions);
|
|
|
|
}
|
2018-12-26 08:45:19 -07:00
|
|
|
const src = iframe.src;
|
2020-07-19 07:06:19 -06:00
|
|
|
if ( typeof src !== 'string' || src === '' ) { return; }
|
2018-05-20 04:49:12 -06:00
|
|
|
if ( src.startsWith('http') === false ) { return; }
|
2018-12-27 08:17:08 -07:00
|
|
|
toFilter.push({ type: 'sub_frame', url: iframe.src });
|
2017-08-03 08:18:05 -06:00
|
|
|
add(iframe);
|
2015-03-29 10:13:28 -06:00
|
|
|
};
|
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
const addIFrames = function(iframes) {
|
|
|
|
for ( const iframe of iframes ) {
|
|
|
|
addIFrame(iframe);
|
2016-02-03 16:47:30 -07:00
|
|
|
}
|
2016-02-03 17:15:28 -07:00
|
|
|
};
|
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
const onResourceFailed = function(ev) {
|
2017-08-03 08:18:05 -06:00
|
|
|
if ( tagToTypeMap[ev.target.localName] !== undefined ) {
|
2017-10-21 11:43:46 -06:00
|
|
|
add(ev.target);
|
|
|
|
process();
|
2017-08-03 08:18:05 -06:00
|
|
|
}
|
2016-08-13 14:42:58 -06:00
|
|
|
};
|
|
|
|
|
2020-07-24 17:08:48 -06:00
|
|
|
const stop = function() {
|
|
|
|
document.removeEventListener('error', onResourceFailed, true);
|
|
|
|
if ( processTimer !== undefined ) {
|
|
|
|
clearTimeout(processTimer);
|
|
|
|
}
|
|
|
|
if ( vAPI.domWatcher instanceof Object ) {
|
|
|
|
vAPI.domWatcher.removeListener(domWatcherInterface);
|
|
|
|
}
|
|
|
|
vAPI.shutdown.remove(stop);
|
|
|
|
vAPI.domCollapser = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
const start = function() {
|
|
|
|
if ( vAPI.domWatcher instanceof Object ) {
|
|
|
|
vAPI.domWatcher.addListener(domWatcherInterface);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
const domWatcherInterface = {
|
2017-10-21 11:43:46 -06:00
|
|
|
onDOMCreated: function() {
|
2019-09-30 05:50:35 -06:00
|
|
|
if ( self.vAPI instanceof Object === false ) { return; }
|
2017-10-21 11:43:46 -06:00
|
|
|
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 08:45:19 -07:00
|
|
|
const elems = document.images ||
|
|
|
|
document.getElementsByTagName('img');
|
|
|
|
for ( const elem of elems ) {
|
2017-10-21 11:43:46 -06:00
|
|
|
if ( elem.complete ) {
|
|
|
|
add(elem);
|
|
|
|
}
|
2016-08-12 06:55:35 -06:00
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
addMany(document.embeds || document.getElementsByTagName('embed'));
|
|
|
|
addMany(document.getElementsByTagName('object'));
|
|
|
|
addIFrames(document.getElementsByTagName('iframe'));
|
|
|
|
process(0);
|
|
|
|
|
|
|
|
document.addEventListener('error', onResourceFailed, true);
|
|
|
|
|
2020-07-24 17:08:48 -06:00
|
|
|
vAPI.shutdown.add(stop);
|
2017-10-21 11:43:46 -06:00
|
|
|
},
|
|
|
|
onDOMChanged: function(addedNodes) {
|
2018-12-26 08:45:19 -07:00
|
|
|
if ( addedNodes.length === 0 ) { return; }
|
|
|
|
for ( const node of addedNodes ) {
|
2017-10-21 11:43:46 -06:00
|
|
|
if ( node.localName === 'iframe' ) {
|
|
|
|
addIFrame(node);
|
|
|
|
}
|
2021-06-17 07:58:18 -06:00
|
|
|
if ( node.firstElementChild === null ) { continue; }
|
2018-12-26 08:45:19 -07:00
|
|
|
const iframes = node.getElementsByTagName('iframe');
|
2016-08-12 06:55:35 -06:00
|
|
|
if ( iframes.length !== 0 ) {
|
|
|
|
addIFrames(iframes);
|
|
|
|
}
|
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
process();
|
2016-06-28 17:45:11 -06:00
|
|
|
}
|
2016-08-12 06:55:35 -06:00
|
|
|
};
|
|
|
|
|
2020-07-24 17:08:48 -06:00
|
|
|
vAPI.domCollapser = { start };
|
2020-07-22 08:21:16 -06:00
|
|
|
}
|
2015-03-29 10:13:28 -06:00
|
|
|
|
|
|
|
/******************************************************************************/
|
2016-06-27 17:09:04 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2020-07-22 08:21:16 -06:00
|
|
|
// vAPI.domSurveyor
|
|
|
|
|
|
|
|
{
|
2018-12-26 08:45:19 -07:00
|
|
|
const messaging = vAPI.messaging;
|
|
|
|
const queriedIds = new Set();
|
|
|
|
const queriedClasses = new Set();
|
2018-12-31 09:50:40 -07:00
|
|
|
const maxSurveyNodes = 65536;
|
|
|
|
const maxSurveyTimeSlice = 4;
|
|
|
|
const maxSurveyBuffer = 64;
|
2018-12-26 08:45:19 -07:00
|
|
|
|
|
|
|
let domFilterer,
|
2017-10-25 09:42:18 -06:00
|
|
|
hostname = '',
|
2016-10-06 08:49:46 -06:00
|
|
|
surveyCost = 0;
|
2014-06-23 16:42:43 -06:00
|
|
|
|
2018-12-31 09:50:40 -07: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,
|
2021-08-24 08:03:25 -06:00
|
|
|
add(nodes) {
|
2018-12-31 09:50:40 -07:00
|
|
|
if ( nodes.length === 0 || this.accepted >= maxSurveyNodes ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.nodeLists.push(nodes);
|
|
|
|
this.accepted += nodes.length;
|
|
|
|
},
|
2021-08-24 08:03:25 -06:00
|
|
|
next() {
|
2018-12-31 09:50:40 -07:00
|
|
|
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;
|
|
|
|
},
|
2021-08-24 08:03:25 -06:00
|
|
|
hasNodes() {
|
2018-12-31 09:50:40 -07:00
|
|
|
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.
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2021-08-24 08:03:25 -06:00
|
|
|
const idFromNode = (node, out) => {
|
|
|
|
const raw = node.id;
|
|
|
|
if ( typeof raw !== 'string' || raw.length === 0 ) { return; }
|
|
|
|
const s = raw.trim();
|
|
|
|
if ( queriedIds.has(s) || s.length === 0 ) { return; }
|
|
|
|
out.push(s);
|
|
|
|
queriedIds.add(s);
|
|
|
|
};
|
|
|
|
|
2022-04-03 11:13:20 -06:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/discussions/2076
|
|
|
|
// Performance: avoid using Element.classList
|
2021-08-24 08:03:25 -06:00
|
|
|
const classesFromNode = (node, out) => {
|
2022-04-03 11:13:20 -06:00
|
|
|
const s = node.getAttribute('class');
|
|
|
|
if ( typeof s !== 'string' ) { return; }
|
|
|
|
const len = s.length;
|
|
|
|
for ( let beg = 0, end = 0, token = ''; beg < len; beg += 1 ) {
|
|
|
|
end = s.indexOf(' ', beg);
|
|
|
|
if ( end === beg ) { continue; }
|
|
|
|
if ( end === -1 ) { end = len; }
|
|
|
|
token = s.slice(beg, end);
|
|
|
|
beg = end;
|
|
|
|
if ( queriedClasses.has(token) ) { continue; }
|
|
|
|
out.push(token);
|
|
|
|
queriedClasses.add(token);
|
2021-08-24 08:03:25 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-31 09:50:40 -07:00
|
|
|
const surveyPhase1 = function() {
|
|
|
|
//console.time('dom surveyor/surveying');
|
|
|
|
const t0 = performance.now();
|
|
|
|
const ids = [];
|
|
|
|
const classes = [];
|
|
|
|
const nodes = pendingNodes.buffer;
|
|
|
|
const deadline = t0 + maxSurveyTimeSlice;
|
|
|
|
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;
|
2021-08-24 08:03:25 -06:00
|
|
|
idFromNode(node, ids);
|
|
|
|
classesFromNode(node, classes);
|
2018-12-31 09:50:40 -07:00
|
|
|
}
|
|
|
|
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 13:15:01 -06:00
|
|
|
messaging.send('contentscript', {
|
|
|
|
what: 'retrieveGenericCosmeticSelectors',
|
|
|
|
hostname,
|
2021-08-24 08:03:25 -06:00
|
|
|
ids, classes,
|
2019-09-17 13:15:01 -06:00
|
|
|
exceptions: domFilterer.exceptions,
|
|
|
|
cost: surveyCost,
|
|
|
|
}).then(response => {
|
|
|
|
surveyPhase3(response);
|
|
|
|
});
|
2018-12-31 09:50:40 -07:00
|
|
|
} else {
|
|
|
|
surveyPhase3(null);
|
|
|
|
}
|
|
|
|
//console.timeEnd('dom surveyor/surveying');
|
|
|
|
};
|
|
|
|
|
|
|
|
const surveyTimer = new vAPI.SafeAnimationFrame(surveyPhase1);
|
|
|
|
|
2017-10-21 11:43:46 -06:00
|
|
|
// This is to shutdown the surveyor if result of surveying keeps being
|
2017-10-24 14:38:51 -06: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 08:45:19 -07:00
|
|
|
let canShutdownAfter = Date.now() + 300000,
|
2017-10-24 14:38:51 -06:00
|
|
|
surveyingMissCount = 0;
|
2017-10-21 11:43:46 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
// Handle main process' response.
|
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
const surveyPhase3 = function(response) {
|
|
|
|
const result = response && response.result;
|
|
|
|
let mustCommit = false;
|
2015-04-07 17:10:03 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
if ( result ) {
|
2021-02-17 07:12:00 -07:00
|
|
|
const css = result.injectedCSS;
|
|
|
|
if ( typeof css === 'string' && css.length !== 0 ) {
|
|
|
|
domFilterer.addCSS(css);
|
2017-10-26 04:18:03 -06:00
|
|
|
mustCommit = true;
|
|
|
|
}
|
2021-02-17 07:12:00 -07:00
|
|
|
const selectors = result.excepted;
|
2019-05-16 11:44:49 -06:00
|
|
|
if ( Array.isArray(selectors) && selectors.length !== 0 ) {
|
|
|
|
domFilterer.exceptCSSRules(selectors);
|
|
|
|
}
|
2016-08-12 06:55:35 -06:00
|
|
|
}
|
2016-07-12 11:29:30 -06:00
|
|
|
|
2018-12-31 09:50:40 -07: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 14:38:51 -06:00
|
|
|
}
|
2014-07-04 14:47:34 -06:00
|
|
|
|
2017-12-13 06:02:55 -07:00
|
|
|
//console.info('dom surveyor shutting down: too many misses');
|
2014-09-16 17:16:18 -06:00
|
|
|
|
2017-10-24 14:38:51 -06:00
|
|
|
surveyTimer.clear();
|
2017-10-21 11:43:46 -06:00
|
|
|
vAPI.domWatcher.removeListener(domWatcherInterface);
|
|
|
|
vAPI.domSurveyor = null;
|
2016-08-12 06:55:35 -06:00
|
|
|
};
|
2014-07-04 14:47:34 -06:00
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
const domWatcherInterface = {
|
2017-10-21 11:43:46 -06:00
|
|
|
onDOMCreated: function() {
|
|
|
|
if (
|
2019-09-30 05:50:35 -06:00
|
|
|
self.vAPI instanceof Object === false ||
|
2017-10-24 14:38:51 -06:00
|
|
|
vAPI.domSurveyor instanceof Object === false ||
|
2017-10-21 11:43:46 -06:00
|
|
|
vAPI.domFilterer instanceof Object === false
|
|
|
|
) {
|
2019-09-30 05:50:35 -06:00
|
|
|
if ( self.vAPI instanceof Object ) {
|
2017-10-21 11:43:46 -06:00
|
|
|
if ( vAPI.domWatcher instanceof Object ) {
|
|
|
|
vAPI.domWatcher.removeListener(domWatcherInterface);
|
|
|
|
}
|
2017-10-24 14:38:51 -06:00
|
|
|
vAPI.domSurveyor = null;
|
2017-10-21 11:43:46 -06:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2017-10-23 10:21:37 -06:00
|
|
|
//console.time('dom surveyor/dom layout created');
|
2017-10-21 11:43:46 -06:00
|
|
|
domFilterer = vAPI.domFilterer;
|
2021-08-24 08:03:25 -06:00
|
|
|
pendingNodes.add(document.querySelectorAll(
|
|
|
|
'[id]:not(html):not(body),[class]:not(html):not(body)'
|
|
|
|
));
|
2017-10-21 11:43:46 -06:00
|
|
|
surveyTimer.start();
|
2021-08-24 08:03:25 -06:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/1692
|
|
|
|
// Look-up safe-only selectors to mitigate probability of
|
|
|
|
// html/body elements of erroneously being targeted.
|
|
|
|
const ids = [], classes = [];
|
2021-10-14 07:22:36 -06:00
|
|
|
if ( document.documentElement !== null ) {
|
2021-10-14 07:08:08 -06:00
|
|
|
idFromNode(document.documentElement, ids);
|
|
|
|
classesFromNode(document.documentElement, classes);
|
|
|
|
}
|
2021-10-14 07:22:36 -06:00
|
|
|
if ( document.body !== null ) {
|
2021-10-14 07:08:08 -06:00
|
|
|
idFromNode(document.body, ids);
|
|
|
|
classesFromNode(document.body, classes);
|
|
|
|
}
|
2021-08-24 08:03:25 -06:00
|
|
|
if ( ids.length !== 0 || classes.length !== 0 ) {
|
|
|
|
messaging.send('contentscript', {
|
|
|
|
what: 'retrieveGenericCosmeticSelectors',
|
|
|
|
hostname,
|
|
|
|
ids, classes,
|
|
|
|
exceptions: domFilterer.exceptions,
|
|
|
|
safeOnly: true,
|
|
|
|
}).then(response => {
|
|
|
|
surveyPhase3(response);
|
|
|
|
});
|
|
|
|
}
|
2017-10-23 10:21:37 -06:00
|
|
|
//console.timeEnd('dom surveyor/dom layout created');
|
2017-10-21 11:43:46 -06:00
|
|
|
},
|
|
|
|
onDOMChanged: function(addedNodes) {
|
|
|
|
if ( addedNodes.length === 0 ) { return; }
|
2017-10-23 10:21:37 -06:00
|
|
|
//console.time('dom surveyor/dom layout changed');
|
2021-08-24 08:03:25 -06:00
|
|
|
for ( const node of addedNodes ) {
|
2018-12-31 09:50:40 -07:00
|
|
|
pendingNodes.add([ node ]);
|
2021-06-17 07:58:18 -06:00
|
|
|
if ( node.firstElementChild === null ) { continue; }
|
2021-08-24 08:03:25 -06:00
|
|
|
pendingNodes.add(node.querySelectorAll(
|
|
|
|
'[id]:not(html):not(body),[class]:not(html):not(body)'
|
|
|
|
));
|
2017-10-21 11:43:46 -06:00
|
|
|
}
|
2018-12-31 09:50:40 -07:00
|
|
|
if ( pendingNodes.hasNodes() ) {
|
2017-10-21 11:43:46 -06:00
|
|
|
surveyTimer.start(1);
|
|
|
|
}
|
2017-10-23 10:21:37 -06:00
|
|
|
//console.timeEnd('dom surveyor/dom layout changed');
|
2016-06-28 07:06:14 -06:00
|
|
|
}
|
2014-09-16 13:39:21 -06:00
|
|
|
};
|
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
const start = function(details) {
|
2017-10-25 09:42:18 -06:00
|
|
|
if ( vAPI.domWatcher instanceof Object === false ) { return; }
|
|
|
|
hostname = details.hostname;
|
2017-10-21 11:43:46 -06:00
|
|
|
vAPI.domWatcher.addListener(domWatcherInterface);
|
2017-10-25 09:42:18 -06:00
|
|
|
};
|
2015-04-07 17:10:03 -06:00
|
|
|
|
2020-07-22 08:21:16 -06:00
|
|
|
vAPI.domSurveyor = { start };
|
|
|
|
}
|
2014-06-23 16:42:43 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
2014-09-14 14:20:40 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2020-07-22 08:21:16 -06:00
|
|
|
// vAPI.bootstrap:
|
|
|
|
// Bootstrapping allows all components of the content script
|
|
|
|
// to be launched if/when needed.
|
2017-10-21 11:43:46 -06:00
|
|
|
|
2020-07-22 08:21:16 -06:00
|
|
|
{
|
2018-12-26 08:45:19 -07:00
|
|
|
const bootstrapPhase2 = function() {
|
2017-10-21 11:43:46 -06:00
|
|
|
// This can happen on Firefox. For instance:
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1893
|
|
|
|
if ( window.location === null ) { return; }
|
2019-09-30 05:50:35 -06:00
|
|
|
if ( self.vAPI instanceof Object === false ) { return; }
|
2017-10-25 09:42:18 -06:00
|
|
|
|
2019-09-17 13:15:01 -06:00
|
|
|
vAPI.messaging.send('contentscript', {
|
|
|
|
what: 'shouldRenderNoscriptTags',
|
|
|
|
});
|
2018-08-31 16:47:02 -06:00
|
|
|
|
2017-10-25 09:42:18 -06:00
|
|
|
if ( vAPI.domWatcher instanceof Object ) {
|
2017-10-21 11:43:46 -06:00
|
|
|
vAPI.domWatcher.start();
|
2016-08-13 14:42:58 -06:00
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
|
2017-10-25 09:42:18 -06:00
|
|
|
// Element picker works only in top window for now.
|
|
|
|
if (
|
|
|
|
window !== window.top ||
|
|
|
|
vAPI.domFilterer instanceof Object === false
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
|
2019-09-18 10:17:45 -06:00
|
|
|
// To be used by element picker/zapper.
|
|
|
|
vAPI.mouseClick = { x: -1, y: -1 };
|
2017-10-21 11:43:46 -06:00
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
const onMouseClick = function(ev) {
|
2019-09-18 10:17:45 -06: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.
|
2019-11-08 07:30:59 -07:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/777
|
|
|
|
// Mind that href may not be a string.
|
|
|
|
const elem = ev.target.closest('a[href]');
|
|
|
|
if ( elem === null || typeof elem.href !== 'string' ) { return; }
|
2019-09-17 13:15:01 -06:00
|
|
|
vAPI.messaging.send('contentscript', {
|
2019-09-18 10:17:45 -06:00
|
|
|
what: 'maybeGoodPopup',
|
|
|
|
url: elem.href || '',
|
2019-09-17 13:15:01 -06:00
|
|
|
});
|
2017-10-21 11:43:46 -06:00
|
|
|
};
|
2016-08-13 14:42:58 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
document.addEventListener('mousedown', onMouseClick, true);
|
2015-01-01 19:14:53 -07:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
// https://github.com/gorhill/uMatrix/issues/144
|
|
|
|
vAPI.shutdown.add(function() {
|
|
|
|
document.removeEventListener('mousedown', onMouseClick, true);
|
|
|
|
});
|
2017-10-21 11:43:46 -06:00
|
|
|
};
|
|
|
|
|
2019-01-27 15:07:40 -07: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 08:45:19 -07:00
|
|
|
const bootstrapPhase1 = function(response) {
|
2019-09-29 10:26:58 -06:00
|
|
|
if ( response instanceof Object === false ) { return; }
|
|
|
|
|
2019-01-27 15:07:40 -07:00
|
|
|
vAPI.bootstrap = undefined;
|
|
|
|
|
2017-10-21 11:43:46 -06:00
|
|
|
// cosmetic filtering engine aka 'cfe'
|
2018-12-26 08:45:19 -07:00
|
|
|
const cfeDetails = response && response.specificCosmeticFilters;
|
2017-10-21 11:43:46 -06:00
|
|
|
if ( !cfeDetails || !cfeDetails.ready ) {
|
|
|
|
vAPI.domWatcher = vAPI.domCollapser = vAPI.domFilterer =
|
|
|
|
vAPI.domSurveyor = vAPI.domIsLoaded = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-24 17:08:48 -06:00
|
|
|
vAPI.domCollapser.start();
|
|
|
|
|
2021-07-12 09:55:58 -06:00
|
|
|
const {
|
|
|
|
noSpecificCosmeticFiltering,
|
|
|
|
noGenericCosmeticFiltering,
|
|
|
|
scriptlets,
|
|
|
|
} = response;
|
|
|
|
|
|
|
|
vAPI.noSpecificCosmeticFiltering = noSpecificCosmeticFiltering;
|
|
|
|
vAPI.noGenericCosmeticFiltering = noGenericCosmeticFiltering;
|
|
|
|
|
|
|
|
if ( noSpecificCosmeticFiltering && noGenericCosmeticFiltering ) {
|
2017-10-21 11:43:46 -06:00
|
|
|
vAPI.domFilterer = null;
|
|
|
|
vAPI.domSurveyor = null;
|
|
|
|
} else {
|
2020-07-22 08:21:16 -06:00
|
|
|
const domFilterer = vAPI.domFilterer = new vAPI.DOMFilterer();
|
2021-07-12 09:55:58 -06:00
|
|
|
if ( noGenericCosmeticFiltering || cfeDetails.noDOMSurveying ) {
|
2017-10-21 11:43:46 -06:00
|
|
|
vAPI.domSurveyor = null;
|
|
|
|
}
|
|
|
|
domFilterer.exceptions = cfeDetails.exceptionFilters;
|
2021-02-17 07:12:00 -07:00
|
|
|
domFilterer.addCSS(cfeDetails.injectedCSS);
|
2017-10-21 11:43:46 -06:00
|
|
|
domFilterer.addProceduralSelectors(cfeDetails.proceduralFilters);
|
2019-05-16 11:44:49 -06:00
|
|
|
domFilterer.exceptCSSRules(cfeDetails.exceptedFilters);
|
2017-10-21 11:43:46 -06:00
|
|
|
}
|
|
|
|
|
2017-10-22 06:59:29 -06:00
|
|
|
vAPI.userStylesheet.apply();
|
|
|
|
|
2017-10-23 07:01:00 -06:00
|
|
|
// Library of resources is located at:
|
|
|
|
// https://github.com/gorhill/uBlock/blob/master/assets/ublock/resources.txt
|
2021-07-12 09:55:58 -06:00
|
|
|
if ( scriptlets ) {
|
|
|
|
vAPI.injectScriptlet(document, scriptlets);
|
|
|
|
vAPI.injectedScripts = scriptlets;
|
2017-10-21 11:43:46 -06:00
|
|
|
}
|
|
|
|
|
2017-10-25 09:42:18 -06:00
|
|
|
if ( vAPI.domSurveyor instanceof Object ) {
|
|
|
|
vAPI.domSurveyor.start(cfeDetails);
|
|
|
|
}
|
|
|
|
|
2017-10-21 11:43:46 -06: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 08:45:19 -07:00
|
|
|
document.addEventListener(
|
|
|
|
'DOMContentLoaded',
|
|
|
|
bootstrapPhase2,
|
|
|
|
{ once: true }
|
|
|
|
);
|
2017-10-21 11:43:46 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-22 08:21:16 -06:00
|
|
|
vAPI.bootstrap = function() {
|
2019-09-17 13:15:01 -06:00
|
|
|
vAPI.messaging.send('contentscript', {
|
|
|
|
what: 'retrieveContentScriptParameters',
|
2020-07-29 05:38:49 -06:00
|
|
|
url: vAPI.effectiveSelf.location.href,
|
2019-09-17 13:15:01 -06:00
|
|
|
}).then(response => {
|
|
|
|
bootstrapPhase1(response);
|
|
|
|
});
|
2019-01-27 15:07:40 -07:00
|
|
|
};
|
2020-07-22 08:21:16 -06:00
|
|
|
}
|
2015-01-01 19:14:53 -07:00
|
|
|
|
2019-01-27 15:07:40 -07:00
|
|
|
// This starts bootstrap process.
|
|
|
|
vAPI.bootstrap();
|
|
|
|
|
2015-01-01 19:14:53 -07:00
|
|
|
/******************************************************************************/
|
2016-06-27 17:09:04 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
2017-08-17 06:25:02 -06:00
|
|
|
|
2018-12-26 08:45:19 -07:00
|
|
|
}
|
|
|
|
// <<<<<<<< end of HUGE-IF-BLOCK
|