🌐 AI搜索 & 代理 主页
Skip to content

Commit d2bce26

Browse files
committed
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.
1 parent 8cdada4 commit d2bce26

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/js/resources/prevent-fetch.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ function preventFetchFn(
3434
responseType = ''
3535
) {
3636
const safe = safeSelf();
37+
const setTimeout = self.setTimeout;
3738
const scriptletName = `${trusted ? 'trusted-' : ''}prevent-fetch`;
3839
const logPrefix = safe.makeLogPrefix(
3940
scriptletName,
4041
propsToMatch,
4142
responseBody,
4243
responseType
4344
);
45+
const extraArgs = safe.getExtraArgs(Array.from(arguments), 4);
4446
const needles = [];
4547
for ( const condition of safe.String_split.call(propsToMatch, /\s+/) ) {
4648
if ( condition === '' ) { continue; }
@@ -128,6 +130,11 @@ function preventFetchFn(
128130
responseProps
129131
);
130132
safe.Object_defineProperties(response, props);
133+
if ( extraArgs.throttle ) {
134+
return new Promise(resolve => {
135+
setTimeout(( ) => { resolve(response); }, extraArgs.throttle);
136+
});
137+
}
131138
return response;
132139
});
133140
});
@@ -159,6 +166,9 @@ registerScriptlet(preventFetchFn, {
159166
* Optional. The response type to use when emitting a dummy response as a
160167
* result of the prevention.
161168
*
169+
* @param [...varargs]
170+
* ["throttle", n]: the time to wait in ms before returning a result.
171+
*
162172
* */
163173

164174
function preventFetch(...args) {
@@ -192,6 +202,9 @@ registerScriptlet(preventFetch, {
192202
* Optional. The response type to use when emitting a dummy response as a
193203
* result of the prevention.
194204
*
205+
* @param [...varargs]
206+
* ["throttle", n]: the time to wait in ms before returning a result.
207+
*
195208
* */
196209

197210
function trustedPreventFetch(...args) {

0 commit comments

Comments
 (0)