2014-06-23 16:42:43 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2016-03-06 08:51:06 -07:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
2017-02-12 13:53:40 -07:00
|
|
|
Copyright (C) 2014-2017 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
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
+--> [[domSurveyor] --> domFilterer]
|
|
|
|
domWatcher--|
|
|
|
|
+--> [domCollapser]
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
If page is whitelisted:
|
|
|
|
- domWatcher: off
|
|
|
|
- domCollapser: off
|
|
|
|
- domFilterer: off
|
|
|
|
- domSurveyor: off
|
|
|
|
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
|
|
|
|
|
|
|
|
If generic cosmetic filtering is disabled:
|
|
|
|
- domWatcher: on
|
|
|
|
- domCollapser: on
|
|
|
|
- domFilterer: on
|
|
|
|
- domSurveyor: off
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-12-16 14:25:36 -07:00
|
|
|
The domFilterer makes use of platform-dependent user styles[1] code, or
|
|
|
|
provide a default generic implementation if none is present.
|
|
|
|
At time of writing, only modern Firefox provides a custom implementation,
|
|
|
|
which makes for solid, reliable and low overhead cosmetic filtering on
|
|
|
|
Firefox.
|
|
|
|
The generic implementation[2] performs as best as can be, but won't ever be
|
|
|
|
as reliable as real user styles.
|
|
|
|
[1] "user styles" refer to local CSS rules which have priority over, and
|
|
|
|
can't be overriden by a web page's own CSS rules.
|
|
|
|
[2] below, see platformUserCSS / platformHideNode / platformUnhideNode
|
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
*/
|
2014-06-23 16:42:43 -06:00
|
|
|
|
2016-06-27 17:09:04 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Abort execution by throwing if an unexpected condition arise.
|
|
|
|
// - https://github.com/chrisaljoudi/uBlock/issues/456
|
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
if ( typeof vAPI !== 'object' ) {
|
2016-07-09 17:21:46 -06:00
|
|
|
throw new Error('uBlock Origin: aborting content scripts for ' + window.location);
|
2016-06-27 17:09:04 -06:00
|
|
|
}
|
2016-08-12 06:55:35 -06:00
|
|
|
vAPI.lock();
|
2016-06-27 17:09:04 -06:00
|
|
|
|
2016-06-28 20:01:15 -06:00
|
|
|
vAPI.matchesProp = (function() {
|
|
|
|
var docElem = document.documentElement;
|
|
|
|
if ( typeof docElem.matches !== 'function' ) {
|
|
|
|
if ( typeof docElem.mozMatchesSelector === 'function' ) {
|
|
|
|
return 'mozMatchesSelector';
|
|
|
|
} else if ( typeof docElem.webkitMatchesSelector === 'function' ) {
|
|
|
|
return 'webkitMatchesSelector';
|
2017-03-16 17:31:14 -06:00
|
|
|
} else if ( typeof docElem.msMatchesSelector === 'function' ) {
|
|
|
|
return 'msMatchesSelector';
|
2016-06-28 20:01:15 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 'matches';
|
|
|
|
})();
|
|
|
|
|
2016-06-27 17:09:04 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-11-12 11:38:41 -07:00
|
|
|
// https://github.com/gorhill/uBlock/issues/2147
|
|
|
|
|
|
|
|
vAPI.SafeAnimationFrame = function(callback) {
|
|
|
|
this.fid = this.tid = null;
|
|
|
|
this.callback = callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
vAPI.SafeAnimationFrame.prototype.start = function() {
|
|
|
|
if ( this.fid !== null ) { return; }
|
|
|
|
this.fid = requestAnimationFrame(this.callback);
|
|
|
|
this.tid = vAPI.setTimeout(this.callback, 1200000);
|
|
|
|
};
|
|
|
|
|
|
|
|
vAPI.SafeAnimationFrame.prototype.clear = function() {
|
|
|
|
if ( this.fid === null ) { return; }
|
|
|
|
cancelAnimationFrame(this.fid);
|
|
|
|
clearTimeout(this.tid);
|
|
|
|
this.fid = this.tid = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-06-28 07:06:14 -06:00
|
|
|
// The DOM filterer is the heart of uBO's cosmetic filtering.
|
|
|
|
|
2016-07-09 17:21:46 -06:00
|
|
|
vAPI.domFilterer = (function() {
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-05-15 06:51:25 -06:00
|
|
|
var allExceptions = new Set(),
|
|
|
|
allSelectors = new Set(),
|
2016-12-25 14:56:39 -07:00
|
|
|
stagedNodes = [];
|
2016-07-09 17:21:46 -06:00
|
|
|
|
|
|
|
// Complex selectors, due to their nature may need to be "de-committed". A
|
2016-07-09 19:40:07 -06:00
|
|
|
// Set() is used to implement this functionality.
|
2016-07-09 17:21:46 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
var complexSelectorsOldResultSet,
|
2017-05-15 06:51:25 -06:00
|
|
|
complexSelectorsCurrentResultSet = new Set();
|
2016-07-09 17:21:46 -06:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var cosmeticFiltersActivatedTimer = null;
|
|
|
|
|
|
|
|
var cosmeticFiltersActivated = function() {
|
|
|
|
cosmeticFiltersActivatedTimer = null;
|
|
|
|
vAPI.messaging.send(
|
|
|
|
'contentscript',
|
|
|
|
{ what: 'cosmeticFiltersActivated' }
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-12-16 14:25:36 -07:00
|
|
|
// If a platform does not support its own vAPI.userCSS (user styles), we
|
|
|
|
// provide a default (imperfect) implementation.
|
|
|
|
|
|
|
|
// Probably no longer need to watch for style tags removal/tampering with fix
|
|
|
|
// to https://github.com/gorhill/uBlock/issues/963
|
|
|
|
|
2017-07-23 07:56:43 -06:00
|
|
|
// https://github.com/gorhill/uBlock/issues/2810
|
|
|
|
// With Firefox Nightly, it may happens style tags are injected before the
|
|
|
|
// head element is present.
|
|
|
|
|
2016-12-16 14:25:36 -07:00
|
|
|
var platformUserCSS = (function() {
|
|
|
|
if ( vAPI.userCSS instanceof Object ) {
|
|
|
|
return vAPI.userCSS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
enabled: true,
|
|
|
|
styles: [],
|
|
|
|
add: function(css) {
|
|
|
|
var style = document.createElement('style');
|
|
|
|
style.setAttribute('type', 'text/css');
|
|
|
|
style.textContent = css;
|
2017-07-23 07:56:43 -06:00
|
|
|
var parent = document.head || document.documentElement;
|
|
|
|
if ( parent !== null ) {
|
|
|
|
parent.appendChild(style);
|
2016-12-16 14:25:36 -07:00
|
|
|
}
|
|
|
|
this.styles.push(style);
|
|
|
|
if ( style.sheet ) {
|
|
|
|
style.sheet.disabled = !this.enabled;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
remove: function(css) {
|
|
|
|
var i = this.styles.length,
|
|
|
|
style, parent;
|
|
|
|
while ( i-- ) {
|
|
|
|
style = this.styles[i];
|
|
|
|
if ( style.textContent !== css ) { continue; }
|
|
|
|
parent = style.parentNode;
|
|
|
|
if ( parent !== null ) {
|
|
|
|
parent.removeChild(style);
|
|
|
|
}
|
|
|
|
this.styles.splice(i, 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
toggle: function(state) {
|
|
|
|
if ( this.styles.length === '' ) { return; }
|
|
|
|
if ( state === undefined ) {
|
|
|
|
state = !this.enabled;
|
|
|
|
}
|
|
|
|
var i = this.styles.length, style;
|
|
|
|
while ( i-- ) {
|
|
|
|
style = this.styles[i];
|
|
|
|
if ( style.sheet !== null ) {
|
|
|
|
style.sheet.disabled = !state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.enabled = state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
2016-12-15 08:47:32 -07:00
|
|
|
// If a platform does not provide its own (improved) vAPI.hideNode, we assign
|
|
|
|
// a default one to try to override author styles as best as can be.
|
|
|
|
|
|
|
|
var platformHideNode = vAPI.hideNode,
|
|
|
|
platformUnhideNode = vAPI.unhideNode;
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
if ( platformHideNode instanceof Function ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var uid,
|
2017-03-12 08:22:46 -06:00
|
|
|
timer,
|
2016-12-15 08:47:32 -07:00
|
|
|
observer,
|
2017-05-14 10:01:56 -06:00
|
|
|
changedNodes = new Set(),
|
2017-03-12 08:22:46 -06:00
|
|
|
observerOptions = {
|
2017-03-13 11:03:51 -06:00
|
|
|
attributes: true,
|
|
|
|
attributeFilter: [ 'style' ]
|
|
|
|
};
|
2016-12-15 08:47:32 -07:00
|
|
|
|
2017-03-12 08:22:46 -06:00
|
|
|
// https://jsperf.com/clientheight-and-clientwidth-vs-getcomputedstyle
|
|
|
|
// Avoid getComputedStyle(), detecting whether a node is visible can be
|
|
|
|
// achieved with clientWidth/clientHeight.
|
|
|
|
// https://gist.github.com/paulirish/5d52fb081b3570c81e3a
|
|
|
|
// Do not interleave read-from/write-to the DOM. Write-to DOM
|
|
|
|
// operations would cause the first read-from to be expensive, and
|
|
|
|
// interleaving means that potentially all single read-from operation
|
|
|
|
// would be expensive rather than just the 1st one.
|
|
|
|
// Benchmarking toggling off/on cosmetic filtering confirms quite an
|
|
|
|
// improvement when:
|
|
|
|
// - batching as much as possible handling of all nodes;
|
|
|
|
// - avoiding to interleave read-from/write-to operations.
|
|
|
|
// However, toggling off/on cosmetic filtering repeatedly is not
|
|
|
|
// a real use case, but this shows this will help performance
|
|
|
|
// on sites which try to use inline styles to bypass blockers.
|
|
|
|
var batchProcess = function() {
|
|
|
|
timer.clear();
|
2017-05-14 10:01:56 -06:00
|
|
|
var uid_ = uid;
|
|
|
|
for ( var node of changedNodes ) {
|
|
|
|
if (
|
|
|
|
node[uid_] === undefined ||
|
|
|
|
node.clientHeight === 0 || node.clientWidth === 0
|
|
|
|
) {
|
|
|
|
continue;
|
2016-12-15 08:47:32 -07:00
|
|
|
}
|
2017-03-12 08:22:46 -06:00
|
|
|
var attr = node.getAttribute('style');
|
2017-05-14 10:01:56 -06:00
|
|
|
if ( attr === null ) {
|
2017-03-12 08:22:46 -06:00
|
|
|
attr = '';
|
2017-05-14 10:01:56 -06:00
|
|
|
} else if (
|
|
|
|
attr.length !== 0 &&
|
|
|
|
attr.charCodeAt(attr.length - 1) !== 0x3B /* ';' */
|
|
|
|
) {
|
2017-03-12 08:22:46 -06:00
|
|
|
attr += '; ';
|
|
|
|
}
|
|
|
|
node.setAttribute('style', attr + 'display: none !important;');
|
|
|
|
}
|
2017-05-14 10:01:56 -06:00
|
|
|
changedNodes.clear();
|
2016-12-15 08:47:32 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
var observerHandler = function(mutations) {
|
2017-03-12 08:22:46 -06:00
|
|
|
var i = mutations.length,
|
2017-05-14 10:01:56 -06:00
|
|
|
changedNodes_ = changedNodes;
|
2016-12-15 08:47:32 -07:00
|
|
|
while ( i-- ) {
|
2017-05-14 10:01:56 -06:00
|
|
|
changedNodes_.add(mutations[i].target);
|
2016-12-15 08:47:32 -07:00
|
|
|
}
|
2017-03-12 08:22:46 -06:00
|
|
|
timer.start();
|
2016-12-15 08:47:32 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
platformHideNode = function(node) {
|
|
|
|
if ( uid === undefined ) {
|
|
|
|
uid = vAPI.randomToken();
|
2017-03-12 08:22:46 -06:00
|
|
|
timer = new vAPI.SafeAnimationFrame(batchProcess);
|
|
|
|
}
|
|
|
|
if ( node[uid] === undefined ) {
|
|
|
|
node[uid] = node.hasAttribute('style') && (node.getAttribute('style') || '');
|
2016-12-15 08:47:32 -07:00
|
|
|
}
|
2017-03-12 08:22:46 -06:00
|
|
|
// Performance: batch-process nodes to hide.
|
2017-05-14 10:01:56 -06:00
|
|
|
changedNodes.add(node);
|
2017-03-12 08:22:46 -06:00
|
|
|
timer.start();
|
2016-12-15 08:47:32 -07:00
|
|
|
if ( observer === undefined ) {
|
|
|
|
observer = new MutationObserver(observerHandler);
|
|
|
|
}
|
|
|
|
observer.observe(node, observerOptions);
|
|
|
|
};
|
|
|
|
|
|
|
|
platformUnhideNode = function(node) {
|
|
|
|
if ( uid === undefined ) { return; }
|
|
|
|
var attr = node[uid];
|
|
|
|
if ( attr === false ) {
|
|
|
|
node.removeAttribute('style');
|
|
|
|
} else if ( typeof attr === 'string' ) {
|
|
|
|
node.setAttribute('style', attr);
|
|
|
|
}
|
|
|
|
delete node[uid];
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-12-25 14:56:39 -07:00
|
|
|
// 'P' stands for 'Procedural'
|
|
|
|
|
|
|
|
var PSelectorHasTask = function(task) {
|
|
|
|
this.selector = task[1];
|
|
|
|
};
|
|
|
|
PSelectorHasTask.prototype.exec = function(input) {
|
|
|
|
var output = [];
|
|
|
|
for ( var i = 0, n = input.length; i < n; i++ ) {
|
|
|
|
if ( input[i].querySelector(this.selector) !== null ) {
|
|
|
|
output.push(input[i]);
|
|
|
|
}
|
2016-08-06 10:09:18 -06:00
|
|
|
}
|
2016-12-25 14:56:39 -07:00
|
|
|
return output;
|
2016-08-06 10:09:18 -06:00
|
|
|
};
|
|
|
|
|
2016-12-25 14:56:39 -07:00
|
|
|
var PSelectorHasTextTask = function(task) {
|
|
|
|
this.needle = new RegExp(task[1]);
|
|
|
|
};
|
|
|
|
PSelectorHasTextTask.prototype.exec = function(input) {
|
|
|
|
var output = [];
|
|
|
|
for ( var i = 0, n = input.length; i < n; i++ ) {
|
|
|
|
if ( this.needle.test(input[i].textContent) ) {
|
|
|
|
output.push(input[i]);
|
|
|
|
}
|
2016-08-06 10:09:18 -06:00
|
|
|
}
|
2016-12-25 14:56:39 -07:00
|
|
|
return output;
|
2016-08-06 10:09:18 -06:00
|
|
|
};
|
|
|
|
|
2016-12-25 14:56:39 -07:00
|
|
|
var PSelectorIfTask = function(task) {
|
|
|
|
this.pselector = new PSelector(task[1]);
|
|
|
|
};
|
|
|
|
PSelectorIfTask.prototype.target = true;
|
|
|
|
PSelectorIfTask.prototype.exec = function(input) {
|
|
|
|
var output = [];
|
|
|
|
for ( var i = 0, n = input.length; i < n; i++ ) {
|
|
|
|
if ( this.pselector.test(input[i]) === this.target ) {
|
|
|
|
output.push(input[i]);
|
2016-08-06 10:09:18 -06:00
|
|
|
}
|
|
|
|
}
|
2016-12-25 14:56:39 -07:00
|
|
|
return output;
|
2016-08-06 10:09:18 -06:00
|
|
|
};
|
|
|
|
|
2016-12-25 14:56:39 -07:00
|
|
|
var PSelectorIfNotTask = function(task) {
|
|
|
|
PSelectorIfTask.call(this, task);
|
|
|
|
this.target = false;
|
2016-08-06 10:09:18 -06:00
|
|
|
};
|
2016-12-25 14:56:39 -07:00
|
|
|
PSelectorIfNotTask.prototype = Object.create(PSelectorIfTask.prototype);
|
|
|
|
PSelectorIfNotTask.prototype.constructor = PSelectorIfNotTask;
|
2016-08-06 10:09:18 -06:00
|
|
|
|
2016-12-25 14:56:39 -07:00
|
|
|
var PSelectorMatchesCSSTask = function(task) {
|
|
|
|
this.name = task[1].name;
|
|
|
|
this.value = new RegExp(task[1].value);
|
|
|
|
};
|
|
|
|
PSelectorMatchesCSSTask.prototype.pseudo = null;
|
|
|
|
PSelectorMatchesCSSTask.prototype.exec = function(input) {
|
|
|
|
var output = [], style;
|
|
|
|
for ( var i = 0, n = input.length; i < n; i++ ) {
|
|
|
|
style = window.getComputedStyle(input[i], this.pseudo);
|
|
|
|
if ( style === null ) { return null; } /* FF */
|
|
|
|
if ( this.value.test(style[this.name]) ) {
|
|
|
|
output.push(input[i]);
|
2016-08-06 10:09:18 -06:00
|
|
|
}
|
2016-12-25 14:56:39 -07:00
|
|
|
}
|
|
|
|
return output;
|
|
|
|
};
|
|
|
|
|
|
|
|
var PSelectorMatchesCSSAfterTask = function(task) {
|
|
|
|
PSelectorMatchesCSSTask.call(this, task);
|
|
|
|
this.pseudo = ':after';
|
|
|
|
};
|
|
|
|
PSelectorMatchesCSSAfterTask.prototype = Object.create(PSelectorMatchesCSSTask.prototype);
|
|
|
|
PSelectorMatchesCSSAfterTask.prototype.constructor = PSelectorMatchesCSSAfterTask;
|
|
|
|
|
|
|
|
var PSelectorMatchesCSSBeforeTask = function(task) {
|
|
|
|
PSelectorMatchesCSSTask.call(this, task);
|
|
|
|
this.pseudo = ':before';
|
|
|
|
};
|
|
|
|
PSelectorMatchesCSSBeforeTask.prototype = Object.create(PSelectorMatchesCSSTask.prototype);
|
|
|
|
PSelectorMatchesCSSBeforeTask.prototype.constructor = PSelectorMatchesCSSBeforeTask;
|
|
|
|
|
|
|
|
var PSelectorXpathTask = function(task) {
|
|
|
|
this.xpe = document.createExpression(task[1], null);
|
|
|
|
this.xpr = null;
|
|
|
|
};
|
|
|
|
PSelectorXpathTask.prototype.exec = function(input) {
|
|
|
|
var output = [], j, node;
|
|
|
|
for ( var i = 0, n = input.length; i < n; i++ ) {
|
|
|
|
this.xpr = this.xpe.evaluate(
|
|
|
|
input[i],
|
|
|
|
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
|
|
|
|
this.xpr
|
|
|
|
);
|
|
|
|
j = this.xpr.snapshotLength;
|
|
|
|
while ( j-- ) {
|
|
|
|
node = this.xpr.snapshotItem(j);
|
|
|
|
if ( node.nodeType === 1 ) {
|
|
|
|
output.push(node);
|
|
|
|
}
|
2016-08-06 10:09:18 -06:00
|
|
|
}
|
|
|
|
}
|
2016-12-25 14:56:39 -07:00
|
|
|
return output;
|
2016-08-06 10:09:18 -06:00
|
|
|
};
|
|
|
|
|
2016-12-25 14:56:39 -07:00
|
|
|
var PSelector = function(o) {
|
|
|
|
if ( PSelector.prototype.operatorToTaskMap === undefined ) {
|
|
|
|
PSelector.prototype.operatorToTaskMap = new Map([
|
|
|
|
[ ':has', PSelectorHasTask ],
|
|
|
|
[ ':has-text', PSelectorHasTextTask ],
|
|
|
|
[ ':if', PSelectorIfTask ],
|
|
|
|
[ ':if-not', PSelectorIfNotTask ],
|
|
|
|
[ ':matches-css', PSelectorMatchesCSSTask ],
|
|
|
|
[ ':matches-css-after', PSelectorMatchesCSSAfterTask ],
|
|
|
|
[ ':matches-css-before', PSelectorMatchesCSSBeforeTask ],
|
|
|
|
[ ':xpath', PSelectorXpathTask ]
|
|
|
|
]);
|
2016-08-06 10:09:18 -06:00
|
|
|
}
|
2016-12-25 14:56:39 -07:00
|
|
|
this.raw = o.raw;
|
|
|
|
this.selector = o.selector;
|
|
|
|
this.tasks = [];
|
2016-12-30 08:32:17 -07:00
|
|
|
var tasks = o.tasks;
|
|
|
|
if ( !tasks ) { return; }
|
|
|
|
for ( var i = 0, task, ctor; i < tasks.length; i++ ) {
|
2016-12-25 14:56:39 -07:00
|
|
|
task = tasks[i];
|
|
|
|
ctor = this.operatorToTaskMap.get(task[0]);
|
|
|
|
this.tasks.push(new ctor(task));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
PSelector.prototype.operatorToTaskMap = undefined;
|
|
|
|
PSelector.prototype.prime = function(input) {
|
|
|
|
var root = input || document;
|
|
|
|
if ( this.selector !== '' ) {
|
|
|
|
return root.querySelectorAll(this.selector);
|
|
|
|
}
|
|
|
|
return [ root ];
|
|
|
|
};
|
|
|
|
PSelector.prototype.exec = function(input) {
|
|
|
|
//var t0 = window.performance.now();
|
|
|
|
var tasks = this.tasks, nodes = this.prime(input);
|
|
|
|
for ( var i = 0, n = tasks.length; i < n && nodes.length !== 0; i++ ) {
|
|
|
|
nodes = tasks[i].exec(nodes);
|
|
|
|
}
|
|
|
|
//console.log('%s: %s ms', this.raw, (window.performance.now() - t0).toFixed(2));
|
|
|
|
return nodes;
|
|
|
|
};
|
|
|
|
PSelector.prototype.test = function(input) {
|
|
|
|
//var t0 = window.performance.now();
|
2016-12-26 10:37:43 -07:00
|
|
|
var tasks = this.tasks, nodes = this.prime(input), AA = [ null ], aa;
|
2016-12-25 14:56:39 -07:00
|
|
|
for ( var i = 0, ni = nodes.length; i < ni; i++ ) {
|
2016-12-26 10:37:43 -07:00
|
|
|
AA[0] = nodes[i]; aa = AA;
|
2016-12-25 14:56:39 -07:00
|
|
|
for ( var j = 0, nj = tasks.length; j < nj && aa.length !== 0; j++ ) {
|
2016-12-26 10:37:43 -07:00
|
|
|
aa = tasks[j].exec(aa);
|
2016-12-25 14:56:39 -07:00
|
|
|
}
|
|
|
|
if ( aa.length !== 0 ) { return true; }
|
|
|
|
}
|
|
|
|
//console.log('%s: %s ms', this.raw, (window.performance.now() - t0).toFixed(2));
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2016-08-06 10:09:18 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-07-09 17:21:46 -06:00
|
|
|
var domFilterer = {
|
2016-08-13 14:42:58 -06:00
|
|
|
addedNodesHandlerMissCount: 0,
|
2016-11-12 11:38:41 -07:00
|
|
|
commitTimer: null,
|
2016-07-09 17:21:46 -06:00
|
|
|
disabledId: vAPI.randomToken(),
|
2016-06-27 17:09:04 -06:00
|
|
|
enabled: true,
|
2016-10-08 13:14:24 -06:00
|
|
|
excludeId: undefined,
|
2016-07-09 17:21:46 -06:00
|
|
|
hiddenId: vAPI.randomToken(),
|
2016-06-28 07:06:14 -06:00
|
|
|
hiddenNodeCount: 0,
|
2016-09-24 12:42:31 -06:00
|
|
|
hiddenNodeEnforcer: false,
|
2016-08-12 06:55:35 -06:00
|
|
|
loggerEnabled: undefined,
|
2016-06-27 17:09:04 -06:00
|
|
|
|
2017-03-13 11:03:51 -06:00
|
|
|
newHideSelectorBuffer: [], // Hide style filter buffer
|
|
|
|
newStyleRuleBuffer: [], // Non-hide style filter buffer
|
|
|
|
simpleHideSelectors: { // Hiding filters: simple selectors
|
2016-12-25 14:56:39 -07:00
|
|
|
entries: [],
|
|
|
|
matchesProp: vAPI.matchesProp,
|
|
|
|
selector: undefined,
|
|
|
|
add: function(selector) {
|
|
|
|
this.entries.push(selector);
|
|
|
|
this.selector = undefined;
|
|
|
|
},
|
|
|
|
forEachNode: function(callback, root, extra) {
|
|
|
|
if ( this.selector === undefined ) {
|
|
|
|
this.selector = this.entries.join(extra + ',') + extra;
|
|
|
|
}
|
|
|
|
if ( root[this.matchesProp](this.selector) ) {
|
|
|
|
callback(root);
|
|
|
|
}
|
|
|
|
var nodes = root.querySelectorAll(this.selector),
|
|
|
|
i = nodes.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
callback(nodes[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2017-03-13 11:03:51 -06:00
|
|
|
complexHideSelectors: { // Hiding filters: complex selectors
|
2016-12-25 14:56:39 -07:00
|
|
|
entries: [],
|
|
|
|
selector: undefined,
|
|
|
|
add: function(selector) {
|
|
|
|
this.entries.push(selector);
|
|
|
|
this.selector = undefined;
|
|
|
|
},
|
|
|
|
forEachNode: function(callback) {
|
|
|
|
if ( this.selector === undefined ) {
|
|
|
|
this.selector = this.entries.join(',');
|
|
|
|
}
|
|
|
|
var nodes = document.querySelectorAll(this.selector),
|
|
|
|
i = nodes.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
callback(nodes[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2017-03-13 11:03:51 -06:00
|
|
|
nqsSelectors: [], // Non-querySelector-able filters
|
|
|
|
proceduralSelectors: { // Hiding filters: procedural
|
2016-12-30 08:32:17 -07:00
|
|
|
entries: [],
|
|
|
|
add: function(o) {
|
|
|
|
this.entries.push(new PSelector(o));
|
|
|
|
},
|
|
|
|
forEachNode: function(callback) {
|
|
|
|
var pfilters = this.entries, i = pfilters.length, pfilter, nodes, j;
|
|
|
|
while ( i-- ) {
|
|
|
|
pfilter = pfilters[i];
|
|
|
|
nodes = pfilter.exec();
|
|
|
|
j = nodes.length;
|
|
|
|
while ( j-- ) {
|
|
|
|
callback(nodes[j], pfilter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2016-06-27 17:09:04 -06:00
|
|
|
|
|
|
|
addExceptions: function(aa) {
|
|
|
|
for ( var i = 0, n = aa.length; i < n; i++ ) {
|
2016-10-09 09:09:25 -06:00
|
|
|
allExceptions.add(aa[i]);
|
2016-06-27 17:09:04 -06:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-12-25 14:56:39 -07:00
|
|
|
addSelector: function(selector) {
|
|
|
|
if ( allSelectors.has(selector) || allExceptions.has(selector) ) {
|
2016-08-06 21:29:58 -06:00
|
|
|
return;
|
2016-06-27 17:09:04 -06:00
|
|
|
}
|
2016-12-25 14:56:39 -07:00
|
|
|
allSelectors.add(selector);
|
|
|
|
if ( selector.charCodeAt(0) !== 0x7B /* '{' */ ) {
|
|
|
|
this.newHideSelectorBuffer.push(selector);
|
|
|
|
if ( selector.indexOf(' ') === -1 ) {
|
|
|
|
this.simpleHideSelectors.add(selector);
|
2016-08-03 06:06:51 -06:00
|
|
|
} else {
|
2016-12-25 14:56:39 -07:00
|
|
|
this.complexHideSelectors.add(selector);
|
2016-07-09 17:21:46 -06:00
|
|
|
}
|
2016-08-06 21:29:58 -06:00
|
|
|
return;
|
2016-06-27 17:09:04 -06:00
|
|
|
}
|
2016-12-25 14:56:39 -07:00
|
|
|
var o = JSON.parse(selector);
|
|
|
|
if ( o.style ) {
|
2016-12-30 08:32:17 -07:00
|
|
|
this.newStyleRuleBuffer.push(o.style.join(' '));
|
2017-03-13 11:03:51 -06:00
|
|
|
this.nqsSelectors.push(o.raw);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( o.pseudoclass ) {
|
|
|
|
this.newHideSelectorBuffer.push(o.raw);
|
|
|
|
this.nqsSelectors.push(o.raw);
|
2016-12-25 14:56:39 -07:00
|
|
|
return;
|
|
|
|
}
|
2016-12-30 08:32:17 -07:00
|
|
|
if ( o.tasks ) {
|
2016-12-25 14:56:39 -07:00
|
|
|
this.proceduralSelectors.add(o);
|
2016-12-30 08:32:17 -07:00
|
|
|
return;
|
2016-06-29 15:07:33 -06:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-06-27 17:09:04 -06:00
|
|
|
addSelectors: function(aa) {
|
|
|
|
for ( var i = 0, n = aa.length; i < n; i++ ) {
|
|
|
|
this.addSelector(aa[i]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-08-14 06:51:52 -06:00
|
|
|
commit_: function() {
|
2016-11-12 11:38:41 -07:00
|
|
|
this.commitTimer.clear();
|
2016-08-14 06:51:52 -06:00
|
|
|
|
2016-07-12 11:29:30 -06:00
|
|
|
var beforeHiddenNodeCount = this.hiddenNodeCount,
|
2017-05-15 06:51:25 -06:00
|
|
|
styleText = '';
|
2016-07-09 17:21:46 -06:00
|
|
|
|
2016-12-25 14:56:39 -07:00
|
|
|
// CSS rules/hide
|
|
|
|
if ( this.newHideSelectorBuffer.length ) {
|
|
|
|
styleText = '\n:root ' + this.newHideSelectorBuffer.join(',\n:root ') + '\n{ display: none !important; }';
|
|
|
|
this.newHideSelectorBuffer.length = 0;
|
2016-07-09 17:21:46 -06:00
|
|
|
}
|
|
|
|
|
2016-12-25 14:56:39 -07:00
|
|
|
// CSS rules/any css declaration
|
|
|
|
if ( this.newStyleRuleBuffer.length ) {
|
|
|
|
styleText += '\n' + this.newStyleRuleBuffer.join('\n');
|
|
|
|
this.newStyleRuleBuffer.length = 0;
|
2016-06-27 17:09:04 -06:00
|
|
|
}
|
2016-07-09 17:21:46 -06:00
|
|
|
|
|
|
|
// Simple selectors: incremental.
|
2016-06-28 17:45:11 -06:00
|
|
|
|
2016-12-25 14:56:39 -07:00
|
|
|
// Simple css selectors/hide
|
|
|
|
if ( this.simpleHideSelectors.entries.length ) {
|
2017-05-15 06:51:25 -06:00
|
|
|
var i = stagedNodes.length;
|
2016-07-09 17:21:46 -06:00
|
|
|
while ( i-- ) {
|
2016-12-25 14:56:39 -07:00
|
|
|
this.simpleHideSelectors.forEachNode(hideNode, stagedNodes[i], cssNotHiddenId);
|
2016-07-09 17:21:46 -06:00
|
|
|
}
|
2016-06-28 17:45:11 -06:00
|
|
|
}
|
2016-07-12 11:29:30 -06:00
|
|
|
stagedNodes = [];
|
2016-06-28 17:45:11 -06:00
|
|
|
|
2016-07-09 17:21:46 -06:00
|
|
|
// Complex selectors: non-incremental.
|
|
|
|
complexSelectorsOldResultSet = complexSelectorsCurrentResultSet;
|
2017-05-15 06:51:25 -06:00
|
|
|
complexSelectorsCurrentResultSet = new Set();
|
2016-06-28 17:45:11 -06:00
|
|
|
|
2016-12-25 14:56:39 -07:00
|
|
|
// Complex css selectors/hide
|
2016-07-09 17:21:46 -06:00
|
|
|
// The handling of these can be considered optional, since they are
|
|
|
|
// also applied declaratively using a style tag.
|
2016-12-25 14:56:39 -07:00
|
|
|
if ( this.complexHideSelectors.entries.length ) {
|
|
|
|
this.complexHideSelectors.forEachNode(complexHideNode);
|
2016-07-09 17:21:46 -06:00
|
|
|
}
|
2016-06-28 17:45:11 -06:00
|
|
|
|
2016-12-25 14:56:39 -07:00
|
|
|
// Procedural cosmetic filters
|
|
|
|
if ( this.proceduralSelectors.entries.length ) {
|
|
|
|
this.proceduralSelectors.forEachNode(complexHideNode);
|
2016-06-28 17:45:11 -06:00
|
|
|
}
|
|
|
|
|
2016-09-22 10:18:01 -06:00
|
|
|
// https://github.com/gorhill/uBlock/issues/1912
|
|
|
|
// If one or more nodes have been manually hidden, insert a style tag
|
|
|
|
// targeting these manually hidden nodes. For browsers supporting
|
|
|
|
// user styles, this allows uBO to win.
|
2016-07-12 11:29:30 -06:00
|
|
|
var commitHit = this.hiddenNodeCount !== beforeHiddenNodeCount;
|
|
|
|
if ( commitHit ) {
|
2016-09-24 12:42:31 -06:00
|
|
|
if ( this.hiddenNodeEnforcer === false ) {
|
|
|
|
styleText += '\n:root *[' + this.hiddenId + '][hidden] { display: none !important; }';
|
|
|
|
this.hiddenNodeEnforcer = true;
|
2016-09-22 10:18:01 -06:00
|
|
|
}
|
2016-08-13 14:42:58 -06:00
|
|
|
this.addedNodesHandlerMissCount = 0;
|
2016-07-12 11:29:30 -06:00
|
|
|
} else {
|
2016-08-13 14:42:58 -06:00
|
|
|
this.addedNodesHandlerMissCount += 1;
|
2016-07-12 11:29:30 -06:00
|
|
|
}
|
|
|
|
|
2016-09-24 12:42:31 -06:00
|
|
|
if ( styleText !== '' ) {
|
2016-12-16 14:25:36 -07:00
|
|
|
platformUserCSS.add(styleText);
|
2016-09-24 12:42:31 -06:00
|
|
|
}
|
|
|
|
|
2016-07-09 17:21:46 -06:00
|
|
|
// Un-hide nodes previously hidden.
|
2017-05-15 06:51:25 -06:00
|
|
|
for ( var node of complexSelectorsOldResultSet ) {
|
|
|
|
this.unhideNode(node);
|
2016-06-28 17:45:11 -06:00
|
|
|
}
|
2017-05-15 06:51:25 -06:00
|
|
|
complexSelectorsOldResultSet.clear();
|
2016-06-28 17:45:11 -06:00
|
|
|
|
|
|
|
// If DOM nodes have been affected, lazily notify core process.
|
2016-08-12 06:55:35 -06:00
|
|
|
if (
|
|
|
|
this.loggerEnabled !== false &&
|
|
|
|
commitHit &&
|
|
|
|
cosmeticFiltersActivatedTimer === null
|
|
|
|
) {
|
2016-07-09 17:21:46 -06:00
|
|
|
cosmeticFiltersActivatedTimer = vAPI.setTimeout(
|
|
|
|
cosmeticFiltersActivated,
|
2016-06-28 17:45:11 -06:00
|
|
|
503
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-07-26 06:21:03 -06:00
|
|
|
commit: function(nodes, commitNow) {
|
2016-08-13 14:42:58 -06:00
|
|
|
if ( nodes === 'all' ) {
|
2016-07-12 11:29:30 -06:00
|
|
|
stagedNodes = [ document.documentElement ];
|
|
|
|
} else if ( stagedNodes[0] !== document.documentElement ) {
|
|
|
|
stagedNodes = stagedNodes.concat(nodes);
|
|
|
|
}
|
2016-07-26 06:21:03 -06:00
|
|
|
if ( commitNow ) {
|
2016-11-12 11:38:41 -07:00
|
|
|
this.commitTimer.clear();
|
2016-07-26 06:21:03 -06:00
|
|
|
this.commit_();
|
2016-08-14 06:51:52 -06:00
|
|
|
return;
|
|
|
|
}
|
2016-11-12 11:38:41 -07:00
|
|
|
this.commitTimer.start();
|
2016-07-12 11:29:30 -06:00
|
|
|
},
|
|
|
|
|
2016-12-25 14:56:39 -07:00
|
|
|
createProceduralFilter: function(o) {
|
|
|
|
return new PSelector(o);
|
|
|
|
},
|
|
|
|
|
2016-10-08 13:14:24 -06:00
|
|
|
getExcludeId: function() {
|
|
|
|
if ( this.excludeId === undefined ) {
|
|
|
|
this.excludeId = vAPI.randomToken();
|
2016-07-09 17:21:46 -06:00
|
|
|
}
|
2016-10-08 13:14:24 -06:00
|
|
|
return this.excludeId;
|
|
|
|
},
|
|
|
|
|
|
|
|
hideNode: function(node) {
|
|
|
|
if ( node[this.hiddenId] !== undefined ) { return; }
|
|
|
|
if ( this.excludeId !== undefined && node[this.excludeId] ) { return; }
|
2016-07-09 17:21:46 -06:00
|
|
|
node.setAttribute(this.hiddenId, '');
|
|
|
|
this.hiddenNodeCount += 1;
|
|
|
|
node.hidden = true;
|
2016-07-12 11:29:30 -06:00
|
|
|
node[this.hiddenId] = null;
|
2016-12-15 08:47:32 -07:00
|
|
|
platformHideNode(node);
|
2016-06-28 17:45:11 -06:00
|
|
|
},
|
|
|
|
|
2016-11-12 11:38:41 -07:00
|
|
|
init: function() {
|
|
|
|
this.commitTimer = new vAPI.SafeAnimationFrame(this.commit_.bind(this));
|
|
|
|
},
|
|
|
|
|
2016-07-01 06:09:48 -06:00
|
|
|
showNode: function(node) {
|
2016-07-09 17:21:46 -06:00
|
|
|
node.hidden = false;
|
2016-12-15 08:47:32 -07:00
|
|
|
platformUnhideNode(node);
|
2016-07-01 06:09:48 -06:00
|
|
|
},
|
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
toggleLogging: function(state) {
|
|
|
|
this.loggerEnabled = state;
|
|
|
|
},
|
|
|
|
|
2016-06-27 17:09:04 -06:00
|
|
|
toggleOff: function() {
|
2016-12-16 14:25:36 -07:00
|
|
|
platformUserCSS.toggle(false);
|
2016-06-27 17:09:04 -06:00
|
|
|
this.enabled = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleOn: function() {
|
2016-12-16 14:25:36 -07:00
|
|
|
platformUserCSS.toggle(true);
|
2016-06-27 17:09:04 -06:00
|
|
|
this.enabled = true;
|
2016-06-28 17:45:11 -06:00
|
|
|
},
|
|
|
|
|
2016-12-16 14:25:36 -07:00
|
|
|
userCSS: platformUserCSS,
|
|
|
|
|
2016-06-28 17:45:11 -06:00
|
|
|
unhideNode: function(node) {
|
2016-07-12 11:29:30 -06:00
|
|
|
if ( node[this.hiddenId] !== undefined ) {
|
2016-07-09 17:21:46 -06:00
|
|
|
this.hiddenNodeCount--;
|
|
|
|
}
|
2016-06-28 17:45:11 -06:00
|
|
|
node.removeAttribute(this.hiddenId);
|
2016-07-09 17:21:46 -06:00
|
|
|
node[this.hiddenId] = undefined;
|
|
|
|
node.hidden = false;
|
2016-12-15 08:47:32 -07:00
|
|
|
platformUnhideNode(node);
|
2016-07-01 06:09:48 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
unshowNode: function(node) {
|
2016-07-09 17:21:46 -06:00
|
|
|
node.hidden = true;
|
2016-12-15 08:47:32 -07:00
|
|
|
platformHideNode(node);
|
2016-08-13 14:42:58 -06:00
|
|
|
},
|
|
|
|
|
2016-12-16 14:25:36 -07:00
|
|
|
domChangedHandler: function(addedNodes) {
|
2016-08-14 06:51:52 -06:00
|
|
|
this.commit(addedNodes);
|
2016-08-13 14:42:58 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
start: function() {
|
|
|
|
var domChangedHandler = this.domChangedHandler.bind(this);
|
|
|
|
vAPI.domWatcher.addListener(domChangedHandler);
|
|
|
|
vAPI.shutdown.add(function() {
|
|
|
|
vAPI.domWatcher.removeListener(domChangedHandler);
|
|
|
|
});
|
2016-07-09 17:21:46 -06:00
|
|
|
}
|
2016-06-27 17:09:04 -06:00
|
|
|
};
|
|
|
|
|
2016-07-09 17:21:46 -06:00
|
|
|
/******************************************************************************/
|
2016-06-27 17:09:04 -06:00
|
|
|
|
2016-07-09 17:21:46 -06:00
|
|
|
var hideNode = domFilterer.hideNode.bind(domFilterer);
|
2016-06-28 17:45:11 -06:00
|
|
|
|
2016-07-09 17:21:46 -06:00
|
|
|
var complexHideNode = function(node) {
|
|
|
|
complexSelectorsCurrentResultSet.add(node);
|
|
|
|
if ( !complexSelectorsOldResultSet.delete(node) ) {
|
|
|
|
hideNode(node);
|
|
|
|
}
|
|
|
|
};
|
2015-01-01 19:14:53 -07:00
|
|
|
|
2016-07-09 17:21:46 -06:00
|
|
|
var cssNotHiddenId = ':not([' + domFilterer.hiddenId + '])';
|
2015-01-01 19:14:53 -07:00
|
|
|
|
2016-11-12 11:38:41 -07:00
|
|
|
domFilterer.init();
|
|
|
|
|
2014-10-19 05:11:27 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-07-09 17:21:46 -06:00
|
|
|
return domFilterer;
|
2015-01-13 13:52:15 -07:00
|
|
|
|
2016-06-27 17:09:04 -06:00
|
|
|
/******************************************************************************/
|
2014-06-23 16:42:43 -06:00
|
|
|
|
2016-07-09 17:21:46 -06:00
|
|
|
})();
|
2016-06-27 17:09:04 -06:00
|
|
|
|
2016-07-09 17:21:46 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
2016-06-27 17:09:04 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-07-09 17:21:46 -06:00
|
|
|
// This is executed once, and since no hooks are left behind once the response
|
|
|
|
// is received, I expect this code to be garbage collected by the browser.
|
2016-06-27 17:09:04 -06:00
|
|
|
|
2016-07-09 17:21:46 -06:00
|
|
|
(function domIsLoading() {
|
2016-06-27 17:09:04 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
var responseHandler = function(response) {
|
|
|
|
// cosmetic filtering engine aka 'cfe'
|
|
|
|
var cfeDetails = response && response.specificCosmeticFilters;
|
|
|
|
if ( !cfeDetails || !cfeDetails.ready ) {
|
|
|
|
vAPI.domWatcher = vAPI.domCollapser = vAPI.domFilterer =
|
|
|
|
vAPI.domSurveyor = vAPI.domIsLoaded = null;
|
|
|
|
vAPI.unlock();
|
2016-07-09 17:21:46 -06:00
|
|
|
return;
|
2016-06-27 17:09:04 -06:00
|
|
|
}
|
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
if ( response.noCosmeticFiltering ) {
|
|
|
|
vAPI.domFilterer = null;
|
|
|
|
vAPI.domSurveyor = null;
|
|
|
|
} else {
|
|
|
|
var domFilterer = vAPI.domFilterer;
|
|
|
|
domFilterer.toggleLogging(response.loggerEnabled);
|
|
|
|
if ( response.noGenericCosmeticFiltering || cfeDetails.noDOMSurveying ) {
|
|
|
|
vAPI.domSurveyor = null;
|
|
|
|
}
|
|
|
|
if ( cfeDetails.cosmeticHide.length !== 0 || cfeDetails.cosmeticDonthide.length !== 0 ) {
|
|
|
|
domFilterer.addExceptions(cfeDetails.cosmeticDonthide);
|
|
|
|
domFilterer.addSelectors(cfeDetails.cosmeticHide);
|
2016-08-13 14:42:58 -06:00
|
|
|
domFilterer.commit('all', true);
|
2016-07-09 17:21:46 -06:00
|
|
|
}
|
2016-08-12 06:55:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var parent = document.head || document.documentElement;
|
|
|
|
if ( parent ) {
|
|
|
|
var elem, text;
|
|
|
|
if ( cfeDetails.netHide.length !== 0 ) {
|
|
|
|
elem = document.createElement('style');
|
|
|
|
elem.setAttribute('type', 'text/css');
|
|
|
|
text = cfeDetails.netHide.join(',\n');
|
|
|
|
text += response.collapseBlocked ?
|
|
|
|
'\n{display:none !important;}' :
|
|
|
|
'\n{visibility:hidden !important;}';
|
|
|
|
elem.appendChild(document.createTextNode(text));
|
|
|
|
parent.appendChild(elem);
|
2016-07-09 17:21:46 -06:00
|
|
|
}
|
2016-08-12 06:55:35 -06:00
|
|
|
// Library of resources is located at:
|
|
|
|
// https://github.com/gorhill/uBlock/blob/master/assets/ublock/resources.txt
|
|
|
|
if ( cfeDetails.scripts ) {
|
|
|
|
elem = document.createElement('script');
|
|
|
|
// Have the injected script tag remove itself when execution completes:
|
|
|
|
// to keep DOM as clean as possible.
|
|
|
|
text = cfeDetails.scripts +
|
|
|
|
"\n" +
|
|
|
|
"(function() {\n" +
|
|
|
|
" var c = document.currentScript,\n" +
|
|
|
|
" p = c && c.parentNode;\n" +
|
|
|
|
" if ( p ) {\n" +
|
|
|
|
" p.removeChild(c);\n" +
|
|
|
|
" }\n" +
|
|
|
|
"})();";
|
|
|
|
elem.appendChild(document.createTextNode(text));
|
|
|
|
parent.appendChild(elem);
|
|
|
|
vAPI.injectedScripts = text;
|
2016-07-09 17:21:46 -06:00
|
|
|
}
|
|
|
|
}
|
2015-01-01 18:58:19 -07:00
|
|
|
|
2016-07-09 17:21: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.
|
2016-08-12 06:55:35 -06:00
|
|
|
if ( document.readyState !== 'loading' ) {
|
2016-11-12 11:38:41 -07:00
|
|
|
(new vAPI.SafeAnimationFrame(vAPI.domIsLoaded)).start();
|
2016-08-12 06:55:35 -06:00
|
|
|
} else {
|
|
|
|
document.addEventListener('DOMContentLoaded', vAPI.domIsLoaded);
|
|
|
|
}
|
2016-07-09 17:21:46 -06:00
|
|
|
};
|
2016-06-27 17:09:04 -06:00
|
|
|
|
2016-07-09 17:21:46 -06:00
|
|
|
var url = window.location.href;
|
|
|
|
vAPI.messaging.send(
|
|
|
|
'contentscript',
|
|
|
|
{
|
2016-08-12 06:55:35 -06:00
|
|
|
what: 'retrieveContentScriptParameters',
|
2016-07-09 17:21:46 -06:00
|
|
|
pageURL: url,
|
|
|
|
locationURL: url
|
|
|
|
},
|
|
|
|
responseHandler
|
|
|
|
);
|
2016-06-27 17:09:04 -06:00
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2015-04-07 17:10:03 -06:00
|
|
|
/******************************************************************************/
|
2014-06-23 16:42:43 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
vAPI.domWatcher = (function() {
|
|
|
|
|
|
|
|
var domLayoutObserver = null,
|
2017-08-16 12:10:41 -06:00
|
|
|
ignoreTags = new Set([ 'head', 'link', 'meta', 'script', 'style' ]),
|
2016-08-12 06:55:35 -06:00
|
|
|
addedNodeLists = [],
|
|
|
|
addedNodes = [],
|
|
|
|
removedNodes = false,
|
|
|
|
listeners = [];
|
|
|
|
|
|
|
|
var safeObserverHandler = function() {
|
2016-11-12 11:38:41 -07:00
|
|
|
safeObserverHandlerTimer.clear();
|
2016-08-12 06:55:35 -06:00
|
|
|
var i = addedNodeLists.length,
|
2017-08-16 12:10:41 -06:00
|
|
|
j = addedNodes.length,
|
2016-08-12 06:55:35 -06:00
|
|
|
nodeList, iNode, node;
|
|
|
|
while ( i-- ) {
|
|
|
|
nodeList = addedNodeLists[i];
|
|
|
|
iNode = nodeList.length;
|
|
|
|
while ( iNode-- ) {
|
|
|
|
node = nodeList[iNode];
|
2017-08-16 12:10:41 -06:00
|
|
|
if (
|
|
|
|
node.nodeType === 1 &&
|
|
|
|
ignoreTags.has(node.localName) === false &&
|
|
|
|
node.parentElement !== null
|
|
|
|
) {
|
|
|
|
addedNodes[j++] = node;
|
2016-08-12 06:55:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addedNodeLists.length = 0;
|
2017-08-16 12:10:41 -06:00
|
|
|
if ( j === 0 && removedNodes === false ) { return; }
|
|
|
|
listeners[0](addedNodes);
|
|
|
|
if ( listeners[1] ) {
|
|
|
|
listeners[1](addedNodes);
|
2016-08-12 06:55:35 -06:00
|
|
|
}
|
2017-08-16 12:10:41 -06:00
|
|
|
addedNodes.length = 0;
|
|
|
|
removedNodes = false;
|
2016-08-12 06:55:35 -06:00
|
|
|
};
|
|
|
|
|
2016-11-12 11:38:41 -07:00
|
|
|
var safeObserverHandlerTimer = new vAPI.SafeAnimationFrame(safeObserverHandler);
|
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/205
|
|
|
|
// Do not handle added node directly from within mutation observer.
|
|
|
|
var observerHandler = function(mutations) {
|
|
|
|
var nodeList, mutation,
|
|
|
|
i = mutations.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
mutation = mutations[i];
|
|
|
|
nodeList = mutation.addedNodes;
|
|
|
|
if ( nodeList.length !== 0 ) {
|
|
|
|
addedNodeLists.push(nodeList);
|
|
|
|
}
|
|
|
|
if ( mutation.removedNodes.length !== 0 ) {
|
|
|
|
removedNodes = true;
|
|
|
|
}
|
|
|
|
}
|
2016-11-12 11:38:41 -07:00
|
|
|
if ( addedNodeLists.length !== 0 || removedNodes ) {
|
|
|
|
safeObserverHandlerTimer.start();
|
2016-08-12 06:55:35 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var addListener = function(listener) {
|
|
|
|
if ( listeners.indexOf(listener) !== -1 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
listeners.push(listener);
|
|
|
|
if ( domLayoutObserver !== null ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
domLayoutObserver = new MutationObserver(observerHandler);
|
|
|
|
domLayoutObserver.observe(document.documentElement, {
|
|
|
|
//attributeFilter: [ 'class', 'id' ],
|
|
|
|
//attributes: true,
|
|
|
|
childList: true,
|
|
|
|
subtree: true
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var removeListener = function(listener) {
|
|
|
|
var pos = listeners.indexOf(listener);
|
|
|
|
if ( pos === -1 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
listeners.splice(pos, 1);
|
|
|
|
if ( listeners.length !== 0 || domLayoutObserver === null ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
domLayoutObserver.disconnect();
|
|
|
|
domLayoutObserver = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
var start = function() {
|
2016-08-13 14:42:58 -06:00
|
|
|
vAPI.shutdown.add(function() {
|
|
|
|
if ( domLayoutObserver !== null ) {
|
|
|
|
domLayoutObserver.disconnect();
|
|
|
|
domLayoutObserver = null;
|
|
|
|
}
|
2016-11-12 11:38:41 -07:00
|
|
|
safeObserverHandlerTimer.clear();
|
2016-08-13 14:42:58 -06:00
|
|
|
});
|
2016-08-12 06:55:35 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
addListener: addListener,
|
|
|
|
removeListener: removeListener,
|
|
|
|
start: start
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
vAPI.domCollapser = (function() {
|
2017-08-03 08:18:05 -06:00
|
|
|
var resquestIdGenerator = 1,
|
|
|
|
processTimer,
|
|
|
|
toProcess = [],
|
|
|
|
toFilter = [],
|
|
|
|
toCollapse = new Map(),
|
|
|
|
cachedBlockedSet,
|
|
|
|
cachedBlockedSetHash,
|
|
|
|
cachedBlockedSetTimer;
|
2015-06-04 09:17:02 -06:00
|
|
|
var src1stProps = {
|
2015-03-29 10:13:28 -06:00
|
|
|
'embed': 'src',
|
2017-08-03 08:18:05 -06:00
|
|
|
'iframe': 'src',
|
2015-03-29 10:13:28 -06:00
|
|
|
'img': 'src',
|
|
|
|
'object': 'data'
|
|
|
|
};
|
2015-06-04 09:17:02 -06:00
|
|
|
var src2ndProps = {
|
|
|
|
'img': 'srcset'
|
|
|
|
};
|
2017-08-03 08:18:05 -06:00
|
|
|
var tagToTypeMap = {
|
|
|
|
embed: 'object',
|
|
|
|
iframe: 'sub_frame',
|
|
|
|
img: 'image',
|
|
|
|
object: 'object'
|
|
|
|
};
|
|
|
|
var netSelectorCacheCount = 0,
|
|
|
|
messaging = vAPI.messaging;
|
2017-08-16 12:10:41 -06:00
|
|
|
|
2017-08-03 08:18:05 -06:00
|
|
|
var cachedBlockedSetClear = function() {
|
|
|
|
cachedBlockedSet =
|
|
|
|
cachedBlockedSetHash =
|
|
|
|
cachedBlockedSetTimer = undefined;
|
2015-03-29 10:13:28 -06:00
|
|
|
};
|
|
|
|
|
2017-08-03 08:18:05 -06:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/174
|
|
|
|
// Do not remove fragment from src URL
|
2015-04-07 17:10:03 -06:00
|
|
|
var onProcessed = function(response) {
|
2017-08-16 16:06:04 -06:00
|
|
|
if ( !response ) { // This happens if uBO is disabled or restarted.
|
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
|
|
|
|
|
|
|
var targets = toCollapse.get(response.id);
|
|
|
|
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 10:13:28 -06:00
|
|
|
}
|
2017-08-16 12:10:41 -06:00
|
|
|
if ( cachedBlockedSet === undefined || cachedBlockedSet.size === 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-08 07:53:35 -06:00
|
|
|
var selectors = [],
|
2017-08-03 08:18:05 -06:00
|
|
|
iframeLoadEventPatch = vAPI.iframeLoadEventPatch,
|
2016-08-08 07:53:35 -06:00
|
|
|
netSelectorCacheCountMax = response.netSelectorCacheCountMax,
|
2017-08-03 08:18:05 -06:00
|
|
|
tag, prop, src, value;
|
|
|
|
|
|
|
|
for ( var target of targets ) {
|
|
|
|
tag = target.localName;
|
|
|
|
prop = src1stProps[tag];
|
|
|
|
if ( prop === undefined ) { continue; }
|
|
|
|
src = target[prop];
|
|
|
|
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;
|
|
|
|
}
|
2017-08-03 08:18:05 -06: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
|
|
|
|
if (
|
|
|
|
netSelectorCacheCount <= netSelectorCacheCountMax &&
|
|
|
|
(value = target.getAttribute(prop))
|
|
|
|
) {
|
|
|
|
selectors.push(tag + '[' + prop + '="' + value + '"]');
|
|
|
|
netSelectorCacheCount += 1;
|
2016-08-08 07:53:35 -06:00
|
|
|
}
|
2017-08-03 08:18:05 -06:00
|
|
|
if ( iframeLoadEventPatch !== undefined ) {
|
|
|
|
iframeLoadEventPatch(target);
|
2015-03-29 10:13:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( selectors.length !== 0 ) {
|
2016-03-06 08:51:06 -07:00
|
|
|
messaging.send(
|
|
|
|
'contentscript',
|
|
|
|
{
|
|
|
|
what: 'cosmeticFiltersInjected',
|
|
|
|
type: 'net',
|
|
|
|
hostname: window.location.hostname,
|
|
|
|
selectors: selectors
|
|
|
|
}
|
|
|
|
);
|
2015-03-29 10:13:28 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var send = function() {
|
2017-08-03 08:18:05 -06:00
|
|
|
processTimer = undefined;
|
|
|
|
toCollapse.set(resquestIdGenerator, toProcess);
|
2016-08-22 17:13:52 -06:00
|
|
|
// https://github.com/gorhill/uBlock/issues/1927
|
2017-08-03 08:18:05 -06:00
|
|
|
// Normalize hostname to avoid trailing dot of FQHN.
|
2016-08-22 17:13:52 -06:00
|
|
|
var pageHostname = window.location.hostname || '';
|
|
|
|
if (
|
|
|
|
pageHostname.length &&
|
|
|
|
pageHostname.charCodeAt(pageHostname.length - 1) === 0x2e
|
|
|
|
) {
|
|
|
|
pageHostname = pageHostname.slice(0, -1);
|
|
|
|
}
|
2017-08-03 08:18:05 -06:00
|
|
|
var msg = {
|
|
|
|
what: 'getCollapsibleBlockedRequests',
|
|
|
|
id: resquestIdGenerator,
|
|
|
|
pageURL: window.location.href,
|
|
|
|
pageHostname: pageHostname,
|
|
|
|
resources: toFilter,
|
|
|
|
hash: cachedBlockedSetHash
|
|
|
|
};
|
|
|
|
messaging.send('contentscript', msg, onProcessed);
|
|
|
|
toProcess = [];
|
|
|
|
toFilter = [];
|
|
|
|
resquestIdGenerator += 1;
|
2015-03-29 10:13:28 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
var 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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var add = function(target) {
|
2017-08-03 08:18:05 -06:00
|
|
|
toProcess[toProcess.length] = target;
|
2015-03-29 10:13:28 -06:00
|
|
|
};
|
|
|
|
|
2016-02-03 17:15:28 -07:00
|
|
|
var addMany = function(targets) {
|
|
|
|
var i = targets.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
add(targets[i]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-01 17:06:52 -06:00
|
|
|
var iframeSourceModified = function(mutations) {
|
|
|
|
var i = mutations.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
addIFrame(mutations[i].target, true);
|
|
|
|
}
|
|
|
|
process();
|
|
|
|
};
|
|
|
|
var iframeSourceObserver = new MutationObserver(iframeSourceModified);
|
|
|
|
var iframeSourceObserverOptions = {
|
|
|
|
attributes: true,
|
|
|
|
attributeFilter: [ 'src' ]
|
|
|
|
};
|
|
|
|
|
2016-01-21 07:33:54 -07:00
|
|
|
var primeLocalIFrame = function(iframe) {
|
|
|
|
// Should probably also copy injected styles.
|
2016-03-05 12:59:01 -07:00
|
|
|
// The injected scripts are those which were injected in the current
|
|
|
|
// document, from within the `contentscript-start.js / injectScripts`,
|
|
|
|
// and which scripts are selectively looked-up from:
|
|
|
|
// https://github.com/gorhill/uBlock/blob/master/assets/ublock/resources.txt
|
2016-01-21 07:33:54 -07:00
|
|
|
if ( vAPI.injectedScripts ) {
|
|
|
|
var scriptTag = document.createElement('script');
|
|
|
|
scriptTag.appendChild(document.createTextNode(vAPI.injectedScripts));
|
|
|
|
var parent = iframe.contentDocument && iframe.contentDocument.head;
|
|
|
|
if ( parent ) {
|
|
|
|
parent.appendChild(scriptTag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-01 17:06:52 -06:00
|
|
|
var addIFrame = function(iframe, dontObserve) {
|
|
|
|
// https://github.com/gorhill/uBlock/issues/162
|
|
|
|
// Be prepared to deal with possible change of src attribute.
|
|
|
|
if ( dontObserve !== true ) {
|
|
|
|
iframeSourceObserver.observe(iframe, iframeSourceObserverOptions);
|
|
|
|
}
|
|
|
|
|
2015-03-29 10:13:28 -06:00
|
|
|
var src = iframe.src;
|
|
|
|
if ( src === '' || typeof src !== 'string' ) {
|
2016-01-21 07:33:54 -07:00
|
|
|
primeLocalIFrame(iframe);
|
2015-03-29 10:13:28 -06:00
|
|
|
return;
|
|
|
|
}
|
2017-08-03 08:18:05 -06:00
|
|
|
if ( src.lastIndexOf('http', 0) !== 0 ) { return; }
|
|
|
|
toFilter[toFilter.length] = {
|
|
|
|
type: 'sub_frame',
|
|
|
|
url: iframe.src
|
|
|
|
};
|
|
|
|
add(iframe);
|
2015-03-29 10:13:28 -06:00
|
|
|
};
|
|
|
|
|
2016-02-03 17:15:28 -07:00
|
|
|
var addIFrames = function(iframes) {
|
2016-02-03 16:47:30 -07:00
|
|
|
var i = iframes.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
addIFrame(iframes[i]);
|
|
|
|
}
|
2016-02-03 17:15:28 -07:00
|
|
|
};
|
|
|
|
|
2016-08-13 14:42:58 -06:00
|
|
|
var onResourceFailed = function(ev) {
|
2017-08-03 08:18:05 -06:00
|
|
|
if ( tagToTypeMap[ev.target.localName] !== undefined ) {
|
|
|
|
vAPI.domCollapser.add(ev.target);
|
|
|
|
vAPI.domCollapser.process();
|
|
|
|
}
|
2016-08-13 14:42:58 -06:00
|
|
|
};
|
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
var domChangedHandler = function(nodes) {
|
|
|
|
var node;
|
|
|
|
for ( var i = 0, ni = nodes.length; i < ni; i++ ) {
|
|
|
|
node = nodes[i];
|
|
|
|
if ( node.localName === 'iframe' ) {
|
|
|
|
addIFrame(node);
|
|
|
|
}
|
2017-06-22 12:36:45 -06:00
|
|
|
if ( node.childElementCount !== 0 ) {
|
2016-08-12 06:55:35 -06:00
|
|
|
var iframes = node.getElementsByTagName('iframe');
|
|
|
|
if ( iframes.length !== 0 ) {
|
|
|
|
addIFrames(iframes);
|
|
|
|
}
|
|
|
|
}
|
2016-06-28 17:45:11 -06:00
|
|
|
}
|
2016-08-12 06:55:35 -06:00
|
|
|
process();
|
|
|
|
};
|
|
|
|
|
|
|
|
var start = function() {
|
|
|
|
// 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
|
|
|
|
var elems = document.images || document.getElementsByTagName('img'),
|
|
|
|
i = elems.length, elem;
|
|
|
|
while ( i-- ) {
|
|
|
|
elem = elems[i];
|
|
|
|
if ( elem.complete ) {
|
|
|
|
add(elem);
|
2016-06-28 17:45:11 -06:00
|
|
|
}
|
2016-02-03 17:15:28 -07:00
|
|
|
}
|
2016-08-12 06:55:35 -06:00
|
|
|
addMany(document.embeds || document.getElementsByTagName('embed'));
|
|
|
|
addMany(document.getElementsByTagName('object'));
|
|
|
|
addIFrames(document.getElementsByTagName('iframe'));
|
|
|
|
process(0);
|
|
|
|
|
|
|
|
document.addEventListener('error', onResourceFailed, true);
|
2016-08-13 14:42:58 -06:00
|
|
|
vAPI.domWatcher.addListener(domChangedHandler);
|
2016-08-12 06:55:35 -06:00
|
|
|
|
|
|
|
vAPI.shutdown.add(function() {
|
|
|
|
document.removeEventListener('error', onResourceFailed, true);
|
2016-08-13 14:42:58 -06:00
|
|
|
vAPI.domWatcher.removeListener(domChangedHandler);
|
2017-08-03 08:18:05 -06:00
|
|
|
if ( processTimer !== undefined ) {
|
|
|
|
clearTimeout(processTimer);
|
2016-08-12 06:55:35 -06:00
|
|
|
}
|
|
|
|
});
|
2015-03-29 10:13:28 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
add: add,
|
2016-02-03 17:15:28 -07:00
|
|
|
addMany: addMany,
|
2015-03-29 10:13:28 -06:00
|
|
|
addIFrame: addIFrame,
|
2016-02-03 17:15:28 -07:00
|
|
|
addIFrames: addIFrames,
|
2016-08-12 06:55:35 -06:00
|
|
|
process: process,
|
|
|
|
start: start
|
2015-03-29 10:13:28 -06:00
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2016-06-27 17:09:04 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
vAPI.domSurveyor = (function() {
|
|
|
|
var domFilterer = null,
|
|
|
|
messaging = vAPI.messaging,
|
|
|
|
surveyPhase3Nodes = [],
|
|
|
|
cosmeticSurveyingMissCount = 0,
|
|
|
|
highGenerics = null,
|
|
|
|
lowGenericSelectors = [],
|
2017-05-15 06:51:25 -06:00
|
|
|
queriedSelectors = new Set(),
|
2016-10-06 08:49:46 -06:00
|
|
|
surveyCost = 0;
|
2014-06-23 16:42:43 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
// Handle main process' response.
|
|
|
|
|
|
|
|
var surveyPhase3 = function(response) {
|
|
|
|
var result = response && response.result,
|
|
|
|
firstSurvey = highGenerics === null;
|
2015-04-07 17:10:03 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
if ( result ) {
|
|
|
|
if ( result.hide.length ) {
|
|
|
|
processLowGenerics(result.hide);
|
2016-06-27 17:09:04 -06:00
|
|
|
}
|
2016-08-12 06:55:35 -06:00
|
|
|
if ( result.highGenerics ) {
|
|
|
|
highGenerics = result.highGenerics;
|
2014-08-12 10:19:54 -06:00
|
|
|
}
|
2016-08-12 06:55:35 -06:00
|
|
|
}
|
2016-07-09 17:21:46 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
if ( highGenerics ) {
|
2016-10-06 08:49:46 -06:00
|
|
|
var t0 = window.performance.now();
|
2016-08-12 06:55:35 -06:00
|
|
|
if ( highGenerics.hideLowCount ) {
|
|
|
|
processHighLowGenerics(highGenerics.hideLow);
|
2014-08-12 10:19:54 -06:00
|
|
|
}
|
2016-08-12 06:55:35 -06:00
|
|
|
if ( highGenerics.hideMediumCount ) {
|
|
|
|
processHighMediumGenerics(highGenerics.hideMedium);
|
2014-08-12 10:19:54 -06:00
|
|
|
}
|
2016-08-12 06:55:35 -06:00
|
|
|
if ( highGenerics.hideHighSimpleCount || highGenerics.hideHighComplexCount ) {
|
|
|
|
processHighHighGenerics();
|
|
|
|
}
|
2016-10-06 08:49:46 -06:00
|
|
|
surveyCost += window.performance.now() - t0;
|
2016-08-12 06:55:35 -06:00
|
|
|
}
|
2016-06-28 20:01:15 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
// Need to do this before committing DOM filterer, as needed info
|
|
|
|
// will no longer be there after commit.
|
2016-12-25 14:56:39 -07:00
|
|
|
if ( firstSurvey || domFilterer.newHideSelectorBuffer.length ) {
|
2016-08-12 06:55:35 -06:00
|
|
|
messaging.send(
|
|
|
|
'contentscript',
|
|
|
|
{
|
|
|
|
what: 'cosmeticFiltersInjected',
|
|
|
|
type: 'cosmetic',
|
|
|
|
hostname: window.location.hostname,
|
2016-12-25 14:56:39 -07:00
|
|
|
selectors: domFilterer.newHideSelectorBuffer,
|
2016-10-06 08:49:46 -06:00
|
|
|
first: firstSurvey,
|
|
|
|
cost: surveyCost
|
2016-07-12 11:29:30 -06:00
|
|
|
}
|
2016-08-12 06:55:35 -06:00
|
|
|
);
|
|
|
|
}
|
2016-07-12 11:29:30 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
// Shutdown surveyor if too many consecutive empty resultsets.
|
2016-12-25 14:56:39 -07:00
|
|
|
if ( domFilterer.newHideSelectorBuffer.length === 0 ) {
|
2016-08-12 06:55:35 -06:00
|
|
|
cosmeticSurveyingMissCount += 1;
|
|
|
|
} else {
|
|
|
|
cosmeticSurveyingMissCount = 0;
|
|
|
|
}
|
2016-06-30 14:10:38 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
domFilterer.commit(surveyPhase3Nodes);
|
|
|
|
surveyPhase3Nodes = [];
|
|
|
|
};
|
2015-01-01 06:09:44 -07:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
// Query main process.
|
2014-09-16 17:16:18 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
var surveyPhase2 = function(addedNodes) {
|
|
|
|
surveyPhase3Nodes = surveyPhase3Nodes.concat(addedNodes);
|
|
|
|
if ( lowGenericSelectors.length !== 0 || highGenerics === null ) {
|
|
|
|
messaging.send(
|
|
|
|
'contentscript',
|
|
|
|
{
|
|
|
|
what: 'retrieveGenericCosmeticSelectors',
|
|
|
|
pageURL: window.location.href,
|
|
|
|
selectors: lowGenericSelectors,
|
|
|
|
firstSurvey: highGenerics === null
|
|
|
|
},
|
|
|
|
surveyPhase3
|
|
|
|
);
|
|
|
|
lowGenericSelectors = [];
|
|
|
|
} else {
|
|
|
|
surveyPhase3(null);
|
|
|
|
}
|
|
|
|
};
|
2014-07-04 14:47:34 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
// Low generics:
|
|
|
|
// - [id]
|
|
|
|
// - [class]
|
2014-09-16 17:16:18 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
var processLowGenerics = function(generics) {
|
|
|
|
domFilterer.addSelectors(generics);
|
|
|
|
};
|
2014-07-04 14:47:34 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
// High-low generics:
|
|
|
|
// - [alt="..."]
|
|
|
|
// - [title="..."]
|
2014-09-16 17:16:18 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
var processHighLowGenerics = function(generics) {
|
|
|
|
var attrs = ['title', 'alt'];
|
|
|
|
var attr, attrValue, nodeList, iNode, node;
|
|
|
|
var selector;
|
|
|
|
while ( (attr = attrs.pop()) ) {
|
|
|
|
nodeList = selectNodes('[' + attr + ']', surveyPhase3Nodes);
|
|
|
|
iNode = nodeList.length;
|
|
|
|
while ( iNode-- ) {
|
|
|
|
node = nodeList[iNode];
|
|
|
|
attrValue = node.getAttribute(attr);
|
|
|
|
if ( !attrValue ) { continue; }
|
|
|
|
// Candidate 1 = generic form
|
|
|
|
// If generic form is injected, no need to process the
|
|
|
|
// specific form, as the generic will affect all related
|
|
|
|
// specific forms.
|
|
|
|
selector = '[' + attr + '="' + attrValue + '"]';
|
|
|
|
if ( generics.hasOwnProperty(selector) ) {
|
|
|
|
domFilterer.addSelector(selector);
|
2016-06-27 17:09:04 -06:00
|
|
|
continue;
|
2014-08-12 10:19:54 -06:00
|
|
|
}
|
2016-08-12 06:55:35 -06:00
|
|
|
// Candidate 2 = specific form
|
|
|
|
selector = node.localName + selector;
|
|
|
|
if ( generics.hasOwnProperty(selector) ) {
|
|
|
|
domFilterer.addSelector(selector);
|
2014-08-12 10:19:54 -06:00
|
|
|
}
|
|
|
|
}
|
2016-08-12 06:55:35 -06:00
|
|
|
}
|
|
|
|
};
|
2014-07-04 14:47:34 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
// High-medium generics:
|
|
|
|
// - [href^="http"]
|
2014-09-16 17:16:18 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
var processHighMediumGenerics = function(generics) {
|
|
|
|
var stagedNodes = surveyPhase3Nodes,
|
|
|
|
i = stagedNodes.length;
|
|
|
|
if ( i === 1 && stagedNodes[0] === document.documentElement ) {
|
|
|
|
processHighMediumGenericsForNodes(document.links, generics);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var aa = [ null ],
|
|
|
|
node, nodes;
|
|
|
|
while ( i-- ) {
|
|
|
|
node = stagedNodes[i];
|
|
|
|
if ( node.localName === 'a' ) {
|
|
|
|
aa[0] = node;
|
|
|
|
processHighMediumGenericsForNodes(aa, generics);
|
2015-09-11 15:59:25 -06:00
|
|
|
}
|
2016-08-12 06:55:35 -06:00
|
|
|
nodes = node.getElementsByTagName('a');
|
|
|
|
if ( nodes.length !== 0 ) {
|
|
|
|
processHighMediumGenericsForNodes(nodes, generics);
|
2014-07-02 10:02:29 -06:00
|
|
|
}
|
2016-08-12 06:55:35 -06:00
|
|
|
}
|
|
|
|
};
|
2014-09-16 17:16:18 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
var processHighMediumGenericsForNodes = function(nodes, generics) {
|
|
|
|
var i = nodes.length,
|
|
|
|
node, href, pos, entry, j, selector;
|
|
|
|
while ( i-- ) {
|
|
|
|
node = nodes[i];
|
|
|
|
href = node.getAttribute('href');
|
|
|
|
if ( !href ) { continue; }
|
|
|
|
pos = href.indexOf('://');
|
|
|
|
if ( pos === -1 ) { continue; }
|
|
|
|
entry = generics[href.slice(pos + 3, pos + 11)];
|
|
|
|
if ( entry === undefined ) { continue; }
|
|
|
|
if ( typeof entry === 'string' ) {
|
|
|
|
if ( href.lastIndexOf(entry.slice(8, -2), 0) === 0 ) {
|
|
|
|
domFilterer.addSelector(entry);
|
|
|
|
}
|
|
|
|
continue;
|
2016-07-09 17:21:46 -06:00
|
|
|
}
|
2016-08-12 06:55:35 -06:00
|
|
|
j = entry.length;
|
|
|
|
while ( j-- ) {
|
|
|
|
selector = entry[j];
|
|
|
|
if ( href.lastIndexOf(selector.slice(8, -2), 0) === 0 ) {
|
|
|
|
domFilterer.addSelector(selector);
|
2016-06-28 20:01:15 -06:00
|
|
|
}
|
2015-03-01 18:26:33 -07:00
|
|
|
}
|
2016-08-12 06:55:35 -06:00
|
|
|
}
|
|
|
|
};
|
2015-09-04 14:30:53 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
var highHighSimpleGenericsCost = 0,
|
|
|
|
highHighSimpleGenericsInjected = false,
|
|
|
|
highHighComplexGenericsCost = 0,
|
|
|
|
highHighComplexGenericsInjected = false;
|
|
|
|
|
|
|
|
var processHighHighGenerics = function() {
|
|
|
|
var tstart;
|
|
|
|
// Simple selectors.
|
|
|
|
if (
|
|
|
|
highHighSimpleGenericsInjected === false &&
|
|
|
|
highHighSimpleGenericsCost < 50 &&
|
|
|
|
highGenerics.hideHighSimpleCount !== 0
|
|
|
|
) {
|
|
|
|
tstart = window.performance.now();
|
|
|
|
var matchesProp = vAPI.matchesProp,
|
|
|
|
nodes = surveyPhase3Nodes,
|
|
|
|
i = nodes.length, node;
|
2016-07-09 17:21:46 -06:00
|
|
|
while ( i-- ) {
|
|
|
|
node = nodes[i];
|
2016-08-12 06:55:35 -06:00
|
|
|
if (
|
|
|
|
node[matchesProp](highGenerics.hideHighSimple) ||
|
|
|
|
node.querySelector(highGenerics.hideHighSimple) !== null
|
|
|
|
) {
|
|
|
|
highHighSimpleGenericsInjected = true;
|
|
|
|
domFilterer.addSelectors(highGenerics.hideHighSimple.split(',\n'));
|
|
|
|
break;
|
2015-09-04 14:30:53 -06:00
|
|
|
}
|
2014-07-02 10:02:29 -06:00
|
|
|
}
|
2016-08-12 06:55:35 -06:00
|
|
|
highHighSimpleGenericsCost += window.performance.now() - tstart;
|
|
|
|
}
|
|
|
|
// Complex selectors.
|
|
|
|
if (
|
|
|
|
highHighComplexGenericsInjected === false &&
|
|
|
|
highHighComplexGenericsCost < 50 &&
|
|
|
|
highGenerics.hideHighComplexCount !== 0
|
|
|
|
) {
|
|
|
|
tstart = window.performance.now();
|
|
|
|
if ( document.querySelector(highGenerics.hideHighComplex) !== null ) {
|
|
|
|
highHighComplexGenericsInjected = true;
|
|
|
|
domFilterer.addSelectors(highGenerics.hideHighComplex.split(',\n'));
|
|
|
|
}
|
|
|
|
highHighComplexGenericsCost += window.performance.now() - tstart;
|
|
|
|
}
|
|
|
|
};
|
2014-07-30 06:05:00 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
// Extract and return the staged nodes which (may) match the selectors.
|
2015-03-29 10:13:28 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
var selectNodes = function(selector, nodes) {
|
|
|
|
var stagedNodes = nodes,
|
|
|
|
i = stagedNodes.length;
|
|
|
|
if ( i === 1 && stagedNodes[0] === document.documentElement ) {
|
|
|
|
return document.querySelectorAll(selector);
|
|
|
|
}
|
|
|
|
var targetNodes = [],
|
|
|
|
node, nodeList, j;
|
|
|
|
while ( i-- ) {
|
|
|
|
node = stagedNodes[i];
|
|
|
|
targetNodes.push(node);
|
|
|
|
nodeList = node.querySelectorAll(selector);
|
|
|
|
j = nodeList.length;
|
|
|
|
while ( j-- ) {
|
|
|
|
targetNodes.push(nodeList[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return targetNodes;
|
2014-08-12 10:19:54 -06:00
|
|
|
};
|
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
// Extract all classes/ids: these will be passed to the cosmetic
|
|
|
|
// filtering engine, and in return we will obtain only the relevant
|
|
|
|
// CSS selectors.
|
2014-09-16 13:39:21 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
// 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
|
2016-06-30 14:10:38 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
var surveyPhase1 = function(addedNodes) {
|
2016-10-06 08:49:46 -06:00
|
|
|
var t0 = window.performance.now(),
|
2017-02-12 13:53:40 -07:00
|
|
|
rews = reWhitespace,
|
2016-10-06 08:49:46 -06:00
|
|
|
qq = queriedSelectors,
|
|
|
|
ll = lowGenericSelectors,
|
2017-02-12 13:53:40 -07:00
|
|
|
lli = ll.length,
|
|
|
|
nodes, i, node, v, vv, j;
|
|
|
|
nodes = selectNodes('[id]', addedNodes);
|
|
|
|
i = nodes.length;
|
2016-08-12 06:55:35 -06:00
|
|
|
while ( i-- ) {
|
|
|
|
node = nodes[i];
|
|
|
|
v = node.id;
|
2017-02-12 13:53:40 -07:00
|
|
|
if ( typeof v !== 'string' ) { continue; }
|
|
|
|
v = '#' + v.trim();
|
2017-08-16 12:10:41 -06:00
|
|
|
if ( qq.has(v) === false && v.length !== 1 ) {
|
2017-02-12 13:53:40 -07:00
|
|
|
ll[lli] = v; lli++; qq.add(v);
|
2014-07-30 06:05:00 -06:00
|
|
|
}
|
2017-02-12 13:53:40 -07:00
|
|
|
}
|
|
|
|
nodes = selectNodes('[class]', addedNodes);
|
|
|
|
i = nodes.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
node = nodes[i];
|
2016-08-12 06:55:35 -06:00
|
|
|
vv = node.className;
|
2017-02-12 13:53:40 -07:00
|
|
|
if ( typeof vv !== 'string' ) { continue; }
|
2017-08-16 12:10:41 -06:00
|
|
|
if ( rews.test(vv) === false ) {
|
2016-08-12 06:55:35 -06:00
|
|
|
v = '.' + vv;
|
2017-08-16 12:10:41 -06:00
|
|
|
if ( qq.has(v) === false && v.length !== 1 ) {
|
2017-02-12 13:53:40 -07:00
|
|
|
ll[lli] = v; lli++; qq.add(v);
|
|
|
|
}
|
2016-07-09 17:21:46 -06:00
|
|
|
} else {
|
2016-08-12 06:55:35 -06:00
|
|
|
vv = node.classList;
|
|
|
|
j = vv.length;
|
|
|
|
while ( j-- ) {
|
|
|
|
v = '.' + vv[j];
|
2017-08-16 12:10:41 -06:00
|
|
|
if ( qq.has(v) === false ) {
|
2017-02-12 13:53:40 -07:00
|
|
|
ll[lli] = v; lli++; qq.add(v);
|
|
|
|
}
|
2016-07-12 11:29:30 -06:00
|
|
|
}
|
2016-07-09 17:21:46 -06:00
|
|
|
}
|
2014-07-30 06:05:00 -06:00
|
|
|
}
|
2016-10-06 08:49:46 -06:00
|
|
|
surveyCost += window.performance.now() - t0;
|
2016-08-12 06:55:35 -06:00
|
|
|
surveyPhase2(addedNodes);
|
2014-07-02 10:02:29 -06:00
|
|
|
};
|
2017-02-12 13:53:40 -07:00
|
|
|
var reWhitespace = /\s/;
|
2014-08-12 10:19:54 -06:00
|
|
|
|
2016-12-16 14:25:36 -07:00
|
|
|
var domChangedHandler = function(addedNodes) {
|
2016-08-13 14:42:58 -06:00
|
|
|
if ( cosmeticSurveyingMissCount > 255 ) {
|
|
|
|
vAPI.domWatcher.removeListener(domChangedHandler);
|
|
|
|
vAPI.domSurveyor = null;
|
2016-12-16 14:25:36 -07:00
|
|
|
domFilterer.domChangedHandler(addedNodes);
|
2016-08-13 14:42:58 -06:00
|
|
|
domFilterer.start();
|
|
|
|
return;
|
2016-06-28 07:06:14 -06:00
|
|
|
}
|
2016-08-13 14:42:58 -06:00
|
|
|
|
|
|
|
surveyPhase1(addedNodes);
|
2014-09-16 13:39:21 -06:00
|
|
|
};
|
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
var start = function() {
|
|
|
|
domFilterer = vAPI.domFilterer;
|
|
|
|
domChangedHandler([ document.documentElement ]);
|
2016-08-13 14:42:58 -06:00
|
|
|
vAPI.domWatcher.addListener(domChangedHandler);
|
|
|
|
vAPI.shutdown.add(function() {
|
|
|
|
vAPI.domWatcher.removeListener(domChangedHandler);
|
|
|
|
});
|
2015-04-07 17:10:03 -06:00
|
|
|
};
|
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
return {
|
|
|
|
start: start
|
|
|
|
};
|
2015-04-07 17:10:03 -06:00
|
|
|
})();
|
2014-06-23 16:42:43 -06:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
2014-09-14 14:20:40 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
vAPI.domIsLoaded = function(ev) {
|
2016-08-13 19:45:01 -06:00
|
|
|
// This can happen on Firefox. For instance:
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1893
|
|
|
|
if ( window.location === null ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-14 06:51:52 -06:00
|
|
|
var slowLoad = ev instanceof Event;
|
|
|
|
if ( slowLoad ) {
|
2016-08-12 06:55:35 -06:00
|
|
|
document.removeEventListener('DOMContentLoaded', vAPI.domIsLoaded);
|
2015-03-29 10:13:28 -06:00
|
|
|
}
|
2016-08-12 06:55:35 -06:00
|
|
|
vAPI.domIsLoaded = null;
|
2014-09-14 14:20:40 -06:00
|
|
|
|
2016-08-13 14:42:58 -06:00
|
|
|
vAPI.domWatcher.start();
|
|
|
|
vAPI.domCollapser.start();
|
2014-09-28 12:38:17 -06:00
|
|
|
|
2016-08-13 14:42:58 -06:00
|
|
|
if ( vAPI.domFilterer ) {
|
2016-08-14 06:51:52 -06:00
|
|
|
// To avoid neddless CPU overhead, we commit existing cosmetic filters
|
|
|
|
// only if the page loaded "slowly", i.e. if the code here had to wait
|
|
|
|
// for a DOMContentLoaded event -- in which case the DOM may have
|
|
|
|
// changed a lot since last time the domFilterer acted on it.
|
|
|
|
if ( slowLoad ) {
|
|
|
|
vAPI.domFilterer.commit('all');
|
|
|
|
}
|
2016-08-13 14:42:58 -06:00
|
|
|
if ( vAPI.domSurveyor ) {
|
|
|
|
vAPI.domSurveyor.start();
|
|
|
|
} else {
|
|
|
|
vAPI.domFilterer.start();
|
|
|
|
}
|
2015-02-16 10:17:26 -07:00
|
|
|
}
|
2016-03-06 08:51:06 -07:00
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
// To send mouse coordinates to main process, as the chrome API fails
|
|
|
|
// to provide the mouse position to context menu listeners.
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/1143
|
|
|
|
// Also, find a link under the mouse, to try to avoid confusing new tabs
|
|
|
|
// as nuisance popups.
|
|
|
|
// Ref.: https://developer.mozilla.org/en-US/docs/Web/Events/contextmenu
|
2016-03-06 08:51:06 -07:00
|
|
|
|
2016-08-13 14:42:58 -06:00
|
|
|
var onMouseClick = function(ev) {
|
|
|
|
var elem = ev.target;
|
|
|
|
while ( elem !== null && elem.localName !== 'a' ) {
|
|
|
|
elem = elem.parentElement;
|
|
|
|
}
|
|
|
|
vAPI.messaging.send(
|
|
|
|
'contentscript',
|
|
|
|
{
|
|
|
|
what: 'mouseClick',
|
|
|
|
x: ev.clientX,
|
|
|
|
y: ev.clientY,
|
|
|
|
url: elem !== null ? elem.href : ''
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2016-08-12 06:55:35 -06:00
|
|
|
(function() {
|
|
|
|
if ( window !== window.top || !vAPI.domFilterer ) {
|
|
|
|
return;
|
2015-10-13 12:04:48 -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);
|
|
|
|
});
|
|
|
|
})();
|
2016-06-27 17:09:04 -06:00
|
|
|
};
|
2015-01-01 19:14:53 -07:00
|
|
|
|
|
|
|
/******************************************************************************/
|
2016-06-27 17:09:04 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|