Skip to content

Commit

Permalink
Improve trusted-replace-outbound-text scriptlet
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gorhill committed Aug 27, 2024
1 parent f5f042a commit 0dcb985
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down

0 comments on commit 0dcb985

Please sign in to comment.