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.
This commit is contained in:
Raymond Hill 2024-08-27 12:49:35 -04:00
parent f5f042a6f0
commit 0dcb985601
No known key found for this signature in database
GPG key ID: 25E1490B761470C2

View file

@ -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;
}