Resources Library·续4

Examples: - example.com##+js(rpnt, #text, /^Advertisement$/) - example.com##+js(rpnt, #text, Example Domain, Changed, condition, Example, stay, 1) - example.com##+js(rpnt, script, /devtoolsDetector\.launch\(\)\;/, , sedCount, 1)

Related discussions: - New rpnt (prev. sed) scriptlet - Using trusted-replace-node-text

Also see: - remove-node-text - AdGuard trusted-replace-node-text


remove-node-text.js ↪

rmnt.js

New in 1.49.3rc15

Remove the whole text of a DOM node.

By default, the scriptlet will bail out when the document itself has been fully loaded, i.e. when DOMContentLoaded event is fired.

The mutation observer of this scriptlet can be a significant overhead for pages with dynamically updated DOM, and in most cases the scriptlet is useful only for DOM changes occurring before the DOMContentLoaded event, so the default is to quit out when that event is received ("quit out" means discarding the mutation observer and having the scriptlet garbage-collected by the JS engine).

Parameters: - required, the name of the node for which the text content must be removed. Valid node names can be found at: https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeName - required, A string or regex to find in the text content of the node as the target of removing

Optionally, extra pairs of parameters (tokens) can be used to modify the behavior of the scriptlet.

Tokens: - includes, pattern: A string or regex which must be found in the text content of the node in order for the removing to occur - excludes, pattern: A string or regex which must not be found in the text content of the node in order for the removing to occur - sedCount, n: This will cause the scriptlet to stop after n instances of removing. Since a mutation oberver is used by the scriptlet, it's advised to stop it whenever it becomes pointless. Default to zero, which means the scriptlet never stops - stay, 1: Force the scriptlet to stay at work forever - quitAfter, ms: This tells the scriptlet to quit ms milliseconds after the page has been loaded, i.e. after the DOMContentLoaded event has been fired - ~~log, 1~~: This will cause the scriptlet to output information at the console, useful as a debugging tool for filter authors
(deprecated, read about the new method of logging scriptlets)

Examples: - example.com##+js(rmnt, #text, Example) - example.com##+js(rmnt, #text, Example, condition, Exa) - example.com##+js(rmnt, script, timeLeft)

Also see: - trusted-replace-node-text - AdGuard remove-node-text


trusted-replace-outbound-text.js ↪

trusted-rpot.js

Trusted scriptlet

New in 1.57.1b1

(currently alias trusted-rpot is unsupported yet)

Replace the text in the outbound function call.

Parameters: - required, a name of method for which the text content must be substituted - required, a string or regex to match in the outbound text as the target of substitution,
if not provided or empty, the scriptlet will only log the outbound text without modifying it. - optional, the replacement string for the matched part,
can be omitted if the goal is to delete the text which matches the pattern.

Tokens: - condition, pattern: is a vararg which tells the scriptlet to act only if pattern is found in the outbound text. - encoding, base64: Decode and encode outbound base64-encoded text before and after replacement. <sup>1.58.1b3</sup>

Examples: - www.reddit.com##+js(trusted-replace-outbound-text, JSON.stringify) (logging only) - www.reddit.com##+js(trusted-replace-outbound-text, JSON.stringify, '/"csrf_token":"[^"]+",/')
matches and removes: "csrf_token":"ed5ce21c7d7c95c8662c7844c0f4a4dc", from the JSON payload
- www.reddit.com##+js(trusted-replace-outbound-text, JSON.stringify, '/(?<="csrf_token":")[^"]+?(?=")/') or
www.reddit.com##+js(trusted-replace-outbound-text, JSON.stringify, '/("csrf_token":")(?:[^"]+?)(")/', /$1$2/)
matches ed5ce21c7d7c95c8662c7844c0f4a4dc or any similiar value in "csrf_token":"..." and removes it resulting in empty value: "csrf_token":"" - www.reddit.com##+js(trusted-replace-outbound-text, JSON.stringify, '/(?<="adblock":{"enabled":)true/', false)
matches true in "adblock":{"enabled":true and replaces it with false resulting in a new spoofed value: "adblock":{"enabled":false - example.com##+js(trusted-replace-outbound-text, eval, ads:true, ads:false, encoding, base64) For instance, eval('"YWRzOnRydWU="') which decoded value is 'ads:true', will return 'YWRzOmZhbHNl' which is 'ads:false' after decoding.

When the replacement starts with json:, it will be first decoded using JSON.parse() (since 1.59.1b8), example:

  • example.com##+js(trusted-replace-outbound-text, somefn, json:"ok")

The doublequotes are required since this is what JSON.parse() expects as a valid JSON string.

<br>

Solves related issues / discussions: - Add a scriptlet to intercept atob call and prune its text output - Add new scriptlet — 'trusted-replace-outbound-text' - reddit.com: privacy - trusted-replace-outbound-text

Also see: - AdGuard trusted-replace-outbound-text


href-sanitizer.js ↪

Experimental

New in 1.47.5b4

Set the href attribute to a value found in the DOM at, or below the targeted a element, and optionally with transformation steps.

Parameters: - required, A plain CSS selector for elements which href property must be sanitized, if ALL the following conditions are met: - The element is a link (<a>) element - The link element has an existing href attribute - The text content of the element is a valid https-based URL - optional, one or more tokens to lookup the source of the href property, and optionally the transformation steps to perform, otherwise the text content of the element will be used. - [name]: Use the value of the attribute name as the URL - ?name: Use the value of the href query parameter name <sup>1.49.3rc15</sup>. Supports unwrapping query parameters recursively <sup>1.58.1rc11</sup> - Transformation steps: see urlskip documentation <sup>1.61.3b10</sup>

Examples: - vk.com##+js(href-sanitizer, a[href^="/go?to="][title], [title]) - vk.com##+js(href-sanitizer, a[href^="/away.php?to="][title], ?to) - <a href="https://app.adjust.com/2uo1qc?redirect=https%3A%2F%2Fplay.google.com%2Fstore%2Fapps%2Fdetails%3Fid%3Dorg.mozilla.firefox&amp;campaign=www.mozilla.org&amp;adgroup=mobile-android-page">Text</a>: mozilla.org##+js(href-sanitizer, a[href^="https://app.adjust.com/"][href*="?redirect="], ?redirect) - example.com##+js(href-sanitizer, a.clickTracker, ?r?u - example.org##+js(href-sanitizer, a[href*="/redirect"], ?url ?url -base64)

Solves Anti-redirect scriptlet for vk.com (href-sanitizer).

Also see: - AdGuard href-sanitizer


prevent-refresh.js ↪

refresh-defuser.js

New in 1.38.7b3

Attempts to defuse reloading of a document through a meta "refresh" tag. Will stop navigation (call window.stop()) at specified delay.

Parameters: - optional, number (float), number of seconds until the page will be reloaded / when defuser should run. Will be derived from source tag when not specified.

Also see: - AdGuard prevent-refresh


trusted-click-element.js ↪

New in 1.53.5rc13

Trusted scriptlet

Clicks elements on webpages. Can target single or multiple elements in a sequence. The scriptlet will abort after 10s timeout or after all the elements have been clicked. Also hidden elements can be targeted.

Parameters:

  • required, a list of comma-separated directives to process from left to right. Each directive can be:
    • a selector directive: a directive which provide information on which element to click in the web page.
      • A directive prefixed with when-visible: means that a matching element must be clicked only once it becomes visible.
      • A directive prefixed with xpath: means that instead of a plain CSS selector, the rest of the directive is an xpath expression, to be used to lookup a matching element.
      • Use >>> to select elements inside shadow root. (New in 1.54.1b3)
    • An integer: a delay in milliseconds to wait for the next directive to be processed.
  • optional, Extramatch: conditions for the clicking to occur, cookies and localStorage are matched using the format name:key[=value] where the value is optional. Multiple conditions can be separated by comma. Prefix with ! to only click if the cookie/localStorage item does not exist. Supported names: <sup>1.55.1b12</sup>
    • cookie: test string or regex against cookies
    • localStorage: test string or regex against localStorage
  • optional, delay in ms how long the scriptlet will postphone the execution

Examples:

  • example.com##+js(trusted-click-element, button.reject-all)
  • example.com##+js(trusted-click-element, '[for="CookiePurposes_1_"], [for="CookiePurposes_2_"], button.js-save')
  • example.com##+js(trusted-click-element, #cmpwrapper >>> .cmpboxbtnyes)
  • example.com##+js(trusted-click-element, #cmp-btn-accept, , 500)
  • example.com##+js(trusted-click-element, #cmp-btn-accept, !cookie:/^gpt_ppid[^=]+=/, 5000)
  • example.com##+js(trusted-click-element, button[name="agree"], 'cookie:cmpconsent, localStorage:promo=yes')
  • example.com##+js(trusted-click-element, when-visible:button.reject-all)
  • example.com##+js(trusted-click-element, 'xpath://*[@role="dialog"]//button[.//text()="Reject"]')

Also see: - AdGuard trusted-click-element


set-constant.js ↪

set.js

Creates property and initializes it with a value from a predefined set.

Scriptlet will succeed only when: - original property is undefined (scriptlet is called early enough) or null<sup>1.25.0</sup> OR - new property written by set.js is undefined or null<sup>1.25.0</sup> OR - type of original property is equal to type of new property

Value set by scriptlet can be overwritten by page script when: - current property was not set to undefined or null<sup>1.25.0</sup> AND - new property is not undefined or null<sup>1.25.0</sup> AND - type of original property is different than type of new property

Parameters: - required, property (chain of properties joined by .) attached to window object - required, possible values: - positive decimal integer, no sign, with maximum value of 0x7FFF (32767) - one value from set of predefined constants: - undefined - false - true - null<sup>2018-11-24</sup> - noopFunc - function with empty body - trueFunc - function returning true - falseFunc - function returning false - throwFunc - function throwing an error<sup>1.59.1rc5</sup> - '' - empty string<sup>2019-01-06</sup> - [] - empty array<sup>1.36</sup> - {} - empty object<sup>1.36</sup>

<details> <summary> ● optional (deprecated in favor of "runAt" token)</summary>

▬▬► SPOILER START ◄▬▬ - optional, to defer execution of set-constant, possible values: - not present: execute immediately - 1: execute immediately - interactive, end, 2: set the constant when the event DOMContentInteractive is fired - complete, idle, 3: set the constant when the event load is fired

▬▬► SPOILER END ◄▬▬

</details>

Tokens: - "runAt": when this parameter is present, uBO will take it into account to possibly defer execution of the scriptlet
new in 1.49.3b4, updated in 1.51.1rc0, solves uBlockOrigin/uAssets#7320: - "runAt" not present: execute immediately - loading or 1 : execute immediately - interactive or end or 2: set the constant when the event DOMContentLoaded is fired - complete or idle or 3: set the constant when the event load is fired - "as": when this parameter is present, uBO will take it into account how the values will be returned
new in 1.49.3b13, updated in 1.51.1rc0, solves uBlockOrigin/uBlock-issues#2615: - function: the constant will be a function returning the specified value (~~asFunction~~ is deprecated) - callback: the constant will be a function returning a function returning the specified value (~~asCallback~~ is deprecated) - resolved: the constant will be a promise resolving to the specified value (~~asResolved~~ is deprecated) - rejected: the constant will be a promise failing with the specified value (~~asRejected~~ is deprecated) - "stack": considered to be implemented in the future, ideally, AdGuard would support its stack parameter as a vararg, to be discussed,
issue to be solved: https://github.com/uBlockOrigin/uBlock-issues/issues/2783 - string or regular expression that must match the current function call stack trace, defaults to matching every call; if regular expression is invalid, it will be skipped