Skip to content

Commit

Permalink
new cosmetic filter to foil specific inline script tags
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Sep 26, 2015
1 parent 9d90bb2 commit 7970f4d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
22 changes: 20 additions & 2 deletions src/js/contentscript-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 ) {
Expand All @@ -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) {
Expand All @@ -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;

Expand Down
23 changes: 20 additions & 3 deletions src/js/cosmetic-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -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*/);
Expand Down Expand Up @@ -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;
};
})();

Expand Down

0 comments on commit 7970f4d

Please sign in to comment.