diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/js/contentscript-start.js | 22 | ||||
-rw-r--r-- | src/js/cosmetic-filtering.js | 23 |
2 files changed, 40 insertions, 5 deletions
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; }; })(); |