Skip to content

Commit 0dcb985

Browse files
committed
Improve trusted-replace-outbound-text scriptlet
When the replacement starts with `json:`, it will be first decoded using JSON.parse(). 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.
1 parent f5f042a commit 0dcb985

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

assets/resources/scriptlets.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4906,14 +4906,17 @@ builtinScriptlets.push({
49064906
});
49074907
function trustedReplaceOutboundText(
49084908
propChain = '',
4909-
pattern = '',
4910-
replacement = '',
4909+
rawPattern = '',
4910+
rawReplacement = '',
49114911
...args
49124912
) {
49134913
if ( propChain === '' ) { return; }
49144914
const safe = safeSelf();
4915-
const logPrefix = safe.makeLogPrefix('trusted-replace-outbound-text', propChain, pattern, replacement, ...args);
4916-
const rePattern = safe.patternToRegex(pattern);
4915+
const logPrefix = safe.makeLogPrefix('trusted-replace-outbound-text', propChain, rawPattern, rawReplacement, ...args);
4916+
const rePattern = safe.patternToRegex(rawPattern);
4917+
const replacement = rawReplacement.startsWith('json:')
4918+
? safe.JSON_parse(rawReplacement.slice(5))
4919+
: rawReplacement;
49174920
const extraArgs = safe.getExtraArgs(args);
49184921
const reCondition = safe.patternToRegex(extraArgs.condition || '');
49194922
const reflector = proxyApplyFn(propChain, function(...args) {
@@ -4923,7 +4926,7 @@ function trustedReplaceOutboundText(
49234926
try { textBefore = self.atob(encodedTextBefore); }
49244927
catch(ex) { return encodedTextBefore; }
49254928
}
4926-
if ( pattern === '' ) {
4929+
if ( rawPattern === '' ) {
49274930
safe.uboLog(logPrefix, 'Decoded outbound text:\n', textBefore);
49284931
return encodedTextBefore;
49294932
}

0 commit comments

Comments
 (0)