Skip to content

Commit 21e1ee3

Browse files
committed
Add trusted-replace-outbound-text scriptlet
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.
1 parent 6876fa4 commit 21e1ee3

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

assets/resources/scriptlets.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4704,3 +4704,38 @@ function trustedReplaceArgument(
47044704
}
47054705

47064706
/******************************************************************************/
4707+
4708+
builtinScriptlets.push({
4709+
name: 'trusted-replace-outbound-text.js',
4710+
requiresTrust: true,
4711+
fn: trustedReplaceOutboundText,
4712+
dependencies: [
4713+
'proxy-apply.fn',
4714+
'safe-self.fn',
4715+
],
4716+
});
4717+
function trustedReplaceOutboundText(
4718+
propChain = '',
4719+
pattern = '',
4720+
replacement = ''
4721+
) {
4722+
if ( propChain === '' ) { return; }
4723+
const safe = safeSelf();
4724+
const logPrefix = safe.makeLogPrefix('trusted-replace-outbound-text', propChain, pattern, replacement);
4725+
const rePattern = safe.patternToRegex(pattern);
4726+
const reflector = proxyApplyFn(propChain, function(...args) {
4727+
const textBefore = reflector(...args);
4728+
const textAfter = pattern !== ''
4729+
? textBefore.replace(rePattern, replacement)
4730+
: textBefore;
4731+
if ( textAfter !== textBefore ) {
4732+
safe.uboLog(logPrefix, 'Matched and replaced');
4733+
}
4734+
if ( safe.logLevel > 1 || pattern === '' ) {
4735+
safe.uboLog(logPrefix, 'Outbound text:\n', textAfter);
4736+
}
4737+
return textAfter;
4738+
});
4739+
}
4740+
4741+
/******************************************************************************/

0 commit comments

Comments
 (0)