replace-fetch-response

The replace-fetch-response snippet replaces the response of a fetch request if the response text matches a given regular expression pattern.

Parameters

Name
Description
Mandatory
Default value

search

String or regex pattern that will be run against the stringified fetch response. Everything that matches the regex will be replaced with the replacement value.

Yes

n.a.

replacement

Everything that matches the regex will be replaced with the replacement value. By default this will be an empty string, so everything that matches the regex will be removed from the fetch response.

No

empty string

needle

The replacement will only happen if the needle text is present in the response. By default this is an empty string. If the string begins and ends with a slash ( / ), the text in between is treated as a regular expression.

No

empty string

Filter examples

Filter
Result

replace-xhr-response '/ads:\\[(.*?)\\]/'

Will remove anything that looks like ads:[any-string] from every fetch response body.

replace-fetch-response 'serve-ads:true' 'serve-ads:false'

Will replace the string serve-ads:true to serve-ads:false in the response body.

replace-fetch-response 'serve-ads:true' 'serve-ads:false' example.com

Will replace the string serve-ads:true to serve-ads:false in the response body only if the response contains "http://example.com"

Debugging

The following table contains messages you'll find useful during debugging:

Message
When the message occurs
Definition

Network API proxied

After we have wrapped the fetch API.

From this moment on fetch calls are intercepted by us.

Needle found in fetch response [original fetch response body]

A matching needle has been found in the response.

Needle found, we will look for the search parameter next.

Needle not found in fetch response [original fetch response body]

Needle not found in the response.

We will not modify this response.

Search * replaced with replacement in fetch response FILTER: replace-fetch-response param [replaced fetch response body]

The search parameter has been found and the response has been replaced with the replacement string.

This is a group of logs containing the filter that successfully replaced the fetch response (the parameters will be individually wrapped in single quotes) and the replaced response.

Last updated

Was this helpful?