replace-argument

replace-argument

The replace-argument snippet wraps a native method and rewrites one of its call arguments before the method runs. Use it to neutralise a configuration call without having to override the whole property, or to rewrite part of a serialised payload on its way into a method such as JSON.parse.

On the first filter for a given method, the method is resolved from the page global and wrapped once. Further filters targeting the same method append rules. On each call the first rule that actually changes the target argument wins, the original method is always invoked with the resulting arguments, and the wrapper never throws — if anything goes wrong internally the call proceeds with the arguments untouched.

Parameters

Name

Description

Mandatory

Default

methodPath

Dotted path to the method to wrap, resolved from the page global — for example adConfig.setEnabled. Must resolve to a function.

Yes

n.a.

argPosition

Zero-based index of the argument to rewrite. Must be a non-negative integer. Calls that pass fewer arguments than this index are skipped.

Yes

n.a.

pattern

Regex or literal text matched against the stringified argument. An empty string selects whole-value mode — see below.

No

''

replacement

In substitution mode, the replacement text. In whole-value mode, a value token injected as a real typed value.

No

''

stackNeedle

Comma-separated list of strings or regex which must be present in the callstack for the rule to apply. If given, the rewrite only happens when the callstack contains at least one of them, so the method keeps behaving normally everywhere else on the page.

No

''

Modes

Mode

Selected by

Behaviour

Substitution

pattern is non-empty

Replaces text matching pattern inside the stringified argument, keeping the rest of the value. Every match is replaced, not just the first. If the replacement produces no change, the rule does not count as a match and the next rule is tried.

Whole-value

pattern is an empty string

Replaces the whole argument with a typed value, or with a literal string if the token is not a recognised one. The accepted values are the same list as override-property-read, so false becomes the boolean and emptyArray becomes a real empty array.

Filter examples

For parameter syntax and escaping rules, see Snippets Overview.

Filter

Result

replace-argument adConfig.setEnabled 0 '' false

Whole-value mode. The first argument of every adConfig.setEnabled() call becomes the boolean false.

replace-argument adConfig.setSlots 0 '' emptyArray

The first argument becomes a real empty array.

replace-argument JSON.parse 0 /"showAds":true/ '"showAds":false'

Substitution mode. Rewrites every occurrence of "showAds":true inside the string passed to JSON.parse(), leaving the rest of the payload intact.

replace-argument tracker.send 1 /uid=[^&]*/ 'uid=0'

Substitution mode on argument index 1. Every uid=... match in the stringified argument is replaced.

replace-argument adConfig.setEnabled 0 '' false ads.js

As the first example, but only when ads.js appears in the callstack. The call behaves normally when it originates anywhere else.

replace-argument adConfig.setEnabled 0 '' false 'ads.js,tracker.js'

Applies when either ads.js or tracker.js appears in the callstack.

Debugging

Message

When

Definition

methodPath param must be a string.

Right after the snippet fires

The methodPath parameter is missing or is not a string. The snippet does nothing.

argPosition param must be a non-negative integer.

Right after the snippet fires

The argPosition parameter is not a non-negative integer. The snippet does nothing.

could not resolve <<methodPath>>

Right after the snippet fires

The path did not resolve to a function. Either the path is wrong, or the snippet ran before the page defined the method.

Added rule for <<methodPath>>

After the rule has been registered

The parameters were accepted and the rule is active for that method.

<<methodPath>> wrapped

After the method has been wrapped

Calls to that method are now intercepted. Logged only for the first filter targeting a given method.

argument <<argPosition>> of <<methodPath>> replaced

A call matched and the argument actually changed

The rewrite happened. If the wrapped message appears but this one never does, the method is being called but no rule changed the argument — check pattern, argPosition and stackNeedle.

The method must already exist on the page when the snippet runs. In substitution mode, object and array arguments are passed through untouched, since only stringified values are rewritten — use whole-value mode for those.