From 7970f4dc70cbc878e0c2b616ef8e35e10edddf16 Mon Sep 17 00:00:00 2001 From: gorhill Date: Sat, 26 Sep 2015 19:07:23 -0400 Subject: new cosmetic filter to foil specific inline script tags --- src/js/contentscript-start.js | 22 ++++++++++++++++++++-- src/js/cosmetic-filtering.js | 23 ++++++++++++++++++++--- 2 files changed, 40 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/js/contentscript-start.js b/src/js/contentscript-start.js index ac7d924..0eaa1d9 100644 --- a/src/js/contentscript-start.js +++ b/src/js/contentscript-start.js @@ -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; diff --git a/src/js/cosmetic-filtering.js b/src/js/cosmetic-filtering.js index 9a5d23f..5d386af 100644 --- a/src/js/cosmetic-filtering.js +++ b/src/js/cosmetic-filtering.js @@ -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; }; })(); -- cgit v1.1