From 0dcb9856013eba719d1b2bbc01b25039696593cf Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Tue, 27 Aug 2024 12:49:35 -0400 Subject: [PATCH] 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. --- assets/resources/scriptlets.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/assets/resources/scriptlets.js b/assets/resources/scriptlets.js index 0272ecce3..f450b88a9 100644 --- a/assets/resources/scriptlets.js +++ b/assets/resources/scriptlets.js @@ -4906,14 +4906,17 @@ builtinScriptlets.push({ }); function trustedReplaceOutboundText( propChain = '', - pattern = '', - replacement = '', + rawPattern = '', + rawReplacement = '', ...args ) { if ( propChain === '' ) { return; } const safe = safeSelf(); - const logPrefix = safe.makeLogPrefix('trusted-replace-outbound-text', propChain, pattern, replacement, ...args); - const rePattern = safe.patternToRegex(pattern); + const logPrefix = safe.makeLogPrefix('trusted-replace-outbound-text', propChain, rawPattern, rawReplacement, ...args); + const rePattern = safe.patternToRegex(rawPattern); + const replacement = rawReplacement.startsWith('json:') + ? safe.JSON_parse(rawReplacement.slice(5)) + : rawReplacement; const extraArgs = safe.getExtraArgs(args); const reCondition = safe.patternToRegex(extraArgs.condition || ''); const reflector = proxyApplyFn(propChain, function(...args) { @@ -4923,7 +4926,7 @@ function trustedReplaceOutboundText( try { textBefore = self.atob(encodedTextBefore); } catch(ex) { return encodedTextBefore; } } - if ( pattern === '' ) { + if ( rawPattern === '' ) { safe.uboLog(logPrefix, 'Decoded outbound text:\n', textBefore); return encodedTextBefore; }