2014-06-24 00:42:43 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
|
|
|
Copyright (C) 2014-2016 Raymond Hill
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
2016-10-09 17:09:25 +02:00
|
|
|
/* global createSet */
|
|
|
|
|
2016-06-28 01:09:04 +02:00
|
|
|
'use strict';
|
|
|
|
|
2016-08-12 14:55:35 +02: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-24 00:42:43 +02:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
*/
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2016-06-28 01:09:04 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Abort execution by throwing if an unexpected condition arise.
|
|
|
|
// - https://github.com/chrisaljoudi/uBlock/issues/456
|
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
if ( typeof vAPI !== 'object' ) {
|
2016-07-10 01:21:46 +02:00
|
|
|
throw new Error('uBlock Origin: aborting content scripts for ' + window.location);
|
2016-06-28 01:09:04 +02:00
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
vAPI.lock();
|
2016-06-28 01:09:04 +02:00
|
|
|
|
2016-06-29 04:01:15 +02: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';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 'matches';
|
|
|
|
})();
|
|
|
|
|
2016-06-28 01:09:04 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-11-12 19:38:41 +01:00
|
|
|
// https://github.com/gorhill/uBlock/issues/2147
|
|
|
|
|
|
|
|
vAPI.SafeAnimationFrame = function(callback) {
|
|
|
|
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 15:06:14 +02:00
|
|
|
// The DOM filterer is the heart of uBO's cosmetic filtering.
|
|
|
|
|
2016-07-10 01:21:46 +02:00
|
|
|
vAPI.domFilterer = (function() {
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var shadowId = document.documentElement.shadowRoot !== undefined ?
|
|
|
|
vAPI.randomToken():
|
|
|
|
undefined;
|
|
|
|
|
|
|
|
var jobQueue = [
|
|
|
|
{ t: 'css-hide', _0: [] }, // to inject in style tag
|
|
|
|
{ t: 'css-style', _0: [] }, // to inject in style tag
|
|
|
|
{ t: 'css-ssel', _0: [] }, // to manually hide (incremental)
|
|
|
|
{ t: 'css-csel', _0: [] } // to manually hide (not incremental)
|
|
|
|
];
|
|
|
|
|
2016-08-03 15:20:55 +02:00
|
|
|
var reParserEx = /:(?:matches-css|has|style|xpath)\(.+?\)$/;
|
2016-07-10 01:21:46 +02:00
|
|
|
|
2016-10-09 17:09:25 +02:00
|
|
|
var allExceptions = createSet(),
|
|
|
|
allSelectors = createSet(),
|
2016-08-12 14:55:35 +02:00
|
|
|
stagedNodes = [],
|
2016-08-31 23:50:49 +02:00
|
|
|
matchesProp = vAPI.matchesProp,
|
|
|
|
userCSS = vAPI.userCSS;
|
2016-07-10 01:21:46 +02:00
|
|
|
|
|
|
|
// Complex selectors, due to their nature may need to be "de-committed". A
|
2016-07-10 03:40:07 +02:00
|
|
|
// Set() is used to implement this functionality.
|
2016-07-10 01:21:46 +02:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
var complexSelectorsOldResultSet,
|
2016-10-09 17:09:25 +02:00
|
|
|
complexSelectorsCurrentResultSet = createSet('object');
|
2016-07-10 01:21:46 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var cosmeticFiltersActivatedTimer = null;
|
|
|
|
|
|
|
|
var cosmeticFiltersActivated = function() {
|
|
|
|
cosmeticFiltersActivatedTimer = null;
|
|
|
|
vAPI.messaging.send(
|
|
|
|
'contentscript',
|
|
|
|
{ what: 'cosmeticFiltersActivated' }
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-08-06 18:09:18 +02:00
|
|
|
var runSimpleSelectorJob = function(job, root, fn) {
|
|
|
|
if ( job._1 === undefined ) {
|
|
|
|
job._1 = job._0.join(cssNotHiddenId + ',');
|
|
|
|
}
|
|
|
|
if ( root[matchesProp](job._1) ) {
|
|
|
|
fn(root);
|
|
|
|
}
|
|
|
|
var nodes = root.querySelectorAll(job._1),
|
|
|
|
i = nodes.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
fn(nodes[i], job);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var runComplexSelectorJob = function(job, fn) {
|
|
|
|
if ( job._1 === undefined ) {
|
|
|
|
job._1 = job._0.join(',');
|
|
|
|
}
|
|
|
|
var nodes = document.querySelectorAll(job._1),
|
|
|
|
i = nodes.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
fn(nodes[i], job);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var runHasJob = function(job, fn) {
|
|
|
|
var nodes = document.querySelectorAll(job._0),
|
|
|
|
i = nodes.length, node;
|
|
|
|
while ( i-- ) {
|
|
|
|
node = nodes[i];
|
|
|
|
if ( node.querySelector(job._1) !== null ) {
|
|
|
|
fn(node, job);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var csspropDictFromString = function(s) {
|
|
|
|
var aa = s.split(/;\s+|;$/),
|
|
|
|
i = aa.length,
|
|
|
|
dict = Object.create(null),
|
|
|
|
prop, pos;
|
|
|
|
while ( i-- ) {
|
|
|
|
prop = aa[i].trim();
|
|
|
|
if ( prop === '' ) { continue; }
|
|
|
|
pos = prop.indexOf(':');
|
|
|
|
if ( pos === -1 ) { continue; }
|
|
|
|
dict[prop.slice(0, pos).trim()] = prop.slice(pos + 1).trim();
|
|
|
|
}
|
|
|
|
return dict;
|
|
|
|
};
|
|
|
|
|
|
|
|
var runMatchesCSSJob = function(job, fn) {
|
2016-08-19 14:46:39 +02:00
|
|
|
if ( job._2 === undefined ) {
|
|
|
|
if ( job._0.indexOf(':after', job._0.length - 6) !== -1 ) {
|
|
|
|
job._0 = job._0.slice(0, -6);
|
|
|
|
job._2 = ':after';
|
|
|
|
} else {
|
|
|
|
job._2 = null;
|
|
|
|
}
|
|
|
|
}
|
2016-08-06 18:09:18 +02:00
|
|
|
var nodes = document.querySelectorAll(job._0),
|
|
|
|
i = nodes.length;
|
|
|
|
if ( i === 0 ) { return; }
|
|
|
|
if ( typeof job._1 === 'string' ) {
|
|
|
|
job._1 = csspropDictFromString(job._1);
|
|
|
|
}
|
|
|
|
var node, match, style;
|
|
|
|
while ( i-- ) {
|
|
|
|
node = nodes[i];
|
2016-08-19 14:46:39 +02:00
|
|
|
style = window.getComputedStyle(node, job._2);
|
2016-08-06 18:09:18 +02:00
|
|
|
match = undefined;
|
|
|
|
for ( var prop in job._1 ) {
|
|
|
|
match = style[prop] === job._1[prop];
|
|
|
|
if ( match === false ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( match === true ) {
|
|
|
|
fn(node, job);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var runXpathJob = function(job, fn) {
|
|
|
|
if ( job._1 === undefined ) {
|
|
|
|
job._1 = document.createExpression(job._0, null);
|
|
|
|
}
|
|
|
|
var xpr = job._2 = job._1.evaluate(
|
|
|
|
document,
|
|
|
|
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
|
|
|
|
job._2 || null
|
|
|
|
);
|
|
|
|
var i = xpr.snapshotLength, node;
|
|
|
|
while ( i-- ) {
|
|
|
|
node = xpr.snapshotItem(i);
|
|
|
|
if ( node.nodeType === 1 ) {
|
|
|
|
fn(node, job);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-07-10 01:21:46 +02:00
|
|
|
var domFilterer = {
|
2016-08-13 22:42:58 +02:00
|
|
|
addedNodesHandlerMissCount: 0,
|
|
|
|
removedNodesHandlerMissCount: 0,
|
2016-11-12 19:38:41 +01:00
|
|
|
commitTimer: null,
|
2016-07-10 01:21:46 +02:00
|
|
|
disabledId: vAPI.randomToken(),
|
2016-06-28 01:09:04 +02:00
|
|
|
enabled: true,
|
2016-10-08 21:14:24 +02:00
|
|
|
excludeId: undefined,
|
2016-07-10 01:21:46 +02:00
|
|
|
hiddenId: vAPI.randomToken(),
|
2016-06-28 15:06:14 +02:00
|
|
|
hiddenNodeCount: 0,
|
2016-09-24 20:42:31 +02:00
|
|
|
hiddenNodeEnforcer: false,
|
2016-08-12 14:55:35 +02:00
|
|
|
loggerEnabled: undefined,
|
2016-06-28 01:09:04 +02:00
|
|
|
styleTags: [],
|
|
|
|
|
2016-07-10 01:21:46 +02:00
|
|
|
jobQueue: jobQueue,
|
|
|
|
// Stock jobs.
|
|
|
|
job0: jobQueue[0],
|
|
|
|
job1: jobQueue[1],
|
|
|
|
job2: jobQueue[2],
|
|
|
|
job3: jobQueue[3],
|
2016-06-28 01:09:04 +02:00
|
|
|
|
|
|
|
addExceptions: function(aa) {
|
|
|
|
for ( var i = 0, n = aa.length; i < n; i++ ) {
|
2016-10-09 17:09:25 +02:00
|
|
|
allExceptions.add(aa[i]);
|
2016-06-28 01:09:04 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-07-10 01:21:46 +02:00
|
|
|
// Job:
|
|
|
|
// Stock jobs in job queue:
|
|
|
|
// 0 = css rules/css declaration to remove visibility
|
|
|
|
// 1 = css rules/any css declaration
|
|
|
|
// 2 = simple css selectors/hide
|
|
|
|
// 3 = complex css selectors/hide
|
|
|
|
// Custom jobs:
|
2016-08-03 15:20:55 +02:00
|
|
|
// matches-css/hide
|
2016-07-10 01:21:46 +02:00
|
|
|
// has/hide
|
|
|
|
// xpath/hide
|
|
|
|
|
2016-06-28 01:09:04 +02:00
|
|
|
addSelector: function(s) {
|
2016-10-09 17:09:25 +02:00
|
|
|
if ( allSelectors.has(s) || allExceptions.has(s) ) {
|
2016-08-07 05:29:58 +02:00
|
|
|
return;
|
2016-06-28 01:09:04 +02:00
|
|
|
}
|
2016-10-09 17:09:25 +02:00
|
|
|
allSelectors.add(s);
|
2016-08-03 14:06:51 +02:00
|
|
|
var sel0 = s, sel1 = '';
|
|
|
|
if ( s.charCodeAt(s.length - 1) === 0x29 ) {
|
|
|
|
var parts = reParserEx.exec(s);
|
|
|
|
if ( parts !== null ) {
|
|
|
|
sel1 = parts[0];
|
2016-07-10 01:21:46 +02:00
|
|
|
}
|
2016-08-03 14:06:51 +02:00
|
|
|
}
|
|
|
|
if ( sel1 === '' ) {
|
|
|
|
this.job0._0.push(sel0);
|
|
|
|
if ( sel0.indexOf(' ') === -1 ) {
|
|
|
|
this.job2._0.push(sel0);
|
|
|
|
this.job2._1 = undefined;
|
|
|
|
} else {
|
|
|
|
this.job3._0.push(sel0);
|
|
|
|
this.job3._1 = undefined;
|
2016-07-10 01:21:46 +02:00
|
|
|
}
|
2016-08-07 05:29:58 +02:00
|
|
|
return;
|
2016-06-28 01:09:04 +02:00
|
|
|
}
|
2016-08-03 14:06:51 +02:00
|
|
|
sel0 = sel0.slice(0, sel0.length - sel1.length);
|
2016-08-03 15:20:55 +02:00
|
|
|
if ( sel1.lastIndexOf(':has', 0) === 0 ) {
|
2016-08-03 14:06:51 +02:00
|
|
|
this.jobQueue.push({ t: 'has-hide', raw: s, _0: sel0, _1: sel1.slice(5, -1) });
|
2016-08-03 15:20:55 +02:00
|
|
|
} else if ( sel1.lastIndexOf(':matches-css', 0) === 0 ) {
|
|
|
|
this.jobQueue.push({ t: 'matches-css-hide', raw: s, _0: sel0, _1: sel1.slice(13, -1) });
|
2016-08-19 14:46:39 +02:00
|
|
|
} else if ( sel1.lastIndexOf(':style', 0) === 0 ) {
|
2016-08-03 14:06:51 +02:00
|
|
|
this.job1._0.push(sel0 + ' { ' + sel1.slice(7, -1) + ' }');
|
2016-07-10 01:21:46 +02:00
|
|
|
this.job1._1 = undefined;
|
2016-08-19 14:46:39 +02:00
|
|
|
} else if ( sel1.lastIndexOf(':xpath', 0) === 0 ) {
|
2016-08-03 14:06:51 +02:00
|
|
|
this.jobQueue.push({ t: 'xpath-hide', raw: s, _0: sel1.slice(7, -1) });
|
2016-06-29 23:07:33 +02:00
|
|
|
}
|
2016-08-07 05:29:58 +02:00
|
|
|
return;
|
2016-06-29 23:07:33 +02:00
|
|
|
},
|
|
|
|
|
2016-06-28 01:09:04 +02:00
|
|
|
addSelectors: function(aa) {
|
|
|
|
for ( var i = 0, n = aa.length; i < n; i++ ) {
|
|
|
|
this.addSelector(aa[i]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-09-22 18:18:01 +02:00
|
|
|
addStyleTag: function(text) {
|
|
|
|
var styleTag = document.createElement('style');
|
|
|
|
styleTag.setAttribute('type', 'text/css');
|
|
|
|
styleTag.textContent = text;
|
|
|
|
if ( document.head ) {
|
|
|
|
document.head.appendChild(styleTag);
|
|
|
|
}
|
|
|
|
this.styleTags.push(styleTag);
|
|
|
|
if ( userCSS ) {
|
|
|
|
userCSS.add(text);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-08-14 14:51:52 +02:00
|
|
|
checkStyleTags_: function() {
|
2016-06-28 01:09:04 +02:00
|
|
|
var doc = document,
|
|
|
|
html = doc.documentElement,
|
|
|
|
head = doc.head,
|
|
|
|
newParent = head || html;
|
2016-08-14 14:51:52 +02:00
|
|
|
if ( newParent === null ) { return; }
|
|
|
|
this.removedNodesHandlerMissCount += 1;
|
2016-06-28 01:09:04 +02:00
|
|
|
var styles = this.styleTags,
|
2016-08-14 14:51:52 +02:00
|
|
|
style, oldParent;
|
2016-06-28 01:09:04 +02:00
|
|
|
for ( var i = 0; i < styles.length; i++ ) {
|
|
|
|
style = styles[i];
|
|
|
|
oldParent = style.parentNode;
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1031
|
|
|
|
// If our style tag was disabled, re-insert into the page.
|
|
|
|
if (
|
|
|
|
style.disabled &&
|
|
|
|
oldParent !== null &&
|
|
|
|
style.hasAttribute(this.disabledId) === false
|
|
|
|
) {
|
|
|
|
oldParent.removeChild(style);
|
|
|
|
oldParent = null;
|
|
|
|
}
|
2016-08-14 14:51:52 +02:00
|
|
|
if ( oldParent === head || oldParent === html ) { continue; }
|
2016-06-28 01:09:04 +02:00
|
|
|
style.disabled = false;
|
|
|
|
newParent.appendChild(style);
|
2016-08-14 14:51:52 +02:00
|
|
|
this.removedNodesHandlerMissCount = 0;
|
2016-06-28 01:09:04 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-08-14 14:51:52 +02:00
|
|
|
checkStyleTags: function() {
|
|
|
|
if ( this.removedNodesHandlerMissCount < 16 ) {
|
|
|
|
this.checkStyleTags_();
|
2016-08-12 14:55:35 +02:00
|
|
|
}
|
2016-08-14 14:51:52 +02:00
|
|
|
},
|
2016-08-12 14:55:35 +02:00
|
|
|
|
2016-08-14 14:51:52 +02:00
|
|
|
commit_: function() {
|
2016-11-12 19:38:41 +01:00
|
|
|
this.commitTimer.clear();
|
2016-08-14 14:51:52 +02:00
|
|
|
|
2016-07-12 19:29:30 +02:00
|
|
|
var beforeHiddenNodeCount = this.hiddenNodeCount,
|
|
|
|
styleText = '', i, n;
|
2016-07-10 01:21:46 +02:00
|
|
|
|
|
|
|
// Stock job 0 = css rules/hide
|
|
|
|
if ( this.job0._0.length ) {
|
|
|
|
styleText = '\n:root ' + this.job0._0.join(',\n:root ') + '\n{ display: none !important; }';
|
|
|
|
this.job0._0.length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stock job 1 = css rules/any css declaration
|
|
|
|
if ( this.job1._0.length ) {
|
2016-07-17 00:15:42 +02:00
|
|
|
styleText += '\n' + this.job1._0.join('\n');
|
2016-07-10 01:21:46 +02:00
|
|
|
this.job1._0.length = 0;
|
2016-06-28 01:09:04 +02:00
|
|
|
}
|
2016-07-10 01:21:46 +02:00
|
|
|
|
|
|
|
// Simple selectors: incremental.
|
2016-06-29 01:45:11 +02:00
|
|
|
|
2016-07-10 01:21:46 +02:00
|
|
|
// Stock job 2 = simple css selectors/hide
|
|
|
|
if ( this.job2._0.length ) {
|
|
|
|
i = stagedNodes.length;
|
|
|
|
while ( i-- ) {
|
2016-08-06 18:09:18 +02:00
|
|
|
runSimpleSelectorJob(this.job2, stagedNodes[i], hideNode);
|
2016-07-10 01:21:46 +02:00
|
|
|
}
|
2016-06-29 01:45:11 +02:00
|
|
|
}
|
2016-07-12 19:29:30 +02:00
|
|
|
stagedNodes = [];
|
2016-06-29 01:45:11 +02:00
|
|
|
|
2016-07-10 01:21:46 +02:00
|
|
|
// Complex selectors: non-incremental.
|
|
|
|
complexSelectorsOldResultSet = complexSelectorsCurrentResultSet;
|
2016-10-09 17:09:25 +02:00
|
|
|
complexSelectorsCurrentResultSet = createSet('object');
|
2016-06-29 01:45:11 +02:00
|
|
|
|
2016-07-10 01:21:46 +02:00
|
|
|
// Stock job 3 = complex css selectors/hide
|
|
|
|
// The handling of these can be considered optional, since they are
|
|
|
|
// also applied declaratively using a style tag.
|
|
|
|
if ( this.job3._0.length ) {
|
2016-08-06 18:09:18 +02:00
|
|
|
runComplexSelectorJob(this.job3, complexHideNode);
|
2016-07-10 01:21:46 +02:00
|
|
|
}
|
2016-06-29 01:45:11 +02:00
|
|
|
|
2016-07-10 01:21:46 +02:00
|
|
|
// Custom jobs. No optional since they can't be applied in a
|
|
|
|
// declarative way.
|
|
|
|
for ( i = 4, n = this.jobQueue.length; i < n; i++ ) {
|
|
|
|
this.runJob(this.jobQueue[i], complexHideNode);
|
2016-06-29 01:45:11 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 18:18:01 +02: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 19:29:30 +02:00
|
|
|
var commitHit = this.hiddenNodeCount !== beforeHiddenNodeCount;
|
|
|
|
if ( commitHit ) {
|
2016-09-24 20:42:31 +02:00
|
|
|
if ( this.hiddenNodeEnforcer === false ) {
|
|
|
|
styleText += '\n:root *[' + this.hiddenId + '][hidden] { display: none !important; }';
|
|
|
|
this.hiddenNodeEnforcer = true;
|
2016-09-22 18:18:01 +02:00
|
|
|
}
|
2016-08-13 22:42:58 +02:00
|
|
|
this.addedNodesHandlerMissCount = 0;
|
2016-07-12 19:29:30 +02:00
|
|
|
} else {
|
2016-08-13 22:42:58 +02:00
|
|
|
this.addedNodesHandlerMissCount += 1;
|
2016-07-12 19:29:30 +02:00
|
|
|
}
|
|
|
|
|
2016-09-24 20:42:31 +02:00
|
|
|
if ( styleText !== '' ) {
|
|
|
|
this.addStyleTag(styleText);
|
|
|
|
}
|
|
|
|
|
2016-07-10 01:21:46 +02:00
|
|
|
// Un-hide nodes previously hidden.
|
|
|
|
i = complexSelectorsOldResultSet.size;
|
|
|
|
if ( i !== 0 ) {
|
|
|
|
var iter = complexSelectorsOldResultSet.values();
|
|
|
|
while ( i-- ) {
|
|
|
|
this.unhideNode(iter.next().value);
|
|
|
|
}
|
|
|
|
complexSelectorsOldResultSet.clear();
|
2016-06-29 01:45:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If DOM nodes have been affected, lazily notify core process.
|
2016-08-12 14:55:35 +02:00
|
|
|
if (
|
|
|
|
this.loggerEnabled !== false &&
|
|
|
|
commitHit &&
|
|
|
|
cosmeticFiltersActivatedTimer === null
|
|
|
|
) {
|
2016-07-10 01:21:46 +02:00
|
|
|
cosmeticFiltersActivatedTimer = vAPI.setTimeout(
|
|
|
|
cosmeticFiltersActivated,
|
2016-06-29 01:45:11 +02:00
|
|
|
503
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-07-26 14:21:03 +02:00
|
|
|
commit: function(nodes, commitNow) {
|
2016-08-13 22:42:58 +02:00
|
|
|
if ( nodes === 'all' ) {
|
2016-07-12 19:29:30 +02:00
|
|
|
stagedNodes = [ document.documentElement ];
|
|
|
|
} else if ( stagedNodes[0] !== document.documentElement ) {
|
|
|
|
stagedNodes = stagedNodes.concat(nodes);
|
|
|
|
}
|
2016-07-26 14:21:03 +02:00
|
|
|
if ( commitNow ) {
|
2016-11-12 19:38:41 +01:00
|
|
|
this.commitTimer.clear();
|
2016-07-26 14:21:03 +02:00
|
|
|
this.commit_();
|
2016-08-14 14:51:52 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-11-12 19:38:41 +01:00
|
|
|
this.commitTimer.start();
|
2016-07-12 19:29:30 +02:00
|
|
|
},
|
|
|
|
|
2016-10-08 21:14:24 +02:00
|
|
|
getExcludeId: function() {
|
|
|
|
if ( this.excludeId === undefined ) {
|
|
|
|
this.excludeId = vAPI.randomToken();
|
2016-07-10 01:21:46 +02:00
|
|
|
}
|
2016-10-08 21:14:24 +02:00
|
|
|
return this.excludeId;
|
|
|
|
},
|
|
|
|
|
|
|
|
hideNode: function(node) {
|
|
|
|
if ( node[this.hiddenId] !== undefined ) { return; }
|
|
|
|
if ( this.excludeId !== undefined && node[this.excludeId] ) { return; }
|
2016-07-10 01:21:46 +02:00
|
|
|
node.setAttribute(this.hiddenId, '');
|
|
|
|
this.hiddenNodeCount += 1;
|
|
|
|
node.hidden = true;
|
2016-07-12 19:29:30 +02:00
|
|
|
node[this.hiddenId] = null;
|
|
|
|
var style = window.getComputedStyle(node),
|
|
|
|
display = style.getPropertyValue('display');
|
|
|
|
if ( display !== '' && display !== 'none' ) {
|
|
|
|
var styleAttr = node.getAttribute('style') || '';
|
|
|
|
node[this.hiddenId] = node.hasAttribute('style') && styleAttr;
|
2016-08-03 14:06:51 +02:00
|
|
|
if ( styleAttr !== '' ) { styleAttr += '; '; }
|
2016-07-12 19:29:30 +02:00
|
|
|
node.setAttribute('style', styleAttr + 'display: none !important;');
|
|
|
|
}
|
2016-10-08 21:14:24 +02:00
|
|
|
if ( shadowId === undefined ) { return; }
|
2016-07-10 01:21:46 +02:00
|
|
|
var shadow = node.shadowRoot;
|
|
|
|
if ( shadow ) {
|
|
|
|
if ( shadow[shadowId] && shadow.firstElementChild !== null ) {
|
|
|
|
shadow.removeChild(shadow.firstElementChild);
|
2016-06-29 01:45:11 +02:00
|
|
|
}
|
2016-07-10 01:21:46 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// https://github.com/gorhill/uBlock/pull/555
|
|
|
|
// Not all nodes can be shadowed:
|
|
|
|
// https://github.com/w3c/webcomponents/issues/102
|
|
|
|
try {
|
|
|
|
shadow = node.createShadowRoot();
|
|
|
|
shadow[shadowId] = true;
|
|
|
|
} catch (ex) {
|
2016-06-29 01:45:11 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-11-12 19:38:41 +01:00
|
|
|
init: function() {
|
|
|
|
this.commitTimer = new vAPI.SafeAnimationFrame(this.commit_.bind(this));
|
|
|
|
},
|
|
|
|
|
2016-07-10 01:21:46 +02:00
|
|
|
runJob: function(job, fn) {
|
|
|
|
switch ( job.t ) {
|
|
|
|
case 'has-hide':
|
2016-08-06 18:09:18 +02:00
|
|
|
runHasJob(job, fn);
|
2016-07-10 01:21:46 +02:00
|
|
|
break;
|
2016-08-03 15:20:55 +02:00
|
|
|
case 'matches-css-hide':
|
2016-08-06 18:09:18 +02:00
|
|
|
runMatchesCSSJob(job, fn);
|
2016-08-03 15:20:55 +02:00
|
|
|
break;
|
2016-07-10 01:21:46 +02:00
|
|
|
case 'xpath-hide':
|
2016-08-06 18:09:18 +02:00
|
|
|
runXpathJob(job, fn);
|
2016-07-10 01:21:46 +02:00
|
|
|
break;
|
2016-06-28 01:09:04 +02:00
|
|
|
}
|
2016-07-10 01:21:46 +02:00
|
|
|
},
|
2016-06-28 01:09:04 +02:00
|
|
|
|
2016-07-01 14:09:48 +02:00
|
|
|
showNode: function(node) {
|
2016-07-10 01:21:46 +02:00
|
|
|
node.hidden = false;
|
2016-07-12 19:29:30 +02:00
|
|
|
var styleAttr = node[this.hiddenId];
|
|
|
|
if ( styleAttr === false ) {
|
|
|
|
node.removeAttribute('style');
|
|
|
|
} else if ( typeof styleAttr === 'string' ) {
|
|
|
|
node.setAttribute('style', node[this.hiddenId]);
|
|
|
|
}
|
2016-07-01 14:09:48 +02:00
|
|
|
var shadow = node.shadowRoot;
|
2016-07-10 01:21:46 +02:00
|
|
|
if ( shadow && shadow[shadowId] ) {
|
2016-07-01 14:09:48 +02:00
|
|
|
if ( shadow.firstElementChild !== null ) {
|
|
|
|
shadow.removeChild(shadow.firstElementChild);
|
|
|
|
}
|
|
|
|
shadow.appendChild(document.createElement('content'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
toggleLogging: function(state) {
|
|
|
|
this.loggerEnabled = state;
|
|
|
|
},
|
|
|
|
|
2016-06-28 01:09:04 +02:00
|
|
|
toggleOff: function() {
|
2016-08-31 23:50:49 +02:00
|
|
|
if ( userCSS ) {
|
|
|
|
userCSS.toggle(false);
|
|
|
|
}
|
2016-06-28 01:09:04 +02:00
|
|
|
this.enabled = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleOn: function() {
|
2016-08-31 23:50:49 +02:00
|
|
|
if ( userCSS ) {
|
|
|
|
userCSS.toggle(true);
|
|
|
|
}
|
2016-06-28 01:09:04 +02:00
|
|
|
this.enabled = true;
|
2016-06-29 01:45:11 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
unhideNode: function(node) {
|
2016-07-12 19:29:30 +02:00
|
|
|
if ( node[this.hiddenId] !== undefined ) {
|
2016-07-10 01:21:46 +02:00
|
|
|
this.hiddenNodeCount--;
|
|
|
|
}
|
2016-06-29 01:45:11 +02:00
|
|
|
node.removeAttribute(this.hiddenId);
|
2016-07-10 01:21:46 +02:00
|
|
|
node[this.hiddenId] = undefined;
|
|
|
|
node.hidden = false;
|
2016-06-29 01:45:11 +02:00
|
|
|
var shadow = node.shadowRoot;
|
2016-07-10 01:21:46 +02:00
|
|
|
if ( shadow && shadow[shadowId] ) {
|
2016-06-29 01:45:11 +02:00
|
|
|
if ( shadow.firstElementChild !== null ) {
|
|
|
|
shadow.removeChild(shadow.firstElementChild);
|
|
|
|
}
|
|
|
|
shadow.appendChild(document.createElement('content'));
|
|
|
|
}
|
2016-07-01 14:09:48 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
unshowNode: function(node) {
|
2016-07-10 01:21:46 +02:00
|
|
|
node.hidden = true;
|
2016-07-12 19:29:30 +02:00
|
|
|
var styleAttr = node[this.hiddenId];
|
|
|
|
if ( styleAttr === false ) {
|
|
|
|
node.setAttribute('style', 'display: none !important;');
|
|
|
|
} else if ( typeof styleAttr === 'string' ) {
|
|
|
|
node.setAttribute('style', node[this.hiddenId] + '; display: none !important;');
|
|
|
|
}
|
2016-07-01 14:09:48 +02:00
|
|
|
var shadow = node.shadowRoot;
|
2016-07-10 01:21:46 +02:00
|
|
|
if ( shadow && shadow[shadowId] && shadow.firstElementChild !== null ) {
|
2016-07-01 14:09:48 +02:00
|
|
|
shadow.removeChild(shadow.firstElementChild);
|
|
|
|
}
|
2016-08-13 22:42:58 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
domChangedHandler: function(addedNodes, removedNodes) {
|
2016-08-14 14:51:52 +02:00
|
|
|
this.commit(addedNodes);
|
2016-08-13 22:42:58 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/873
|
|
|
|
// This will ensure our style elements will stay in the DOM.
|
2016-08-14 14:51:52 +02:00
|
|
|
if ( removedNodes ) {
|
|
|
|
domFilterer.checkStyleTags();
|
2016-08-13 22:42:58 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
start: function() {
|
|
|
|
var domChangedHandler = this.domChangedHandler.bind(this);
|
|
|
|
vAPI.domWatcher.addListener(domChangedHandler);
|
|
|
|
vAPI.shutdown.add(function() {
|
|
|
|
vAPI.domWatcher.removeListener(domChangedHandler);
|
|
|
|
});
|
2016-07-10 01:21:46 +02:00
|
|
|
}
|
2016-06-28 01:09:04 +02:00
|
|
|
};
|
|
|
|
|
2016-07-10 01:21:46 +02:00
|
|
|
/******************************************************************************/
|
2016-06-28 01:09:04 +02:00
|
|
|
|
2016-07-10 01:21:46 +02:00
|
|
|
var hideNode = domFilterer.hideNode.bind(domFilterer);
|
2016-06-29 01:45:11 +02:00
|
|
|
|
2016-07-10 01:21:46 +02:00
|
|
|
var complexHideNode = function(node) {
|
|
|
|
complexSelectorsCurrentResultSet.add(node);
|
|
|
|
if ( !complexSelectorsOldResultSet.delete(node) ) {
|
|
|
|
hideNode(node);
|
|
|
|
}
|
|
|
|
};
|
2015-01-02 03:14:53 +01:00
|
|
|
|
2016-07-10 01:21:46 +02:00
|
|
|
var cssNotHiddenId = ':not([' + domFilterer.hiddenId + '])';
|
2015-01-02 03:14:53 +01:00
|
|
|
|
2016-11-12 19:38:41 +01:00
|
|
|
domFilterer.init();
|
|
|
|
|
2014-10-19 13:11:27 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-07-10 01:21:46 +02:00
|
|
|
return domFilterer;
|
2015-01-13 21:52:15 +01:00
|
|
|
|
2016-06-28 01:09:04 +02:00
|
|
|
/******************************************************************************/
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2016-07-10 01:21:46 +02:00
|
|
|
})();
|
2016-06-28 01:09:04 +02:00
|
|
|
|
2016-07-10 01:21:46 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
2016-06-28 01:09:04 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-07-10 01:21:46 +02: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-28 01:09:04 +02:00
|
|
|
|
2016-07-10 01:21:46 +02:00
|
|
|
(function domIsLoading() {
|
2016-06-28 01:09:04 +02:00
|
|
|
|
2016-08-12 14:55:35 +02: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-10 01:21:46 +02:00
|
|
|
return;
|
2016-06-28 01:09:04 +02:00
|
|
|
}
|
|
|
|
|
2016-08-12 14:55:35 +02: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 22:42:58 +02:00
|
|
|
domFilterer.commit('all', true);
|
2016-07-10 01:21:46 +02:00
|
|
|
}
|
2016-08-12 14:55:35 +02: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-10 01:21:46 +02:00
|
|
|
}
|
2016-08-12 14:55:35 +02: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-10 01:21:46 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-02 02:58:19 +01:00
|
|
|
|
2016-07-10 01:21:46 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/587
|
|
|
|
// If no filters were found, maybe the script was injected before
|
|
|
|
// uBlock's process was fully initialized. When this happens, pages
|
|
|
|
// won't be cleaned right after browser launch.
|
2016-08-12 14:55:35 +02:00
|
|
|
if ( document.readyState !== 'loading' ) {
|
2016-11-12 19:38:41 +01:00
|
|
|
(new vAPI.SafeAnimationFrame(vAPI.domIsLoaded)).start();
|
2016-08-12 14:55:35 +02:00
|
|
|
} else {
|
|
|
|
document.addEventListener('DOMContentLoaded', vAPI.domIsLoaded);
|
|
|
|
}
|
2016-07-10 01:21:46 +02:00
|
|
|
};
|
2016-06-28 01:09:04 +02:00
|
|
|
|
2016-07-10 01:21:46 +02:00
|
|
|
var url = window.location.href;
|
|
|
|
vAPI.messaging.send(
|
|
|
|
'contentscript',
|
|
|
|
{
|
2016-08-12 14:55:35 +02:00
|
|
|
what: 'retrieveContentScriptParameters',
|
2016-07-10 01:21:46 +02:00
|
|
|
pageURL: url,
|
|
|
|
locationURL: url
|
|
|
|
},
|
|
|
|
responseHandler
|
|
|
|
);
|
2016-06-28 01:09:04 +02:00
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2015-04-08 01:10:03 +02:00
|
|
|
/******************************************************************************/
|
2014-06-24 00:42:43 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
vAPI.domWatcher = (function() {
|
|
|
|
|
|
|
|
var domLayoutObserver = null,
|
|
|
|
ignoreTags = { 'head': 1, 'link': 1, 'meta': 1, 'script': 1, 'style': 1 },
|
|
|
|
addedNodeLists = [],
|
|
|
|
addedNodes = [],
|
|
|
|
removedNodes = false,
|
|
|
|
listeners = [];
|
|
|
|
|
|
|
|
var safeObserverHandler = function() {
|
2016-11-12 19:38:41 +01:00
|
|
|
safeObserverHandlerTimer.clear();
|
2016-08-12 14:55:35 +02:00
|
|
|
var i = addedNodeLists.length,
|
|
|
|
nodeList, iNode, node;
|
|
|
|
while ( i-- ) {
|
|
|
|
nodeList = addedNodeLists[i];
|
|
|
|
iNode = nodeList.length;
|
|
|
|
while ( iNode-- ) {
|
|
|
|
node = nodeList[iNode];
|
|
|
|
if ( node.nodeType !== 1 ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( ignoreTags[node.localName] === 1 ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
addedNodes.push(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addedNodeLists.length = 0;
|
2016-08-13 22:42:58 +02:00
|
|
|
if ( addedNodes.length !== 0 || removedNodes ) {
|
2016-08-12 14:55:35 +02:00
|
|
|
listeners[0](addedNodes, removedNodes);
|
|
|
|
if ( listeners[1] ) {
|
|
|
|
listeners[1](addedNodes, removedNodes);
|
|
|
|
}
|
|
|
|
addedNodes.length = 0;
|
|
|
|
removedNodes = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-12 19:38:41 +01:00
|
|
|
var safeObserverHandlerTimer = new vAPI.SafeAnimationFrame(safeObserverHandler);
|
|
|
|
|
2016-08-12 14:55:35 +02: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 19:38:41 +01:00
|
|
|
if ( addedNodeLists.length !== 0 || removedNodes ) {
|
|
|
|
safeObserverHandlerTimer.start();
|
2016-08-12 14:55:35 +02: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 22:42:58 +02:00
|
|
|
vAPI.shutdown.add(function() {
|
|
|
|
if ( domLayoutObserver !== null ) {
|
|
|
|
domLayoutObserver.disconnect();
|
|
|
|
domLayoutObserver = null;
|
|
|
|
}
|
2016-11-12 19:38:41 +01:00
|
|
|
safeObserverHandlerTimer.clear();
|
2016-08-13 22:42:58 +02:00
|
|
|
});
|
2016-08-12 14:55:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
addListener: addListener,
|
|
|
|
removeListener: removeListener,
|
|
|
|
start: start
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
vAPI.domCollapser = (function() {
|
2015-03-29 18:13:28 +02:00
|
|
|
var timer = null;
|
2016-02-04 01:15:28 +01:00
|
|
|
var pendingRequests = Object.create(null);
|
2016-08-08 15:53:35 +02:00
|
|
|
var roundtripRequests = [];
|
2015-06-04 17:17:02 +02:00
|
|
|
var src1stProps = {
|
2015-03-29 18:13:28 +02:00
|
|
|
'embed': 'src',
|
|
|
|
'img': 'src',
|
|
|
|
'object': 'data'
|
|
|
|
};
|
2015-06-04 17:17:02 +02:00
|
|
|
var src2ndProps = {
|
|
|
|
'img': 'srcset'
|
|
|
|
};
|
2016-08-08 15:53:35 +02:00
|
|
|
var netSelectorCacheCount = 0;
|
2016-03-06 16:51:06 +01:00
|
|
|
var messaging = vAPI.messaging;
|
2015-03-29 18:13:28 +02:00
|
|
|
|
|
|
|
// Because a while ago I have observed constructors are faster than
|
|
|
|
// literal object instanciations.
|
2016-08-08 15:53:35 +02:00
|
|
|
var RoundtripRequest = function(tag, attr, url) {
|
|
|
|
this.tag = tag;
|
|
|
|
this.attr = attr;
|
2015-03-29 18:13:28 +02:00
|
|
|
this.url = url;
|
|
|
|
this.collapse = false;
|
|
|
|
};
|
|
|
|
|
2015-04-08 01:10:03 +02:00
|
|
|
var onProcessed = function(response) {
|
2016-03-21 15:33:40 +01:00
|
|
|
// This can happens if uBO is restarted.
|
|
|
|
if ( !response ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-04-08 01:10:03 +02:00
|
|
|
var requests = response.result;
|
2015-03-29 18:13:28 +02:00
|
|
|
if ( requests === null || Array.isArray(requests) === false ) {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-08 15:53:35 +02:00
|
|
|
var selectors = [],
|
|
|
|
netSelectorCacheCountMax = response.netSelectorCacheCountMax,
|
|
|
|
aa = [ null ],
|
|
|
|
request, key, entry, target, value;
|
|
|
|
// Important: process in chronological order -- this ensures the
|
|
|
|
// cached selectors are the most useful ones.
|
|
|
|
for ( var i = 0, ni = requests.length; i < ni; i++ ) {
|
2015-03-29 18:13:28 +02:00
|
|
|
request = requests[i];
|
2016-08-08 15:53:35 +02:00
|
|
|
key = request.tag + ' ' + request.attr + ' ' + request.url;
|
|
|
|
entry = pendingRequests[key];
|
2016-02-04 01:15:28 +01:00
|
|
|
if ( entry === undefined ) {
|
2015-03-29 18:13:28 +02:00
|
|
|
continue;
|
|
|
|
}
|
2016-08-08 15:53:35 +02:00
|
|
|
delete pendingRequests[key];
|
2015-04-07 03:26:05 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/869
|
2015-03-29 18:13:28 +02:00
|
|
|
if ( !request.collapse ) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-08-08 15:53:35 +02:00
|
|
|
if ( Array.isArray(entry) === false ) {
|
|
|
|
aa[0] = entry;
|
|
|
|
entry = aa;
|
|
|
|
}
|
|
|
|
for ( var j = 0, nj = entry.length; j < nj; j++ ) {
|
|
|
|
target = entry[j];
|
|
|
|
// 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(request.attr))
|
|
|
|
) {
|
|
|
|
selectors.push(request.tag + '[' + request.attr + '="' + value + '"]');
|
|
|
|
netSelectorCacheCount += 1;
|
|
|
|
}
|
2015-03-29 18:13:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( selectors.length !== 0 ) {
|
2016-03-06 16:51:06 +01:00
|
|
|
messaging.send(
|
|
|
|
'contentscript',
|
|
|
|
{
|
|
|
|
what: 'cosmeticFiltersInjected',
|
|
|
|
type: 'net',
|
|
|
|
hostname: window.location.hostname,
|
|
|
|
selectors: selectors
|
|
|
|
}
|
|
|
|
);
|
2015-03-29 18:13:28 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var send = function() {
|
|
|
|
timer = null;
|
2016-08-23 01:13:52 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/1927
|
|
|
|
// Normalize hostname to avoid trailing dot of FQHN.
|
|
|
|
var pageHostname = window.location.hostname || '';
|
|
|
|
if (
|
|
|
|
pageHostname.length &&
|
|
|
|
pageHostname.charCodeAt(pageHostname.length - 1) === 0x2e
|
|
|
|
) {
|
|
|
|
pageHostname = pageHostname.slice(0, -1);
|
|
|
|
}
|
2016-03-06 16:51:06 +01:00
|
|
|
messaging.send(
|
|
|
|
'contentscript',
|
|
|
|
{
|
|
|
|
what: 'filterRequests',
|
|
|
|
pageURL: window.location.href,
|
2016-08-23 01:13:52 +02:00
|
|
|
pageHostname: pageHostname,
|
2016-08-08 15:53:35 +02:00
|
|
|
requests: roundtripRequests
|
2016-03-06 16:51:06 +01:00
|
|
|
}, onProcessed
|
|
|
|
);
|
2016-08-09 01:41:53 +02:00
|
|
|
roundtripRequests = [];
|
2015-03-29 18:13:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
var process = function(delay) {
|
2016-08-08 15:53:35 +02:00
|
|
|
if ( roundtripRequests.length === 0 ) {
|
2015-03-29 18:13:28 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( delay === 0 ) {
|
|
|
|
clearTimeout(timer);
|
|
|
|
send();
|
|
|
|
} else if ( timer === null ) {
|
2015-05-17 19:02:56 +02:00
|
|
|
timer = vAPI.setTimeout(send, delay || 20);
|
2015-03-29 18:13:28 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// If needed eventually, we could listen to `src` attribute changes
|
|
|
|
// for iframes.
|
|
|
|
|
|
|
|
var add = function(target) {
|
2016-08-08 15:53:35 +02:00
|
|
|
var tag = target.localName;
|
|
|
|
var prop = src1stProps[tag];
|
2015-03-29 18:13:28 +02:00
|
|
|
if ( prop === undefined ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-04-07 03:26:05 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/174
|
2015-03-29 18:13:28 +02:00
|
|
|
// Do not remove fragment from src URL
|
|
|
|
var src = target[prop];
|
2015-06-04 17:17:02 +02:00
|
|
|
if ( typeof src !== 'string' || src.length === 0 ) {
|
2016-08-08 15:53:35 +02:00
|
|
|
prop = src2ndProps[tag];
|
2015-06-04 17:17:02 +02:00
|
|
|
if ( prop === undefined ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
src = target[prop];
|
|
|
|
if ( typeof src !== 'string' || src.length === 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-29 18:13:28 +02:00
|
|
|
}
|
2016-08-23 01:13:52 +02:00
|
|
|
if ( src.lastIndexOf('http', 0) !== 0 ) {
|
|
|
|
return;
|
2016-07-25 14:18:17 +02:00
|
|
|
}
|
2016-08-08 15:53:35 +02:00
|
|
|
var key = tag + ' ' + prop + ' ' + src,
|
|
|
|
entry = pendingRequests[key];
|
|
|
|
if ( entry === undefined ) {
|
|
|
|
pendingRequests[key] = target;
|
|
|
|
roundtripRequests.push(new RoundtripRequest(tag, prop, src));
|
|
|
|
} else if ( Array.isArray(entry) ) {
|
|
|
|
entry.push(target);
|
|
|
|
} else {
|
|
|
|
pendingRequests[key] = [ entry, target ];
|
|
|
|
}
|
2015-03-29 18:13:28 +02:00
|
|
|
};
|
|
|
|
|
2016-02-04 01:15:28 +01:00
|
|
|
var addMany = function(targets) {
|
|
|
|
var i = targets.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
add(targets[i]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-02 01:06:52 +02: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 15:33:54 +01:00
|
|
|
var primeLocalIFrame = function(iframe) {
|
|
|
|
// Should probably also copy injected styles.
|
2016-03-05 20:59:01 +01: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 15:33:54 +01: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-02 01:06:52 +02: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 18:13:28 +02:00
|
|
|
var src = iframe.src;
|
|
|
|
if ( src === '' || typeof src !== 'string' ) {
|
2016-01-21 15:33:54 +01:00
|
|
|
primeLocalIFrame(iframe);
|
2015-03-29 18:13:28 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( src.lastIndexOf('http', 0) !== 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-08 15:53:35 +02:00
|
|
|
var key = 'iframe' + ' ' + 'src' + ' ' + src,
|
|
|
|
entry = pendingRequests[key];
|
|
|
|
if ( entry === undefined ) {
|
|
|
|
pendingRequests[key] = iframe;
|
|
|
|
roundtripRequests.push(new RoundtripRequest('iframe', 'src', src));
|
|
|
|
} else if ( Array.isArray(entry) ) {
|
|
|
|
entry.push(iframe);
|
|
|
|
} else {
|
|
|
|
pendingRequests[key] = [ entry, iframe ];
|
|
|
|
}
|
2015-03-29 18:13:28 +02:00
|
|
|
};
|
|
|
|
|
2016-02-04 01:15:28 +01:00
|
|
|
var addIFrames = function(iframes) {
|
2016-02-04 00:47:30 +01:00
|
|
|
var i = iframes.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
addIFrame(iframes[i]);
|
|
|
|
}
|
2016-02-04 01:15:28 +01:00
|
|
|
};
|
|
|
|
|
2016-08-13 22:42:58 +02:00
|
|
|
var onResourceFailed = function(ev) {
|
|
|
|
vAPI.domCollapser.add(ev.target);
|
|
|
|
vAPI.domCollapser.process();
|
|
|
|
};
|
|
|
|
|
2016-08-12 14:55:35 +02: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);
|
|
|
|
}
|
2016-10-30 21:04:53 +01:00
|
|
|
if ( node.children && node.children.length !== 0 ) {
|
2016-08-12 14:55:35 +02:00
|
|
|
var iframes = node.getElementsByTagName('iframe');
|
|
|
|
if ( iframes.length !== 0 ) {
|
|
|
|
addIFrames(iframes);
|
|
|
|
}
|
|
|
|
}
|
2016-06-29 01:45:11 +02:00
|
|
|
}
|
2016-08-12 14:55:35 +02: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-29 01:45:11 +02:00
|
|
|
}
|
2016-02-04 01:15:28 +01:00
|
|
|
}
|
2016-08-12 14:55:35 +02: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 22:42:58 +02:00
|
|
|
vAPI.domWatcher.addListener(domChangedHandler);
|
2016-08-12 14:55:35 +02:00
|
|
|
|
|
|
|
vAPI.shutdown.add(function() {
|
|
|
|
document.removeEventListener('error', onResourceFailed, true);
|
2016-08-13 22:42:58 +02:00
|
|
|
vAPI.domWatcher.removeListener(domChangedHandler);
|
|
|
|
if ( timer !== null ) {
|
|
|
|
clearTimeout(timer);
|
2016-08-12 14:55:35 +02:00
|
|
|
}
|
|
|
|
});
|
2015-03-29 18:13:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
add: add,
|
2016-02-04 01:15:28 +01:00
|
|
|
addMany: addMany,
|
2015-03-29 18:13:28 +02:00
|
|
|
addIFrame: addIFrame,
|
2016-02-04 01:15:28 +01:00
|
|
|
addIFrames: addIFrames,
|
2016-08-12 14:55:35 +02:00
|
|
|
process: process,
|
|
|
|
start: start
|
2015-03-29 18:13:28 +02:00
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2016-06-28 01:09:04 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
vAPI.domSurveyor = (function() {
|
|
|
|
var domFilterer = null,
|
|
|
|
messaging = vAPI.messaging,
|
|
|
|
surveyPhase3Nodes = [],
|
|
|
|
cosmeticSurveyingMissCount = 0,
|
|
|
|
highGenerics = null,
|
|
|
|
lowGenericSelectors = [],
|
2016-10-09 17:09:25 +02:00
|
|
|
queriedSelectors = createSet(),
|
2016-10-06 16:49:46 +02:00
|
|
|
surveyCost = 0;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
// Handle main process' response.
|
|
|
|
|
|
|
|
var surveyPhase3 = function(response) {
|
|
|
|
var result = response && response.result,
|
|
|
|
firstSurvey = highGenerics === null;
|
2015-04-08 01:10:03 +02:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
if ( result ) {
|
|
|
|
if ( result.hide.length ) {
|
|
|
|
processLowGenerics(result.hide);
|
2016-06-28 01:09:04 +02:00
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
if ( result.highGenerics ) {
|
|
|
|
highGenerics = result.highGenerics;
|
2014-08-12 18:19:54 +02:00
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
}
|
2016-07-10 01:21:46 +02:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
if ( highGenerics ) {
|
2016-10-06 16:49:46 +02:00
|
|
|
var t0 = window.performance.now();
|
2016-08-12 14:55:35 +02:00
|
|
|
if ( highGenerics.hideLowCount ) {
|
|
|
|
processHighLowGenerics(highGenerics.hideLow);
|
2014-08-12 18:19:54 +02:00
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
if ( highGenerics.hideMediumCount ) {
|
|
|
|
processHighMediumGenerics(highGenerics.hideMedium);
|
2014-08-12 18:19:54 +02:00
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
if ( highGenerics.hideHighSimpleCount || highGenerics.hideHighComplexCount ) {
|
|
|
|
processHighHighGenerics();
|
|
|
|
}
|
2016-10-06 16:49:46 +02:00
|
|
|
surveyCost += window.performance.now() - t0;
|
2016-08-12 14:55:35 +02:00
|
|
|
}
|
2016-06-29 04:01:15 +02:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
// Need to do this before committing DOM filterer, as needed info
|
|
|
|
// will no longer be there after commit.
|
|
|
|
if ( firstSurvey || domFilterer.job0._0.length ) {
|
|
|
|
messaging.send(
|
|
|
|
'contentscript',
|
|
|
|
{
|
|
|
|
what: 'cosmeticFiltersInjected',
|
|
|
|
type: 'cosmetic',
|
|
|
|
hostname: window.location.hostname,
|
2016-10-06 16:49:46 +02:00
|
|
|
selectors: domFilterer.job0._0,
|
|
|
|
first: firstSurvey,
|
|
|
|
cost: surveyCost
|
2016-07-12 19:29:30 +02:00
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
);
|
|
|
|
}
|
2016-07-12 19:29:30 +02:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
// Shutdown surveyor if too many consecutive empty resultsets.
|
|
|
|
if ( domFilterer.job0._0.length === 0 ) {
|
|
|
|
cosmeticSurveyingMissCount += 1;
|
|
|
|
} else {
|
|
|
|
cosmeticSurveyingMissCount = 0;
|
|
|
|
}
|
2016-06-30 22:10:38 +02:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
domFilterer.commit(surveyPhase3Nodes);
|
|
|
|
surveyPhase3Nodes = [];
|
|
|
|
};
|
2015-01-01 14:09:44 +01:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
// Query main process.
|
2014-09-17 01:16:18 +02:00
|
|
|
|
2016-08-12 14:55:35 +02: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 22:47:34 +02:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
// Low generics:
|
|
|
|
// - [id]
|
|
|
|
// - [class]
|
2014-09-17 01:16:18 +02:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
var processLowGenerics = function(generics) {
|
|
|
|
domFilterer.addSelectors(generics);
|
|
|
|
};
|
2014-07-04 22:47:34 +02:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
// High-low generics:
|
|
|
|
// - [alt="..."]
|
|
|
|
// - [title="..."]
|
2014-09-17 01:16:18 +02:00
|
|
|
|
2016-08-12 14:55:35 +02: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-28 01:09:04 +02:00
|
|
|
continue;
|
2014-08-12 18:19:54 +02:00
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
// Candidate 2 = specific form
|
|
|
|
selector = node.localName + selector;
|
|
|
|
if ( generics.hasOwnProperty(selector) ) {
|
|
|
|
domFilterer.addSelector(selector);
|
2014-08-12 18:19:54 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
}
|
|
|
|
};
|
2014-07-04 22:47:34 +02:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
// High-medium generics:
|
|
|
|
// - [href^="http"]
|
2014-09-17 01:16:18 +02:00
|
|
|
|
2016-08-12 14:55:35 +02: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 23:59:25 +02:00
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
nodes = node.getElementsByTagName('a');
|
|
|
|
if ( nodes.length !== 0 ) {
|
|
|
|
processHighMediumGenericsForNodes(nodes, generics);
|
2014-07-02 18:02:29 +02:00
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
}
|
|
|
|
};
|
2014-09-17 01:16:18 +02:00
|
|
|
|
2016-08-12 14:55:35 +02: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-10 01:21:46 +02:00
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
j = entry.length;
|
|
|
|
while ( j-- ) {
|
|
|
|
selector = entry[j];
|
|
|
|
if ( href.lastIndexOf(selector.slice(8, -2), 0) === 0 ) {
|
|
|
|
domFilterer.addSelector(selector);
|
2016-06-29 04:01:15 +02:00
|
|
|
}
|
2015-03-02 02:26:33 +01:00
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
}
|
|
|
|
};
|
2015-09-04 22:30:53 +02:00
|
|
|
|
2016-08-12 14:55:35 +02: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-10 01:21:46 +02:00
|
|
|
while ( i-- ) {
|
|
|
|
node = nodes[i];
|
2016-08-12 14:55:35 +02:00
|
|
|
if (
|
|
|
|
node[matchesProp](highGenerics.hideHighSimple) ||
|
|
|
|
node.querySelector(highGenerics.hideHighSimple) !== null
|
|
|
|
) {
|
|
|
|
highHighSimpleGenericsInjected = true;
|
|
|
|
domFilterer.addSelectors(highGenerics.hideHighSimple.split(',\n'));
|
|
|
|
break;
|
2015-09-04 22:30:53 +02:00
|
|
|
}
|
2014-07-02 18:02:29 +02:00
|
|
|
}
|
2016-08-12 14:55:35 +02: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 14:05:00 +02:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
// Extract and return the staged nodes which (may) match the selectors.
|
2015-03-29 18:13:28 +02:00
|
|
|
|
2016-08-12 14:55:35 +02: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 18:19:54 +02:00
|
|
|
};
|
|
|
|
|
2016-08-12 14:55:35 +02: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 21:39:21 +02:00
|
|
|
|
2016-08-12 14:55:35 +02: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 22:10:38 +02:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
var surveyPhase1 = function(addedNodes) {
|
2016-10-06 16:49:46 +02:00
|
|
|
var t0 = window.performance.now(),
|
|
|
|
nodes = selectNodes('[class],[id]', addedNodes),
|
|
|
|
qq = queriedSelectors,
|
|
|
|
ll = lowGenericSelectors,
|
|
|
|
node, v, vv, j,
|
|
|
|
i = nodes.length;
|
2016-08-12 14:55:35 +02:00
|
|
|
while ( i-- ) {
|
|
|
|
node = nodes[i];
|
|
|
|
if ( node.nodeType !== 1 ) { continue; }
|
|
|
|
v = node.id;
|
|
|
|
if ( v !== '' && typeof v === 'string' ) {
|
|
|
|
v = '#' + v.trim();
|
2016-10-09 17:09:25 +02:00
|
|
|
if ( v !== '#' && !qq.has(v) ) { ll.push(v); qq.add(v); }
|
2014-07-30 14:05:00 +02:00
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
vv = node.className;
|
|
|
|
if ( vv === '' || typeof vv !== 'string' ) { continue; }
|
|
|
|
if ( /\s/.test(vv) === false ) {
|
|
|
|
v = '.' + vv;
|
2016-10-09 17:09:25 +02:00
|
|
|
if ( !qq.has(v) ) { ll.push(v); qq.add(v); }
|
2016-07-10 01:21:46 +02:00
|
|
|
} else {
|
2016-08-12 14:55:35 +02:00
|
|
|
vv = node.classList;
|
|
|
|
j = vv.length;
|
|
|
|
while ( j-- ) {
|
|
|
|
v = '.' + vv[j];
|
2016-10-09 17:09:25 +02:00
|
|
|
if ( !qq.has(v) ) { ll.push(v); qq.add(v); }
|
2016-07-12 19:29:30 +02:00
|
|
|
}
|
2016-07-10 01:21:46 +02:00
|
|
|
}
|
2014-07-30 14:05:00 +02:00
|
|
|
}
|
2016-10-06 16:49:46 +02:00
|
|
|
surveyCost += window.performance.now() - t0;
|
2016-08-12 14:55:35 +02:00
|
|
|
surveyPhase2(addedNodes);
|
2014-07-02 18:02:29 +02:00
|
|
|
};
|
2014-08-12 18:19:54 +02:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
var domChangedHandler = function(addedNodes, removedNodes) {
|
2016-08-13 22:42:58 +02:00
|
|
|
if ( cosmeticSurveyingMissCount > 255 ) {
|
|
|
|
vAPI.domWatcher.removeListener(domChangedHandler);
|
|
|
|
vAPI.domSurveyor = null;
|
|
|
|
domFilterer.domChangedHandler(addedNodes, removedNodes);
|
|
|
|
domFilterer.start();
|
|
|
|
return;
|
2016-06-28 15:06:14 +02:00
|
|
|
}
|
2016-08-13 22:42:58 +02:00
|
|
|
|
|
|
|
surveyPhase1(addedNodes);
|
|
|
|
if ( removedNodes ) {
|
2016-08-14 14:51:52 +02:00
|
|
|
domFilterer.checkStyleTags();
|
2015-10-31 05:55:10 +01:00
|
|
|
}
|
2014-09-16 21:39:21 +02:00
|
|
|
};
|
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
var start = function() {
|
|
|
|
domFilterer = vAPI.domFilterer;
|
|
|
|
domChangedHandler([ document.documentElement ]);
|
2016-08-13 22:42:58 +02:00
|
|
|
vAPI.domWatcher.addListener(domChangedHandler);
|
|
|
|
vAPI.shutdown.add(function() {
|
|
|
|
vAPI.domWatcher.removeListener(domChangedHandler);
|
|
|
|
});
|
2015-04-08 01:10:03 +02:00
|
|
|
};
|
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
return {
|
|
|
|
start: start
|
|
|
|
};
|
2015-04-08 01:10:03 +02:00
|
|
|
})();
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
2014-09-14 22:20:40 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
vAPI.domIsLoaded = function(ev) {
|
2016-08-14 03:45:01 +02:00
|
|
|
// This can happen on Firefox. For instance:
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1893
|
|
|
|
if ( window.location === null ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-14 14:51:52 +02:00
|
|
|
var slowLoad = ev instanceof Event;
|
|
|
|
if ( slowLoad ) {
|
2016-08-12 14:55:35 +02:00
|
|
|
document.removeEventListener('DOMContentLoaded', vAPI.domIsLoaded);
|
2015-03-29 18:13:28 +02:00
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
vAPI.domIsLoaded = null;
|
2014-09-14 22:20:40 +02:00
|
|
|
|
2016-08-13 22:42:58 +02:00
|
|
|
vAPI.domWatcher.start();
|
|
|
|
vAPI.domCollapser.start();
|
2014-09-28 20:38:17 +02:00
|
|
|
|
2016-08-13 22:42:58 +02:00
|
|
|
if ( vAPI.domFilterer ) {
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/789
|
|
|
|
// https://github.com/gorhill/uBlock/issues/873
|
2016-08-14 14:51:52 +02:00
|
|
|
// Be sure our style tags used for cosmetic filtering are still
|
|
|
|
// applied.
|
|
|
|
vAPI.domFilterer.checkStyleTags();
|
|
|
|
// 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 22:42:58 +02:00
|
|
|
if ( vAPI.domSurveyor ) {
|
|
|
|
vAPI.domSurveyor.start();
|
|
|
|
} else {
|
|
|
|
vAPI.domFilterer.start();
|
|
|
|
}
|
2015-02-16 18:17:26 +01:00
|
|
|
}
|
2016-03-06 16:51:06 +01:00
|
|
|
|
2016-08-12 14:55:35 +02: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 16:51:06 +01:00
|
|
|
|
2016-08-13 22:42:58 +02: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 14:55:35 +02:00
|
|
|
(function() {
|
|
|
|
if ( window !== window.top || !vAPI.domFilterer ) {
|
|
|
|
return;
|
2015-10-13 20:04:48 +02:00
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
document.addEventListener('mousedown', onMouseClick, true);
|
2015-01-02 03:14:53 +01:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
// https://github.com/gorhill/uMatrix/issues/144
|
|
|
|
vAPI.shutdown.add(function() {
|
|
|
|
document.removeEventListener('mousedown', onMouseClick, true);
|
|
|
|
});
|
|
|
|
})();
|
2016-06-28 01:09:04 +02:00
|
|
|
};
|
2015-01-02 03:14:53 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
2016-06-28 01:09:04 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|