1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-07 03:12:33 +01:00

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

View File

@ -283,6 +283,20 @@ FilterParser.prototype.parse = function(s) {
this.suffix = this.suffix.slice(1); 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; this.unhide = matches[2].charAt(1) === '@' ? 1 : 0;
if ( this.prefix !== '' ) { if ( this.prefix !== '' ) {
this.hostnames = this.prefix.split(/\s*,\s*/); this.hostnames = this.prefix.split(/\s*,\s*/);
@ -576,11 +590,14 @@ FilterContainer.prototype.isValidSelector = (function() {
try { try {
// https://github.com/gorhill/uBlock/issues/693 // https://github.com/gorhill/uBlock/issues/693
div.matches(s + ',\n#foo'); div.matches(s + ',\n#foo');
return true;
} catch (e) { } catch (e) {
}
if ( s.lastIndexOf('script//:', 0) === 0 ) {
return true;
}
console.error('uBlock> invalid cosmetic filter:', s); console.error('uBlock> invalid cosmetic filter:', s);
return false; return false;
}
return true;
}; };
})(); })();