Skip to content

Commit c553657

Browse files
committed
Add two scriptlets: setTimeout-if and setInterval-if
Usage is similar to that of raf-if introduced in commit 6831967. The two new scriptlets are meant to replace: - setTimeout-defuser - setTimeout-logger - setInterval-defuser - setInterval-logger setTimeout-logger and setInterval-logger have been removed, since they are not to be used in production. To log setTimeout and setInterval usage, respectively (using aliases): - ##+js(stif) - ##+js(siif) To defuse setTimeout unconditionally: - ##+js(stif, !) Usage of setTimeout-defuser and setInterval-defuser is deprecated and will be removed in some future when they are no longer in use. Keep in mind that the new scriptlets function on a whitelist basis, whereas the deprecated ones function on a blacklist basis. Prefixing the needle with `!` allow to use the new scriptlets on a blacklist basis.
1 parent 708e500 commit c553657

File tree

1 file changed

+55
-15
lines changed

1 file changed

+55
-15
lines changed

assets/resources/scriptlets.js

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,10 @@
407407
needle = new RegExp(needle);
408408
window.requestAnimationFrame = new Proxy(window.requestAnimationFrame, {
409409
apply: function(target, thisArg, args) {
410-
const a = args[0];
411-
const s = a.toString();
410+
const a = String(args[0]);
412411
if ( log !== undefined ) {
413-
log('uBO: requestAnimationFrame("%s")', s);
414-
}
415-
if ( needle.test(s) === not ) {
412+
log('uBO: requestAnimationFrame("%s")', a);
413+
} else if ( needle.test(a) === not ) {
416414
args[0] = function(){};
417415
}
418416
return target.apply(thisArg, args);
@@ -528,15 +526,36 @@
528526
})();
529527

530528

531-
/// setInterval-logger.js
532-
/// alias sil.js
529+
/// setInterval-if.js
530+
/// alias siif.js
533531
(function() {
534-
const log = console.log.bind(console);
532+
let needle = '{{1}}';
533+
const not = needle.charAt(0) === '!';
534+
if ( not ) { needle = needle.slice(1); }
535+
const delay = parseInt('{{2}}', 10);
536+
if ( needle === '' || needle === '{{1}}' ) {
537+
needle = '.?';
538+
} else if ( needle.startsWith('/') && needle.endsWith('/') ) {
539+
needle = needle.slice(1,-1);
540+
} else {
541+
needle = needle.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
542+
}
543+
const log = not === false && needle === '.?' && isNaN(delay)
544+
? console.log
545+
: undefined;
546+
needle = new RegExp(needle);
535547
window.setInterval = new Proxy(window.setInterval, {
536548
apply: function(target, thisArg, args) {
537-
const a = args[0];
549+
const a = String(args[0]);
538550
const b = args[1];
539-
log('uBO: setInterval("%s", %s)', a.toString(), b);
551+
if ( log !== undefined ) {
552+
log('uBO: setInterval("%s", %s)', a, b);
553+
} else if (
554+
(isNaN(delay) || b === delay) &&
555+
needle.test(a) === not
556+
) {
557+
args[0] = function(){};
558+
}
540559
return target.apply(thisArg, args);
541560
}
542561
});
@@ -569,15 +588,36 @@
569588
})();
570589

571590

572-
/// setTimeout-logger.js
573-
/// alias stl.js
591+
/// setTimeout-if.js
592+
/// alias stif.js
574593
(function() {
575-
const log = console.log.bind(console);
594+
let needle = '{{1}}';
595+
const not = needle.charAt(0) === '!';
596+
if ( not ) { needle = needle.slice(1); }
597+
const delay = parseInt('{{2}}', 10);
598+
if ( needle === '' || needle === '{{1}}' ) {
599+
needle = '.?';
600+
} else if ( needle.startsWith('/') && needle.endsWith('/') ) {
601+
needle = needle.slice(1,-1);
602+
} else {
603+
needle = needle.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
604+
}
605+
const log = not === false && needle === '.?' && isNaN(delay)
606+
? console.log
607+
: undefined;
608+
needle = new RegExp(needle);
576609
window.setTimeout = new Proxy(window.setTimeout, {
577610
apply: function(target, thisArg, args) {
578-
const a = args[0];
611+
const a = String(args[0]);
579612
const b = args[1];
580-
log('uBO: setTimeout("%s", %s)', a.toString(), b);
613+
if ( log !== undefined ) {
614+
log('uBO: setTimeout("%s", %s)', a, b);
615+
} else if (
616+
(isNaN(delay) || b === delay) &&
617+
needle.test(a) === not
618+
) {
619+
args[0] = function(){};
620+
}
581621
return target.apply(thisArg, args);
582622
}
583623
});

0 commit comments

Comments
 (0)