mirror of
https://github.com/gorhill/uBlock.git
synced 2026-03-11 09:04:36 +00:00
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:
parent
8cdada4899
commit
d2bce26e7d
1 changed files with 13 additions and 0 deletions
|
|
@ -34,6 +34,7 @@ function preventFetchFn(
|
||||||
responseType = ''
|
responseType = ''
|
||||||
) {
|
) {
|
||||||
const safe = safeSelf();
|
const safe = safeSelf();
|
||||||
|
const setTimeout = self.setTimeout;
|
||||||
const scriptletName = `${trusted ? 'trusted-' : ''}prevent-fetch`;
|
const scriptletName = `${trusted ? 'trusted-' : ''}prevent-fetch`;
|
||||||
const logPrefix = safe.makeLogPrefix(
|
const logPrefix = safe.makeLogPrefix(
|
||||||
scriptletName,
|
scriptletName,
|
||||||
|
|
@ -41,6 +42,7 @@ function preventFetchFn(
|
||||||
responseBody,
|
responseBody,
|
||||||
responseType
|
responseType
|
||||||
);
|
);
|
||||||
|
const extraArgs = safe.getExtraArgs(Array.from(arguments), 4);
|
||||||
const needles = [];
|
const needles = [];
|
||||||
for ( const condition of safe.String_split.call(propsToMatch, /\s+/) ) {
|
for ( const condition of safe.String_split.call(propsToMatch, /\s+/) ) {
|
||||||
if ( condition === '' ) { continue; }
|
if ( condition === '' ) { continue; }
|
||||||
|
|
@ -128,6 +130,11 @@ function preventFetchFn(
|
||||||
responseProps
|
responseProps
|
||||||
);
|
);
|
||||||
safe.Object_defineProperties(response, props);
|
safe.Object_defineProperties(response, props);
|
||||||
|
if ( extraArgs.throttle ) {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
setTimeout(( ) => { resolve(response); }, extraArgs.throttle);
|
||||||
|
});
|
||||||
|
}
|
||||||
return response;
|
return response;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
@ -159,6 +166,9 @@ registerScriptlet(preventFetchFn, {
|
||||||
* Optional. The response type to use when emitting a dummy response as a
|
* Optional. The response type to use when emitting a dummy response as a
|
||||||
* result of the prevention.
|
* result of the prevention.
|
||||||
*
|
*
|
||||||
|
* @param [...varargs]
|
||||||
|
* ["throttle", n]: the time to wait in ms before returning a result.
|
||||||
|
*
|
||||||
* */
|
* */
|
||||||
|
|
||||||
function preventFetch(...args) {
|
function preventFetch(...args) {
|
||||||
|
|
@ -192,6 +202,9 @@ registerScriptlet(preventFetch, {
|
||||||
* Optional. The response type to use when emitting a dummy response as a
|
* Optional. The response type to use when emitting a dummy response as a
|
||||||
* result of the prevention.
|
* result of the prevention.
|
||||||
*
|
*
|
||||||
|
* @param [...varargs]
|
||||||
|
* ["throttle", n]: the time to wait in ms before returning a result.
|
||||||
|
*
|
||||||
* */
|
* */
|
||||||
|
|
||||||
function trustedPreventFetch(...args) {
|
function trustedPreventFetch(...args) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue