Skip to content

Commit

Permalink
Add trusted-replace-outbound-text scriptlet
Browse files Browse the repository at this point in the history
Related issue:
uBlockOrigin/uBlock-issues#3157

Paremeters:
- `pattern`: a string or regex to match in the outbound text. If
  not provided or empty, the scriptlet will only log the outbound
  text without modifying it.
- `replacement`: the replacement string for the matched part.
  • Loading branch information
gorhill committed Apr 1, 2024
1 parent 6876fa4 commit 21e1ee3
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -4704,3 +4704,38 @@ function trustedReplaceArgument(
}

/******************************************************************************/

builtinScriptlets.push({
name: 'trusted-replace-outbound-text.js',
requiresTrust: true,
fn: trustedReplaceOutboundText,
dependencies: [
'proxy-apply.fn',
'safe-self.fn',
],
});
function trustedReplaceOutboundText(
propChain = '',
pattern = '',
replacement = ''
) {
if ( propChain === '' ) { return; }
const safe = safeSelf();
const logPrefix = safe.makeLogPrefix('trusted-replace-outbound-text', propChain, pattern, replacement);
const rePattern = safe.patternToRegex(pattern);
const reflector = proxyApplyFn(propChain, function(...args) {
const textBefore = reflector(...args);
const textAfter = pattern !== ''
? textBefore.replace(rePattern, replacement)
: textBefore;
if ( textAfter !== textBefore ) {
safe.uboLog(logPrefix, 'Matched and replaced');
}
if ( safe.logLevel > 1 || pattern === '' ) {
safe.uboLog(logPrefix, 'Outbound text:\n', textAfter);
}
return textAfter;
});
}

/******************************************************************************/

0 comments on commit 21e1ee3

Please sign in to comment.