Skip to content
Permalink
Browse files
Add any-delay to timeout/interval boosters
Using `*` as delay argument will match any
delay.

As per internal feedback from filter list
maintainers.

Also convert to the modern syntax which
uses `Proxy` to redefine functions.

Related commits:
- gorhill/uBlock@001f5a6
- gorhill/uBlock@9a95fbf
- gorhill/uBlock@e55cae6
- gorhill/uBlock@6f5aa94

Co-authored-by: Raymond Hill <rhill@raymondhill.net>
  • Loading branch information
JustOff and gorhill committed Jun 11, 2021
1 parent b7d0b48 commit 74f6a68ff642d958a3524bd49e33e7a98901f5b6
Showing with 78 additions and 66 deletions.
  1. +78 −66 assets/resources/resources.txt
@@ -2012,89 +2012,101 @@ set-constant.js application/javascript


# Imported from:
# https://github.com/NanoAdblocker/NanoFilters/blob/1f3be7211bb0809c5106996f52564bf10c4525f7/NanoFiltersSource/NanoResources.txt#L82
# https://github.com/NanoAdblocker/NanoFilters/blob/1f3be7211bb0809c5106996f52564bf10c4525f7/NanoFiltersSource/NanoResources.txt#L82
#
# Speed up or down setTimeout, 3 optional arguments.
# funcMatcher - The payload matcher, a string literal or a JavaScript RegExp,
# defaults to match all.
# delayMatcher - The delay matcher, an integer, defaults to 1000.
# funcMatcher
# The payload matcher, a string literal or a JavaScript RegExp, defaults
# to match all.
# delayMatcher
# The delay matcher, an integer, defaults to 1000.
# Use `*` to match any delay.
# boostRatio - The delay multiplier when there is a match, 0.5 speeds up by
# 2 times and 2 slows down by 2 times, defaults to 0.05 or speed up 20 times.
# Speed up and down both cap at 50 times.
# 2 times and 2 slows down by 2 times, defaults to 0.05 or speed up
# 20 times. Speed up and down both cap at 50 times.
nano-setTimeout-booster.js application/javascript
(function() {
// Based on uAssets
// License: https://github.com/uBlockOrigin/uAssets/blob/master/LICENSE
var z = window.setTimeout,
needle = '{{1}}',
delay = parseInt('{{2}}', 10),
boost = parseFloat('{{3}}');
if ( needle === '' || needle === '{{1}}' ) {
needle = '.?';
} else if ( needle.charAt(0) === '/' && needle.slice(-1) === '/' ) {
needle = needle.slice(1, -1);
let needleArg = '{{1}}';
if ( needleArg === '{{1}}' ) { needleArg = ''; }
let delayArg = '{{2}}';
if ( delayArg === '{{2}}' ) { delayArg = ''; }
let boostArg = '{{3}}';
if ( boostArg === '{{3}}' ) { boostArg = ''; }
if ( needleArg === '' ) {
needleArg = '.?';
} else if ( needleArg.charAt(0) === '/' && needleArg.slice(-1) === '/' ) {
needleArg = needleArg.slice(1, -1);
} else {
needle = needle.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
needle = new RegExp(needle);
if ( isNaN(delay) || !isFinite(delay) ) {
delay = 1000;
}
if ( isNaN(boost) || !isFinite(boost) ) {
boost = 0.05;
}
if ( boost < 0.02 ) {
boost = 0.02;
}
if ( boost > 50 ) {
boost = 50;
needleArg = needleArg.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
window.setTimeout = function(a, b) {
if ( b === delay && needle.test(a.toString()) ) {
b *= boost;
const reNeedle = new RegExp(needleArg);
let delay = delayArg !== '*' ? parseInt(delayArg, 10) : -1;
if ( isNaN(delay) || isFinite(delay) === false ) { delay = 1000; }
let boost = parseFloat(boostArg);
boost = isNaN(boost) === false && isFinite(boost)
? Math.min(Math.max(boost, 0.02), 50)
: 0.05;
self.setTimeout = new Proxy(self.setTimeout, {
apply: function(target, thisArg, args) {
const [ a, b ] = args;
if (
(delay === -1 || b === delay) &&
reNeedle.test(a.toString())
) {
args[1] = b * boost;
}
return target.apply(thisArg, args);
}
return z.apply(this, arguments);
}.bind(window);
});
})();


# Imported from:
# https://github.com/NanoAdblocker/NanoFilters/blob/1f3be7211bb0809c5106996f52564bf10c4525f7/NanoFiltersSource/NanoResources.txt#L126
# https://github.com/NanoAdblocker/NanoFilters/blob/1f3be7211bb0809c5106996f52564bf10c4525f7/NanoFiltersSource/NanoResources.txt#L126
#
# Same as the last one except it is for setInterval instead of setTimeout.
# Speed up or down setInterval, 3 optional arguments.
# The payload matcher, a string literal or a JavaScript RegExp, defaults
# to match all.
# delayMatcher
# The delay matcher, an integer, defaults to 1000.
# Use `*` to match any delay.
# boostRatio - The delay multiplier when there is a match, 0.5 speeds up by
# 2 times and 2 slows down by 2 times, defaults to 0.05 or speed up
# 20 times. Speed up and down both cap at 50 times.
nano-setInterval-booster.js application/javascript
(function() {
// Based on uAssets
// License: https://github.com/uBlockOrigin/uAssets/blob/master/LICENSE
var z = window.setInterval,
needle = '{{1}}',
delay = parseInt('{{2}}', 10),
boost = parseFloat('{{3}}');
if ( needle === '' || needle === '{{1}}' ) {
needle = '.?';
} else if ( needle.charAt(0) === '/' && needle.slice(-1) === '/' ) {
needle = needle.slice(1, -1);
let needleArg = '{{1}}';
if ( needleArg === '{{1}}' ) { needleArg = ''; }
let delayArg = '{{2}}';
if ( delayArg === '{{2}}' ) { delayArg = ''; }
let boostArg = '{{3}}';
if ( boostArg === '{{3}}' ) { boostArg = ''; }
if ( needleArg === '' ) {
needleArg = '.?';
} else if ( needleArg.charAt(0) === '/' && needleArg.slice(-1) === '/' ) {
needleArg = needleArg.slice(1, -1);
} else {
needle = needle.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
needle = new RegExp(needle);
if ( isNaN(delay) || !isFinite(delay) ) {
delay = 1000;
}
if ( isNaN(boost) || !isFinite(boost) ) {
boost = 0.05;
}
if ( boost < 0.02 ) {
boost = 0.02;
needleArg = needleArg.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
if ( boost > 50 ) {
boost = 50;
}
window.setInterval = function(a, b) {
if ( b === delay && needle.test(a.toString()) ) {
b *= boost;
const reNeedle = new RegExp(needleArg);
let delay = delayArg !== '*' ? parseInt(delayArg, 10) : -1;
if ( isNaN(delay) || isFinite(delay) === false ) { delay = 1000; }
let boost = parseFloat(boostArg);
boost = isNaN(boost) === false && isFinite(boost)
? Math.min(Math.max(boost, 0.02), 50)
: 0.05;
self.setInterval = new Proxy(self.setInterval, {
apply: function(target, thisArg, args) {
const [ a, b ] = args;
if (
(delay === -1 || b === delay) &&
reNeedle.test(a.toString())
) {
args[1] = b * boost;
}
return target.apply(thisArg, args);
}
return z.apply(this, arguments);
}.bind(window);
});
})();


0 comments on commit 74f6a68

Please sign in to comment.