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 |
Yes |
n.a. |
|
replacement |
The string to replace the matched pattern with. In JSONPath mode the value is parsed as JSON, so |
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. |
No |
|
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 |
|---|---|
|
|
Replaces every occurrence of |
|
|
Same, tolerating whitespace after the colon. The backslash is doubled so it survives the filter parser. |
|
|
Replaces every occurrence of |
|
|
Same, but only for requests whose body also contains |
|
|
JSONPath mode. Sets |
|
|
Sets every |
|
|
Append mode: pushes the string |
Debugging
|
Message |
When |
Definition |
|---|---|---|
|
|
After wrapping |
Outgoing fetch request bodies are now intercepted. Logged once per page, for the first filter. |
|
|
Right after the snippet fires |
The query could not be compiled, so the rule is not registered. |
|
|
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. |
|
|
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, per matched property |
A property matched the query and was replaced or appended to. One message per match. |
|
|
|
The existing value was neither a string, array nor object. Usually a sign the query points one level too deep. |
|
|
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.