From e0fd9750d453efc076d9cf0a069def234767705a Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sun, 25 Aug 2019 09:03:24 -0400 Subject: [PATCH] Further fix new setTimeout-if/setInterval-if scriptlets Addtionally, a dedicated test page has been added: https://gorhill.github.io/uBlock/tests/scriptlet-injection-filters-1.html --- assets/resources/scriptlets.js | 44 ++-- docs/tests/index.html | 1 + docs/tests/scriptlet-injection-filters-1.html | 194 ++++++++++++++++++ 3 files changed, 221 insertions(+), 18 deletions(-) create mode 100644 docs/tests/scriptlet-injection-filters-1.html diff --git a/assets/resources/scriptlets.js b/assets/resources/scriptlets.js index da3efcdde..41246049d 100644 --- a/assets/resources/scriptlets.js +++ b/assets/resources/scriptlets.js @@ -537,7 +537,7 @@ if ( delayNot ) { delay = delay.slice(1); } delay = parseInt(delay, 10); if ( needle === '' || needle === '{{1}}' ) { - needle = '.?'; + needle = ''; } else if ( needle.startsWith('/') && needle.endsWith('/') ) { needle = needle.slice(1,-1); } else { @@ -547,20 +547,24 @@ delayNot === false && isNaN(delay) ? console.log : undefined; - needle = new RegExp(needle); + const reNeedle = new RegExp(needle); window.setInterval = new Proxy(window.setInterval, { apply: function(target, thisArg, args) { const a = String(args[0]); const b = args[1]; + let defuse = false; if ( log !== undefined ) { log('uBO: setInterval("%s", %s)', a, b); - } else if ( - ( - isNaN(delay) && (delayNot || needleNot) || - isNaN(delay) === false && (b === delay) === delayNot - ) && - (needle.test(a) === needleNot) - ) { + } else if ( needle === '' && needleNot || isNaN(delay) && delayNot ) { + defuse = true; + } else if ( isNaN(delay) ) { + defuse = reNeedle.test(a) === needleNot; + } else if ( needle === '' ) { + defuse = (b === delay) === delayNot; + } else if ( reNeedle.test(a) === needleNot || (b === delay) === delayNot ) { + defuse = true; + } + if ( defuse ) { args[0] = function(){}; } return target.apply(thisArg, args); @@ -606,7 +610,7 @@ if ( delayNot ) { delay = delay.slice(1); } delay = parseInt(delay, 10); if ( needle === '' || needle === '{{1}}' ) { - needle = '.?'; + needle = ''; } else if ( needle.startsWith('/') && needle.endsWith('/') ) { needle = needle.slice(1,-1); } else { @@ -616,20 +620,24 @@ delayNot === false && isNaN(delay) ? console.log : undefined; - needle = new RegExp(needle); + const reNeedle = new RegExp(needle); window.setTimeout = new Proxy(window.setTimeout, { apply: function(target, thisArg, args) { const a = String(args[0]); const b = args[1]; + let defuse = false; if ( log !== undefined ) { log('uBO: setTimeout("%s", %s)', a, b); - } else if ( - ( - isNaN(delay) && (delayNot || needleNot) || - isNaN(delay) === false && (b === delay) === delayNot - ) && - (needle.test(a) === needleNot) - ) { + } else if ( needle === '' && needleNot || isNaN(delay) && delayNot ) { + defuse = true; + } else if ( isNaN(delay) ) { + defuse = reNeedle.test(a) === needleNot; + } else if ( needle === '' ) { + defuse = (b === delay) === delayNot; + } else if ( reNeedle.test(a) === needleNot || (b === delay) === delayNot ) { + defuse = true; + } + if ( defuse ) { args[0] = function(){}; } return target.apply(thisArg, args); diff --git a/docs/tests/index.html b/docs/tests/index.html index 3817d34a5..a9ca21c09 100644 --- a/docs/tests/index.html +++ b/docs/tests/index.html @@ -14,6 +14,7 @@
  • HNTrie, small (2) to large (40,000+) set: benchmarks
  • Procedural cosmetic filters
  • Procedural HTML filters +
  • Scriptlet injection filters / setTimeout-if diff --git a/docs/tests/scriptlet-injection-filters-1.html b/docs/tests/scriptlet-injection-filters-1.html new file mode 100644 index 000000000..f7e32b751 --- /dev/null +++ b/docs/tests/scriptlet-injection-filters-1.html @@ -0,0 +1,194 @@ + + + + + +Scriptlet injection filters / setTimeout-if + + + +

    Scriptlet injection filters / setTimeout-if

    +

    Back +

    +

    Filters

    +

    The filters below must be tried one by one, not all at the same +time. When you try a filter, ensure the result is what is expected.

    + + +
    FilterExpected +
    None     +
    + +

    Results

    +
    + + + + +
    +
    + bad, 33 + +
    + +
    +
    + bad, 66 + +
    + +
    +
    + good, 33 + +
    + +
    +
    + good, 66 + +
    + +
    + + + +