Improve prevent-fetch scriptlet

Add vararg `throttle, n` where `n` is the time to wait in ms before
returning a response from proxied `fetch()`.

As discussed with filter list maintainers.
This commit is contained in:
Raymond Hill 2025-09-02 10:00:27 -04:00
parent 8cdada4899
commit d2bce26e7d
No known key found for this signature in database
GPG key ID: 25E1490B761470C2

View file

@ -34,6 +34,7 @@ function preventFetchFn(
responseType = ''
) {
const safe = safeSelf();
const setTimeout = self.setTimeout;
const scriptletName = `${trusted ? 'trusted-' : ''}prevent-fetch`;
const logPrefix = safe.makeLogPrefix(
scriptletName,
@ -41,6 +42,7 @@ function preventFetchFn(
responseBody,
responseType
);
const extraArgs = safe.getExtraArgs(Array.from(arguments), 4);
const needles = [];
for ( const condition of safe.String_split.call(propsToMatch, /\s+/) ) {
if ( condition === '' ) { continue; }
@ -128,6 +130,11 @@ function preventFetchFn(
responseProps
);
safe.Object_defineProperties(response, props);
if ( extraArgs.throttle ) {
return new Promise(resolve => {
setTimeout(( ) => { resolve(response); }, extraArgs.throttle);
});
}
return response;
});
});
@ -159,6 +166,9 @@ registerScriptlet(preventFetchFn, {
* Optional. The response type to use when emitting a dummy response as a
* result of the prevention.
*
* @param [...varargs]
* ["throttle", n]: the time to wait in ms before returning a result.
*
* */
function preventFetch(...args) {
@ -192,6 +202,9 @@ registerScriptlet(preventFetch, {
* Optional. The response type to use when emitting a dummy response as a
* result of the prevention.
*
* @param [...varargs]
* ["throttle", n]: the time to wait in ms before returning a result.
*
* */
function trustedPreventFetch(...args) {