replace-fetch-response

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

search

String or regex pattern run against the stringified fetch response.

Yes

n.a.

replacement

Everything that matches the regex will be replaced with this.

No

empty string

needle

Replacement only happens if needle text is present in the response. Enclose in / for regex.

No

empty string

Filter examples

Filter

Result

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

Removes anything like ads:[any-string] from every fetch response body.

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

Replaces serve-ads:true with serve-ads:false in the response body.

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

Replaces only if the response contains "example.com".

Debugging

Message

When

Definition

Network API proxied

After wrapping fetch

fetch calls are intercepted.

Needle found in fetch response

Matching needle found

We will look for the search parameter next.

Needle not found in fetch response

Needle not found

Response will not be modified.

Search * replaced with replacement in fetch response

After replacement

The response was successfully replaced.