new cosmetic filter to foil specific inline script tags

This commit is contained in:
gorhill 2015-09-26 19:07:23 -04:00
parent 9d90bb243f
commit 7970f4dc70
2 changed files with 40 additions and 5 deletions

View file

@ -74,6 +74,7 @@ var localMessager = vAPI.messaging.channel('contentscript-start.js');
var cosmeticFilters = function(details) {
var donthideCosmeticFilters = {};
var hideCosmeticFilters = {};
var scriptTagFilters = [];
var donthide = details.cosmeticDonthide;
var hide = details.cosmeticHide;
var i;
@ -91,9 +92,14 @@ var cosmeticFilters = function(details) {
selector = hide[i];
if ( donthideCosmeticFilters[selector] ) {
hide.splice(i, 1);
} else {
hideCosmeticFilters[selector] = true;
continue;
}
if ( selector.lastIndexOf('script//:', 0) === 0 ) {
scriptTagFilters.push(selector.slice(9));
hide.splice(i, 1);
continue;
}
hideCosmeticFilters[selector] = true;
}
}
if ( hide.length !== 0 ) {
@ -111,6 +117,11 @@ var cosmeticFilters = function(details) {
}
vAPI.donthideCosmeticFilters = donthideCosmeticFilters;
vAPI.hideCosmeticFilters = hideCosmeticFilters;
if ( scriptTagFilters.length !== 0 ) {
vAPI.reScriptTagFilters = new RegExp(scriptTagFilters.join('|'));
document.addEventListener('beforescriptexecute', onBeforeScriptExecuteHandler);
}
};
var netFilters = function(details) {
@ -128,6 +139,13 @@ var netFilters = function(details) {
//console.debug('document.querySelectorAll("%s") = %o', text, document.querySelectorAll(text));
};
var onBeforeScriptExecuteHandler = function(ev) {
if ( vAPI.reScriptTagFilters.test(ev.target.textContent) ) {
ev.preventDefault();
ev.stopPropagation();
}
};
var filteringHandler = function(details) {
var styleTagCount = vAPI.styles.length;

View file

@ -283,6 +283,20 @@ FilterParser.prototype.parse = function(s) {
this.suffix = this.suffix.slice(1);
}
// Script tag filters: pre-process them so that can be used with minimal
// overhead in the content script.
if (
this.suffix.lastIndexOf('script:contains(/', 0) === 0 &&
this.suffix.slice(-2) === '/)'
) {
// Currently supported only as non-generic selector.
if ( this.prefix.length === 0 ) {
this.invalid = true;
return this;
}
this.suffix = 'script//:' + this.suffix.slice(17, -2).replace(/\\/g, '\\');
}
this.unhide = matches[2].charAt(1) === '@' ? 1 : 0;
if ( this.prefix !== '' ) {
this.hostnames = this.prefix.split(/\s*,\s*/);
@ -576,11 +590,14 @@ FilterContainer.prototype.isValidSelector = (function() {
try {
// https://github.com/gorhill/uBlock/issues/693
div.matches(s + ',\n#foo');
return true;
} catch (e) {
console.error('uBlock> invalid cosmetic filter:', s);
return false;
}
return true;
if ( s.lastIndexOf('script//:', 0) === 0 ) {
return true;
}
console.error('uBlock> invalid cosmetic filter:', s);
return false;
};
})();