2017-07-23 09:33:39 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-09-29 16:56:59 -06:00
|
|
|
Copyright (C) 2017-present Raymond Hill
|
2017-07-23 09:33:39 -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
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2018-04-26 09:36:22 -06:00
|
|
|
// Packaging this file is optional: it is not necessary to package it if the
|
|
|
|
// platform is known to not support user stylesheets.
|
2017-07-23 09:33:39 -06:00
|
|
|
|
2018-04-12 10:17:38 -06:00
|
|
|
// >>>>>>>> start of HUGE-IF-BLOCK
|
2018-05-03 07:55:36 -06:00
|
|
|
if ( typeof vAPI === 'object' && vAPI.supportsUserStylesheets ) {
|
2017-10-21 11:43:46 -06:00
|
|
|
|
|
|
|
/******************************************************************************/
|
2017-07-23 09:33:39 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-10-22 06:59:29 -06:00
|
|
|
vAPI.userStylesheet = {
|
|
|
|
added: new Set(),
|
|
|
|
removed: new Set(),
|
2017-11-21 16:08:32 -07:00
|
|
|
apply: function(callback) {
|
2017-10-22 06:59:29 -06:00
|
|
|
if ( this.added.size === 0 && this.removed.size === 0 ) { return; }
|
2017-11-20 06:42:32 -07:00
|
|
|
vAPI.messaging.send('vapi', {
|
2017-10-22 06:59:29 -06:00
|
|
|
what: 'userCSS',
|
|
|
|
add: Array.from(this.added),
|
|
|
|
remove: Array.from(this.removed)
|
2017-11-21 16:08:32 -07:00
|
|
|
}, callback);
|
2017-10-22 06:59:29 -06:00
|
|
|
this.added.clear();
|
|
|
|
this.removed.clear();
|
|
|
|
},
|
|
|
|
add: function(cssText, now) {
|
|
|
|
if ( cssText === '' ) { return; }
|
|
|
|
this.added.add(cssText);
|
|
|
|
if ( now ) { this.apply(); }
|
|
|
|
},
|
|
|
|
remove: function(cssText, now) {
|
|
|
|
if ( cssText === '' ) { return; }
|
|
|
|
this.removed.add(cssText);
|
|
|
|
if ( now ) { this.apply(); }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-05-16 11:44:49 -06:00
|
|
|
vAPI.DOMFilterer = class {
|
|
|
|
constructor() {
|
2019-05-16 15:22:20 -06:00
|
|
|
this.commitTimer = new vAPI.SafeAnimationFrame(this.commitNow.bind(this));
|
2019-05-16 11:44:49 -06:00
|
|
|
this.domIsReady = document.readyState !== 'loading';
|
|
|
|
this.disabled = false;
|
|
|
|
this.listeners = [];
|
|
|
|
this.filterset = new Set();
|
|
|
|
this.excludedNodeSet = new WeakSet();
|
|
|
|
this.addedCSSRules = new Set();
|
|
|
|
this.exceptedCSSRules = [];
|
|
|
|
this.reOnlySelectors = /\n\{[^\n]+/g;
|
|
|
|
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/167
|
|
|
|
// By the time the DOMContentLoaded is fired, the content script might
|
|
|
|
// have been disconnected from the background page. Unclear why this
|
|
|
|
// would happen, so far seems to be a Chromium-specific behavior at
|
|
|
|
// launch time.
|
|
|
|
if ( this.domIsReady !== true ) {
|
|
|
|
document.addEventListener('DOMContentLoaded', ( ) => {
|
|
|
|
if ( vAPI instanceof Object === false ) { return; }
|
|
|
|
this.domIsReady = true;
|
|
|
|
this.commit();
|
|
|
|
});
|
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
}
|
2017-10-22 06:59:29 -06:00
|
|
|
|
|
|
|
// Here we will deal with:
|
|
|
|
// - Injecting low priority user styles;
|
|
|
|
// - Notifying listeners about changed filterset.
|
2018-09-29 16:56:59 -06:00
|
|
|
// https://www.reddit.com/r/uBlockOrigin/comments/9jj0y1/no_longer_blocking_ads/
|
|
|
|
// Ensure vAPI is still valid -- it can go away by the time we are
|
|
|
|
// called, since the port could be force-disconnected from the main
|
|
|
|
// process. Another approach would be to have vAPI.SafeAnimationFrame
|
|
|
|
// register a shutdown job: to evaluate. For now I will keep the fix
|
|
|
|
// trivial.
|
2019-05-16 11:44:49 -06:00
|
|
|
commitNow() {
|
2017-10-21 11:43:46 -06:00
|
|
|
this.commitTimer.clear();
|
2018-09-29 16:56:59 -06:00
|
|
|
if ( vAPI instanceof Object === false ) { return; }
|
2019-05-16 11:44:49 -06:00
|
|
|
const userStylesheet = vAPI.userStylesheet;
|
|
|
|
for ( const entry of this.addedCSSRules ) {
|
2017-10-23 07:01:00 -06:00
|
|
|
if (
|
|
|
|
this.disabled === false &&
|
|
|
|
entry.lazy &&
|
|
|
|
entry.injected === false
|
|
|
|
) {
|
2017-10-22 06:59:29 -06:00
|
|
|
userStylesheet.add(
|
|
|
|
entry.selectors + '\n{' + entry.declarations + '}'
|
|
|
|
);
|
2017-10-21 11:43:46 -06:00
|
|
|
}
|
|
|
|
}
|
2017-10-22 06:59:29 -06:00
|
|
|
this.addedCSSRules.clear();
|
|
|
|
userStylesheet.apply();
|
2019-05-16 11:44:49 -06:00
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
|
2019-05-16 11:44:49 -06:00
|
|
|
commit(commitNow) {
|
2017-10-21 11:43:46 -06:00
|
|
|
if ( commitNow ) {
|
|
|
|
this.commitTimer.clear();
|
|
|
|
this.commitNow();
|
|
|
|
} else {
|
|
|
|
this.commitTimer.start();
|
|
|
|
}
|
2019-05-16 11:44:49 -06:00
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
|
2019-06-20 05:14:34 -06:00
|
|
|
addCSSRule(selectors, declarations, details = {}) {
|
2017-10-21 11:43:46 -06:00
|
|
|
if ( selectors === undefined ) { return; }
|
2019-05-16 11:44:49 -06:00
|
|
|
const selectorsStr = Array.isArray(selectors)
|
2017-10-21 11:43:46 -06:00
|
|
|
? selectors.join(',\n')
|
|
|
|
: selectors;
|
|
|
|
if ( selectorsStr.length === 0 ) { return; }
|
2019-05-16 11:44:49 -06:00
|
|
|
const entry = {
|
2017-10-21 11:43:46 -06:00
|
|
|
selectors: selectorsStr,
|
|
|
|
declarations,
|
2017-10-23 07:01:00 -06:00
|
|
|
lazy: details.lazy === true,
|
|
|
|
injected: details.injected === true
|
2017-10-22 06:59:29 -06:00
|
|
|
};
|
|
|
|
this.addedCSSRules.add(entry);
|
|
|
|
this.filterset.add(entry);
|
2017-10-23 07:01:00 -06:00
|
|
|
if (
|
|
|
|
this.disabled === false &&
|
|
|
|
entry.lazy !== true &&
|
|
|
|
entry.injected !== true
|
|
|
|
) {
|
2017-10-22 06:59:29 -06:00
|
|
|
vAPI.userStylesheet.add(selectorsStr + '\n{' + declarations + '}');
|
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
this.commit();
|
2019-06-20 05:14:34 -06:00
|
|
|
if ( details.silent !== true && this.hasListeners() ) {
|
2017-11-14 13:03:20 -07:00
|
|
|
this.triggerListeners({
|
|
|
|
declarative: [ [ selectorsStr, declarations ] ]
|
|
|
|
});
|
|
|
|
}
|
2019-05-16 11:44:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
exceptCSSRules(exceptions) {
|
|
|
|
if ( exceptions.length === 0 ) { return; }
|
|
|
|
this.exceptedCSSRules.push(...exceptions);
|
|
|
|
if ( this.hasListeners() ) {
|
|
|
|
this.triggerListeners({ exceptions });
|
|
|
|
}
|
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
|
2019-05-16 11:44:49 -06:00
|
|
|
addListener(listener) {
|
2017-10-21 11:43:46 -06:00
|
|
|
if ( this.listeners.indexOf(listener) !== -1 ) { return; }
|
|
|
|
this.listeners.push(listener);
|
2019-05-16 11:44:49 -06:00
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
|
2019-05-16 11:44:49 -06:00
|
|
|
removeListener(listener) {
|
|
|
|
const pos = this.listeners.indexOf(listener);
|
2017-10-21 11:43:46 -06:00
|
|
|
if ( pos === -1 ) { return; }
|
|
|
|
this.listeners.splice(pos, 1);
|
2019-05-16 11:44:49 -06:00
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
|
2019-05-16 11:44:49 -06:00
|
|
|
hasListeners() {
|
2017-11-14 13:03:20 -07:00
|
|
|
return this.listeners.length !== 0;
|
2019-05-16 11:44:49 -06:00
|
|
|
}
|
2017-11-14 13:03:20 -07:00
|
|
|
|
2019-05-16 11:44:49 -06:00
|
|
|
triggerListeners(changes) {
|
|
|
|
for ( const listener of this.listeners ) {
|
|
|
|
listener.onFiltersetChanged(changes);
|
2017-10-21 11:43:46 -06:00
|
|
|
}
|
2019-05-16 11:44:49 -06:00
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
|
2019-05-16 11:44:49 -06:00
|
|
|
excludeNode(node) {
|
2017-10-21 11:43:46 -06:00
|
|
|
this.excludedNodeSet.add(node);
|
|
|
|
this.unhideNode(node);
|
2019-05-16 11:44:49 -06:00
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
|
2019-05-16 11:44:49 -06:00
|
|
|
unexcludeNode(node) {
|
2017-10-22 06:59:29 -06:00
|
|
|
this.excludedNodeSet.delete(node);
|
2019-05-16 11:44:49 -06:00
|
|
|
}
|
2017-10-22 06:59:29 -06:00
|
|
|
|
2019-05-16 11:44:49 -06:00
|
|
|
hideNode(node) {
|
2017-10-21 11:43:46 -06:00
|
|
|
if ( this.excludedNodeSet.has(node) ) { return; }
|
2017-10-23 10:21:37 -06:00
|
|
|
if ( this.hideNodeAttr === undefined ) { return; }
|
|
|
|
node.setAttribute(this.hideNodeAttr, '');
|
2019-06-20 05:14:34 -06:00
|
|
|
if ( this.hideNodeStyleSheetInjected ) { return; }
|
|
|
|
this.hideNodeStyleSheetInjected = true;
|
|
|
|
this.addCSSRule(
|
|
|
|
`[${this.hideNodeAttr}]`,
|
|
|
|
'display:none!important;',
|
|
|
|
{ silent: true }
|
|
|
|
);
|
2019-05-16 11:44:49 -06:00
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
|
2019-05-16 11:44:49 -06:00
|
|
|
unhideNode(node) {
|
2017-10-23 10:21:37 -06:00
|
|
|
if ( this.hideNodeAttr === undefined ) { return; }
|
|
|
|
node.removeAttribute(this.hideNodeAttr);
|
2019-05-16 11:44:49 -06:00
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
|
2019-05-16 11:44:49 -06:00
|
|
|
toggle(state, callback) {
|
2017-10-22 06:59:29 -06:00
|
|
|
if ( state === undefined ) { state = this.disabled; }
|
|
|
|
if ( state !== this.disabled ) { return; }
|
|
|
|
this.disabled = !state;
|
2019-05-16 11:44:49 -06:00
|
|
|
const userStylesheet = vAPI.userStylesheet;
|
|
|
|
for ( const entry of this.filterset ) {
|
|
|
|
const rule = `${entry.selectors}\n{${entry.declarations}}`;
|
2017-10-22 06:59:29 -06:00
|
|
|
if ( this.disabled ) {
|
|
|
|
userStylesheet.remove(rule);
|
|
|
|
} else {
|
|
|
|
userStylesheet.add(rule);
|
|
|
|
}
|
|
|
|
}
|
2017-11-21 16:08:32 -07:00
|
|
|
userStylesheet.apply(callback);
|
2019-05-16 11:44:49 -06:00
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
|
2019-05-16 11:44:49 -06:00
|
|
|
getAllSelectors_(all) {
|
|
|
|
const out = {
|
|
|
|
declarative: [],
|
|
|
|
exceptions: this.exceptedCSSRules,
|
2017-11-14 13:03:20 -07:00
|
|
|
};
|
2019-05-16 11:44:49 -06:00
|
|
|
for ( const entry of this.filterset ) {
|
|
|
|
let selectors = entry.selectors;
|
2017-12-31 19:13:46 -07:00
|
|
|
if ( all !== true && this.hideNodeAttr !== undefined ) {
|
|
|
|
selectors = selectors
|
2019-05-16 11:44:49 -06:00
|
|
|
.replace(`[${this.hideNodeAttr}]`, '')
|
2018-01-08 13:03:55 -07:00
|
|
|
.replace(/^,\n|,\n$/gm, '');
|
2017-12-31 19:13:46 -07:00
|
|
|
if ( selectors === '' ) { continue; }
|
|
|
|
}
|
|
|
|
out.declarative.push([ selectors, entry.declarations ]);
|
2017-10-23 10:21:37 -06:00
|
|
|
}
|
|
|
|
return out;
|
2019-05-16 11:44:49 -06:00
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
|
2019-05-16 11:44:49 -06:00
|
|
|
getFilteredElementCount() {
|
|
|
|
const details = this.getAllSelectors_(true);
|
2017-11-14 13:03:20 -07:00
|
|
|
if ( Array.isArray(details.declarative) === false ) { return 0; }
|
2019-05-16 11:44:49 -06:00
|
|
|
const selectors = details.declarative.map(entry => entry[0]);
|
2017-11-14 13:03:20 -07:00
|
|
|
if ( selectors.length === 0 ) { return 0; }
|
|
|
|
return document.querySelectorAll(selectors.join(',\n')).length;
|
2019-05-16 11:44:49 -06:00
|
|
|
}
|
2017-10-21 11:43:46 -06:00
|
|
|
|
2019-05-16 11:44:49 -06:00
|
|
|
getAllSelectors() {
|
2017-11-14 13:03:20 -07:00
|
|
|
return this.getAllSelectors_(false);
|
2017-10-21 11:43:46 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-04-12 10:17:38 -06:00
|
|
|
}
|
2018-05-03 07:55:36 -06:00
|
|
|
// <<<<<<<< end of HUGE-IF-BLOCK
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
DO NOT:
|
|
|
|
- Remove the following code
|
|
|
|
- Add code beyond the following code
|
|
|
|
Reason:
|
|
|
|
- https://github.com/gorhill/uBlock/pull/3721
|
|
|
|
- uBO never uses the return value from injected content scripts
|
|
|
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
void 0;
|