|
4 | 4 | - [trusted-create-element](#trusted-create-element) |
5 | 5 | - [trusted-dispatch-event](#trusted-dispatch-event) |
6 | 6 | - [trusted-prune-inbound-object](#trusted-prune-inbound-object) |
| 7 | +- [trusted-replace-argument](#trusted-replace-argument) |
7 | 8 | - [trusted-replace-fetch-response](#trusted-replace-fetch-response) |
8 | 9 | - [trusted-replace-node-text](#trusted-replace-node-text) |
9 | 10 | - [trusted-replace-outbound-text](#trusted-replace-outbound-text) |
@@ -439,6 +440,136 @@ example.org#%#//scriptlet('trusted-prune-inbound-object', functionName[, propsTo |
439 | 440 |
|
440 | 441 | * * * |
441 | 442 |
|
| 443 | +## <a id="trusted-replace-argument"></a> ⚡️ trusted-replace-argument |
| 444 | +
|
| 445 | +> Added in unknown. |
| 446 | +
|
| 447 | +Replaces a specific argument of a native method with a constant value, JSON parsed value |
| 448 | +or a value derived from a regular expression replacement. |
| 449 | +
|
| 450 | +Related UBO scriptlet: |
| 451 | +https://github.com/gorhill/ublock/wiki/Resources-Library#trusted-replace-argumentjs- |
| 452 | +
|
| 453 | +### Syntax |
| 454 | +
|
| 455 | +<!-- markdownlint-disable line-length --> |
| 456 | +
|
| 457 | +```text |
| 458 | +example.org#%#//scriptlet('trusted-replace-argument', methodPath, [argumentIndex, [argumentValue[, pattern[, stack[, verbose]]]]]) |
| 459 | +``` |
| 460 | + |
| 461 | + |
| 462 | +- `methodPath` – required, string path to a native method (joined with `.` if needed). The property must be attached to `window`. |
| 463 | +- `argumentIndex` – required, string index of the argument to replace (0-based). |
| 464 | +- `argumentValue` – required, string value to set for the argument. |
| 465 | + If it starts with `replace:`, it is treated as a replacement pattern in the format `replace:/regex/replacement/`. |
| 466 | + To replace all occurrences of a pattern, the replacement string must include the global flag `g`, like this: `replace:/foo/bar/g`, otherwise only the first occurrence will be replaced. |
| 467 | + If it starts with `json:`, it is treated as a JSON string to parse and set for the argument. For example, `json:{"key": "value"}` will set the argument to an object `{ key: 'value' }`. |
| 468 | + If it does not start with `replace:` or `json:`, it is treated as a constant value to set for the argument, or as one of the following predefined constants: |
| 469 | + - `undefined` |
| 470 | + - `false` |
| 471 | + - `true` |
| 472 | + - `null` |
| 473 | + - `emptyObj` — empty object |
| 474 | + - `emptyArr` — empty array |
| 475 | + - `noopFunc` — function with empty body |
| 476 | + - `noopCallbackFunc` — function returning noopFunc |
| 477 | + - `trueFunc` — function returning true |
| 478 | + - `falseFunc` — function returning false |
| 479 | + - `throwFunc` — function throwing an error |
| 480 | + - `noopPromiseResolve` — function returning Promise object that is resolved with an empty response |
| 481 | + - `noopPromiseReject` — function returning Promise.reject() |
| 482 | +- `pattern` – optional, string or regular expression pattern to match the argument against. If provided, the argument will only be replaced if it matches this pattern. |
| 483 | +- `stack` — optional, string or regular expression that must match the current function call stack trace. |
| 484 | +- `verbose` — optional, string, if set to `'true'`, logs the method arguments. Defaults to `'false'`. |
| 485 | + It may be useful for debugging but it is not allowed for prod versions of filter lists. |
| 486 | + |
| 487 | + |
| 488 | +### Examples |
| 489 | + |
| 490 | +1. Set the first argument of `eval` with a constant value `"Replacement"` if the pattern matches: |
| 491 | + |
| 492 | + ```adblock |
| 493 | + example.org#%#//scriptlet('trusted-replace-argument', 'eval', '0', '"Replacement"', 'Foo bar') |
| 494 | + ``` |
| 495 | +
|
| 496 | + For instance, the following call will return `"Replacement"`: |
| 497 | +
|
| 498 | + ```html |
| 499 | + eval('"Foo bar"'); |
| 500 | + ``` |
| 501 | +
|
| 502 | +1. Replace the part `foo` of the first argument of `eval` with a `bar` value if the pattern matches: |
| 503 | +
|
| 504 | + ```adblock |
| 505 | + example.org#%#//scriptlet('trusted-replace-argument', 'eval', '0', 'replace:/foo/bar/', 'Text content foo') |
| 506 | + ``` |
| 507 | +
|
| 508 | + For instance, the following call will return `"Text content bar"`: |
| 509 | +
|
| 510 | + ```html |
| 511 | + eval('"Text content foo"'); |
| 512 | + ``` |
| 513 | +
|
| 514 | +1. Replace all occurrences of the first argument of `JSON.parse` with a constant value `"no_ads"` if the pattern matches: |
| 515 | +
|
| 516 | + ```adblock |
| 517 | + example.org#%#//scriptlet('trusted-replace-argument', 'JSON.parse', '0', 'replace:/ads/no_ads/g', 'ads') |
| 518 | + ``` |
| 519 | +
|
| 520 | + For instance, the following call: |
| 521 | +
|
| 522 | + ```html |
| 523 | + const jsonString = '{ "ads1": 1, "ads2": 2, "content": "fooBar", "ads3": 3 }'; |
| 524 | + const result = JSON.parse(jsonString); |
| 525 | + ``` |
| 526 | +
|
| 527 | + will return the object: |
| 528 | +
|
| 529 | + ```json |
| 530 | + { |
| 531 | + no_ads1: 1, |
| 532 | + no_ads2: 2, |
| 533 | + content: 'fooBar', |
| 534 | + no_ads3: 3 |
| 535 | + } |
| 536 | + ``` |
| 537 | +
|
| 538 | +1. Replace the third argument of `Object.defineProperty` with a JSON object `{"value": "disabled"}` if the pattern matches: |
| 539 | +
|
| 540 | + ```adblock |
| 541 | + example.org#%#//scriptlet('trusted-replace-argument', 'Object.defineProperty', '2', 'json:{"value": "disabled"}', 'enabled') |
| 542 | + ``` |
| 543 | +
|
| 544 | + For instance, `window.adblock` property for the following call will return `"disabled"`: |
| 545 | +
|
| 546 | + ```html |
| 547 | + Object.defineProperty(window, 'adblock', { value: 'enabled' }); |
| 548 | + ``` |
| 549 | +
|
| 550 | +1. Replace first argument of `MutationObserver` with `noopFunc` if the pattern matches: |
| 551 | +
|
| 552 | + ```adblock |
| 553 | + example.org#%#//scriptlet('trusted-replace-argument', 'MutationObserver', '0', 'noopFunc', 'Adblock') |
| 554 | + ``` |
| 555 | +
|
| 556 | + For instance, `callback` function for the following call will be replaced with `noopFunc`: |
| 557 | +
|
| 558 | + ```html |
| 559 | + const callback = () => { |
| 560 | + if(adblock) { |
| 561 | + document.body.innerHTML = 'Adblock detected'; |
| 562 | + } |
| 563 | + }; |
| 564 | + const observerToPrevent = new MutationObserver(callback); |
| 565 | + ``` |
| 566 | +
|
| 567 | +<!-- markdownlint-enable line-length --> |
| 568 | +
|
| 569 | +[Scriptlet source](../src/scriptlets/trusted-replace-argument.ts) |
| 570 | +
|
| 571 | +* * * |
| 572 | +
|
442 | 573 | ## <a id="trusted-replace-fetch-response"></a> ⚡️ trusted-replace-fetch-response |
443 | 574 |
|
444 | 575 | > Added in v1.7.3. |
|
0 commit comments