From 6831967f5f9d64412a9c063f3b64104d9dce7b07 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Wed, 21 Aug 2019 10:13:23 -0400 Subject: [PATCH] Add new scriptlet to defuse calls to requestAnimationFrame Scriptlet name: `raf-if.js` Usage: `example.com##+js(raf-if, !/(\d+){4}/)` Argument: one single argument, which is the "needle" to find in the stringified argument passed to requestAnimationFrame. requestAnimationFrame will be defused when: - The needle is not prefixed with `!` and the needle does not match the stringified argument; OR - The needle is prefixed with `!` and the needle matches the stringified argument. The `raf-if.js` scriptlet will log calls to requestAnimationFrame to the console when no parameter is provided, i.e.: example.com##+js(raf-if) Otherwise no logging occurs. --- assets/resources/scriptlets.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/assets/resources/scriptlets.js b/assets/resources/scriptlets.js index 47924e227..fbb48d703 100644 --- a/assets/resources/scriptlets.js +++ b/assets/resources/scriptlets.js @@ -391,6 +391,36 @@ })(); +/// raf-if.js +(function() { + let needle = '{{1}}'; + const not = needle.charAt(0) === '!'; + if ( not ) { needle = needle.slice(1); } + if ( needle === '' || needle === '{{1}}' ) { + needle = '.?'; + } else if ( needle.startsWith('/') && needle.endsWith('/') ) { + needle = needle.slice(1,-1); + } else { + needle = needle.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + } + const log = needle === '.?' && not === false ? console.log : undefined; + needle = new RegExp(needle); + window.requestAnimationFrame = new Proxy(window.setTimeout, { + apply: function(target, thisArg, args) { + const a = args[0]; + const s = a.toString(); + if ( log !== undefined ) { + log('uBO: requestAnimationFrame("%s")', s); + } + if ( needle.test(s) === not ) { + args[0] = function(){}; + } + return target.apply(thisArg, args); + } + }); +})(); + + /// set-constant.js /// alias set.js (function() {