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 |
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 |
|
Replaces text matching |
|
Whole-value |
|
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 |
Filter examples
For parameter syntax and escaping rules, see Snippets Overview.
|
Filter |
Result |
|---|---|
|
|
Whole-value mode. The first argument of every |
|
|
The first argument becomes a real empty array. |
|
|
Substitution mode. Rewrites every occurrence of |
|
|
Substitution mode on argument index |
|
|
As the first example, but only when |
|
|
Applies when either |
Debugging
|
Message |
When |
Definition |
|---|---|---|
|
|
Right after the snippet fires |
The |
|
|
Right after the snippet fires |
The |
|
|
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. |
|
|
After the rule has been registered |
The parameters were accepted and the rule is active for that method. |
|
|
After the method has been wrapped |
Calls to that method are now intercepted. Logged only for the first filter targeting a given method. |
|
|
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 |
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.