2017-07-23 17:33:39 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
|
|
|
Copyright (C) 2017 Raymond Hill
|
|
|
|
|
|
|
|
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';
|
|
|
|
|
|
|
|
// For content pages
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
if ( typeof vAPI === 'object' ) { // >>>>>>>> start of HUGE-IF-BLOCK
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2017-07-23 17:33:39 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-10-22 14:59:29 +02:00
|
|
|
vAPI.userStylesheet = {
|
|
|
|
added: new Set(),
|
|
|
|
removed: new Set(),
|
|
|
|
apply: function() {
|
|
|
|
if ( this.added.size === 0 && this.removed.size === 0 ) { return; }
|
|
|
|
vAPI.messaging.send('vapi-background', {
|
|
|
|
what: 'userCSS',
|
|
|
|
add: Array.from(this.added),
|
|
|
|
remove: Array.from(this.removed)
|
|
|
|
});
|
|
|
|
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(); }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
vAPI.DOMFilterer = function() {
|
|
|
|
this.commitTimer = new vAPI.SafeAnimationFrame(this.commitNow.bind(this));
|
|
|
|
this.domIsReady = document.readyState !== 'loading';
|
2017-10-22 14:59:29 +02:00
|
|
|
this.disabled = false;
|
2017-10-21 19:43:46 +02:00
|
|
|
this.listeners = [];
|
2017-10-22 14:59:29 +02:00
|
|
|
this.filterset = new Set();
|
2017-10-21 19:43:46 +02:00
|
|
|
this.excludedNodeSet = new WeakSet();
|
2017-10-22 14:59:29 +02:00
|
|
|
this.addedCSSRules = new Set();
|
2017-10-21 19:43:46 +02:00
|
|
|
|
|
|
|
if ( this.domIsReady !== true ) {
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
this.domIsReady = true;
|
|
|
|
this.commit();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
vAPI.DOMFilterer.prototype = {
|
|
|
|
reOnlySelectors: /\n\{[^\n]+/g,
|
2017-10-22 14:59:29 +02:00
|
|
|
|
|
|
|
// Here we will deal with:
|
|
|
|
// - Injecting low priority user styles;
|
|
|
|
// - Notifying listeners about changed filterset.
|
2017-10-21 19:43:46 +02:00
|
|
|
commitNow: function() {
|
|
|
|
this.commitTimer.clear();
|
2017-10-22 14:59:29 +02:00
|
|
|
var userStylesheet = vAPI.userStylesheet,
|
|
|
|
addedSelectors = [];
|
|
|
|
for ( var entry of this.addedCSSRules ) {
|
2017-10-23 15:01:00 +02:00
|
|
|
if (
|
|
|
|
this.disabled === false &&
|
|
|
|
entry.lazy &&
|
|
|
|
entry.injected === false
|
|
|
|
) {
|
2017-10-22 14:59:29 +02:00
|
|
|
userStylesheet.add(
|
|
|
|
entry.selectors + '\n{' + entry.declarations + '}'
|
|
|
|
);
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
2017-10-22 14:59:29 +02:00
|
|
|
addedSelectors.push(entry.selectors);
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
2017-10-22 14:59:29 +02:00
|
|
|
this.addedCSSRules.clear();
|
|
|
|
userStylesheet.apply();
|
|
|
|
if ( addedSelectors.length !== 0 ) {
|
|
|
|
this.triggerListeners('declarative', addedSelectors.join(',\n'));
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
commit: function(commitNow) {
|
|
|
|
if ( commitNow ) {
|
|
|
|
this.commitTimer.clear();
|
|
|
|
this.commitNow();
|
|
|
|
} else {
|
|
|
|
this.commitTimer.start();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addCSSRule: function(selectors, declarations, details) {
|
|
|
|
if ( selectors === undefined ) { return; }
|
|
|
|
var selectorsStr = Array.isArray(selectors)
|
|
|
|
? selectors.join(',\n')
|
|
|
|
: selectors;
|
|
|
|
if ( selectorsStr.length === 0 ) { return; }
|
2017-10-23 15:01:00 +02:00
|
|
|
if ( details === undefined ) { details = {}; }
|
2017-10-22 14:59:29 +02:00
|
|
|
var entry = {
|
2017-10-21 19:43:46 +02:00
|
|
|
selectors: selectorsStr,
|
|
|
|
declarations,
|
2017-10-23 15:01:00 +02:00
|
|
|
lazy: details.lazy === true,
|
|
|
|
injected: details.injected === true
|
2017-10-22 14:59:29 +02:00
|
|
|
};
|
|
|
|
this.addedCSSRules.add(entry);
|
|
|
|
this.filterset.add(entry);
|
2017-10-23 15:01:00 +02:00
|
|
|
if (
|
|
|
|
this.disabled === false &&
|
|
|
|
entry.lazy !== true &&
|
|
|
|
entry.injected !== true
|
|
|
|
) {
|
2017-10-22 14:59:29 +02:00
|
|
|
vAPI.userStylesheet.add(selectorsStr + '\n{' + declarations + '}');
|
|
|
|
}
|
2017-10-21 19:43:46 +02:00
|
|
|
this.commit();
|
|
|
|
},
|
|
|
|
|
|
|
|
addListener: function(listener) {
|
|
|
|
if ( this.listeners.indexOf(listener) !== -1 ) { return; }
|
|
|
|
this.listeners.push(listener);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeListener: function(listener) {
|
|
|
|
var pos = this.listeners.indexOf(listener);
|
|
|
|
if ( pos === -1 ) { return; }
|
|
|
|
this.listeners.splice(pos, 1);
|
|
|
|
},
|
|
|
|
|
|
|
|
triggerListeners: function(type, selectors) {
|
|
|
|
var i = this.listeners.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
this.listeners[i].onFiltersetChanged(type, selectors);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
excludeNode: function(node) {
|
|
|
|
this.excludedNodeSet.add(node);
|
|
|
|
this.unhideNode(node);
|
|
|
|
},
|
|
|
|
|
2017-10-22 14:59:29 +02:00
|
|
|
unexcludeNode: function(node) {
|
|
|
|
this.excludedNodeSet.delete(node);
|
|
|
|
},
|
|
|
|
|
2017-10-21 19:43:46 +02:00
|
|
|
hideNode: function(node) {
|
|
|
|
if ( this.excludedNodeSet.has(node) ) { return; }
|
2017-10-23 18:21:37 +02:00
|
|
|
if ( this.hideNodeAttr === undefined ) { return; }
|
|
|
|
node.setAttribute(this.hideNodeAttr, '');
|
|
|
|
if ( this.hideNodeStyleSheetInjected === false ) {
|
|
|
|
this.hideNodeStyleSheetInjected = true;
|
2017-10-21 19:43:46 +02:00
|
|
|
this.addCSSRule(
|
2017-10-23 18:21:37 +02:00
|
|
|
'[' + this.hideNodeAttr + ']',
|
|
|
|
'display:none!important;'
|
2017-10-21 19:43:46 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
unhideNode: function(node) {
|
2017-10-23 18:21:37 +02:00
|
|
|
if ( this.hideNodeAttr === undefined ) { return; }
|
|
|
|
node.removeAttribute(this.hideNodeAttr);
|
2017-10-21 19:43:46 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
toggle: function(state) {
|
2017-10-22 14:59:29 +02:00
|
|
|
if ( state === undefined ) { state = this.disabled; }
|
|
|
|
if ( state !== this.disabled ) { return; }
|
|
|
|
this.disabled = !state;
|
|
|
|
var userStylesheet = vAPI.userStylesheet;
|
|
|
|
for ( var entry of this.filterset ) {
|
|
|
|
var rule = entry.selectors + '\n{' + entry.declarations + '}';
|
|
|
|
if ( this.disabled ) {
|
|
|
|
userStylesheet.remove(rule);
|
|
|
|
} else {
|
|
|
|
userStylesheet.add(rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
userStylesheet.apply();
|
2017-10-21 19:43:46 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
getAllDeclarativeSelectors_: function(all) {
|
|
|
|
let selectors = [];
|
2017-10-22 14:59:29 +02:00
|
|
|
for ( var entry of this.filterset ) {
|
|
|
|
if ( all === false && entry.internal ) { continue; }
|
|
|
|
selectors.push(entry.selectors);
|
2017-10-21 19:43:46 +02:00
|
|
|
}
|
2017-10-23 18:21:37 +02:00
|
|
|
var out = selectors.join(',\n');
|
|
|
|
if ( !all && this.hideNodeAttr !== undefined ) {
|
|
|
|
out = out.replace('[' + this.hideNodeAttr + ']', '')
|
|
|
|
.replace(/^,\n|\n,|,\n$/, '');
|
|
|
|
}
|
|
|
|
return out;
|
2017-10-21 19:43:46 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
getFilteredElementCount: function() {
|
|
|
|
let selectors = this.getAllDeclarativeSelectors_(true);
|
|
|
|
return selectors.length !== 0
|
|
|
|
? document.querySelectorAll(selectors).length
|
|
|
|
: 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
getAllDeclarativeSelectors: function() {
|
|
|
|
return this.getAllDeclarativeSelectors_(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
} // <<<<<<<< end of HUGE-IF-BLOCK
|