replace-fetch-request

replace-fetch-request

The replace-fetch-request snippet replaces the request body of fetch() calls before they are sent, if the body matches a given string. It is the fetch counterpart of replace-xhr-request.

Where replace-fetch-response rewrites what the server sends back, this snippet rewrites what the page sends out — useful when a site reports adblock state, consent flags or ad-slot requests to its backend and changes its behaviour based on the reply.

fetch is wrapped once, on the first filter. Further filters append rules, and every rule is applied in turn to the progressively modified body.

Parameters

Name

Description

Mandatory

Default

search

String or regex pattern to match in the request body. The pattern is globalised, so every occurrence is replaced. If the value starts with jsonpath(, it is treated as a JSONPath query instead — see below.

Yes

n.a.

replacement

The string to replace the matched pattern with. In JSONPath mode the value is parsed as JSON, so false becomes the boolean, null becomes null and [] becomes an empty array; if it is not valid JSON the raw string is used.

No

empty string

needle

Regex used to pre-filter on the body content. If specified, the replacement is only applied when the needle matches the request body.

No

null

mode

JSONPath operation mode. replace sets the matched property to the parsed replacement value. append adds to the existing value instead: it concatenates strings, pushes into or concatenates arrays, and merges objects. Ignored outside JSONPath mode.

No

replace

JSONPath mode

When search starts with jsonpath(, the body is parsed as JSON, the query is evaluated against it, every matching property is replaced or appended to, and the result is re-serialised. Use this when the value to change is a structured field rather than a text pattern.

If the body is not valid JSON, or the query fails to evaluate, the rule is skipped and the body is left unchanged rather than corrupted.

  • For the supported query syntax, see JSONPath evaluator.

  • Quotes inside a filter expression must be written \', or the parser strips them. See Snippets Overview for the full escaping rules.

Filter examples

Filter

Result

replace-fetch-request '"adblock":true' '"adblock":false'

Replaces every occurrence of "adblock":true with "adblock":false in the body of every outgoing fetch request.

replace-fetch-request '/"adblock":\\s*true/' '"adblock":false'

Same, tolerating whitespace after the colon. The backslash is doubled so it survives the filter parser.

replace-fetch-request /trackingId/ blocked

Replaces every occurrence of trackingId with blocked.

replace-fetch-request /trackingId/ blocked analytics

Same, but only for requests whose body also contains analytics.

replace-fetch-request 'jsonpath($.ads.enabled)' false

JSONPath mode. Sets ads.enabled to the boolean false.

replace-fetch-request 'jsonpath($..trackingId)' null

Sets every trackingId property at any depth to null.

replace-fetch-request 'jsonpath($.tags)' '"blocked"' '' append

Append mode: pushes the string "blocked" into the existing tags array instead of replacing it.

Debugging

Message

When

Definition

fetch proxied

After wrapping fetch

Outgoing fetch request bodies are now intercepted. Logged once per page, for the first filter.

Invalid JSONPath query: <<search>>. Error: <<message>>

Right after the snippet fires

The query could not be compiled, so the rule is not registered.

'<<needle>>' found in fetch request body

A body matched the needle

The rule will now be applied. There is no "not found" counterpart, so if this never appears the needle is not matching.

'<<search>>' replaced with '<<replacement>>' in fetch request body

Text mode, and the body actually changed

Only logged when the body genuinely changed, so it will not fire for a rule that matched nothing.

JSONPath [<<mode>>] at [<<key>>] with <<replacement>>

JSONPath mode, per matched property

A property matched the query and was replaced or appended to. One message per match.

append has no meaning for the value at [<<key>>], replaced it instead

append mode hit a value it cannot append to

The existing value was neither a string, array nor object. Usually a sign the query points one level too deep.

JSONPath: skipping non-JSON body or evaluation error: <<message>>

The body could not be handled

The body was not valid JSON, or the query threw. The body is left untouched.

A missing or empty search produces none of the messages above: the snippet throws before the rule is registered, so it installs nothing. The throw is still visible as a console error reading [replace-fetch-request]: Missing 'search' parameter.

Only string request bodies are rewritten. FormData, Blob, URLSearchParams and ArrayBuffer bodies pass through untouched, and GET and HEAD requests cannot carry a body at all.