Skip to content

Commit 88fa550

Browse files
committed
Improve [json-prune|trusted-replace]-fetch-response scriptlets
Output more information to the logger in verbose mode.
1 parent 451e1c2 commit 88fa550

File tree

1 file changed

+24
-42
lines changed

1 file changed

+24
-42
lines changed

src/js/resources/scriptlets.js

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -625,34 +625,23 @@ builtinScriptlets.push({
625625
],
626626
});
627627
function matchObjectProperties(propNeedles, ...objs) {
628-
if ( matchObjectProperties.extractProperties === undefined ) {
629-
matchObjectProperties.extractProperties = (src, des, props) => {
630-
for ( const p of props ) {
631-
const v = src[p];
632-
if ( v === undefined ) { continue; }
633-
des[p] = src[p];
634-
}
635-
};
636-
}
637628
const safe = safeSelf();
638-
const haystack = {};
639-
const props = safe.Array_from(propNeedles.keys());
629+
const matched = [];
640630
for ( const obj of objs ) {
641631
if ( obj instanceof Object === false ) { continue; }
642-
matchObjectProperties.extractProperties(obj, haystack, props);
643-
}
644-
for ( const [ prop, details ] of propNeedles ) {
645-
let value = haystack[prop];
646-
if ( value === undefined ) { continue; }
647-
if ( typeof value !== 'string' ) {
648-
try { value = safe.JSON_stringify(value); }
649-
catch { }
650-
if ( typeof value !== 'string' ) { continue; }
651-
}
652-
if ( safe.testPattern(details, value) ) { continue; }
653-
return false;
632+
for ( const [ prop, details ] of propNeedles ) {
633+
let value = obj[prop];
634+
if ( value === undefined ) { continue; }
635+
if ( typeof value !== 'string' ) {
636+
try { value = safe.JSON_stringify(value); }
637+
catch { }
638+
if ( typeof value !== 'string' ) { continue; }
639+
}
640+
if ( safe.testPattern(details, value) === false ) { return; }
641+
matched.push(`${prop}: ${value}`);
642+
}
654643
}
655-
return true;
644+
return matched;
656645
}
657646

658647
/******************************************************************************/
@@ -679,7 +668,6 @@ function jsonPruneFetchResponseFn(
679668
const logall = rawPrunePaths === '';
680669
const applyHandler = function(target, thisArg, args) {
681670
const fetchPromise = Reflect.apply(target, thisArg, args);
682-
let outcome = logall ? 'nomatch' : 'match';
683671
if ( propNeedles.size !== 0 ) {
684672
const objs = [ args[0] instanceof Object ? args[0] : { url: args[0] } ];
685673
if ( objs[0] instanceof Request ) {
@@ -692,14 +680,12 @@ function jsonPruneFetchResponseFn(
692680
if ( args[1] instanceof Object ) {
693681
objs.push(args[1]);
694682
}
695-
if ( matchObjectProperties(propNeedles, ...objs) === false ) {
696-
outcome = 'nomatch';
683+
const matched = matchObjectProperties(propNeedles, ...objs);
684+
if ( matched === undefined ) { return fetchPromise; }
685+
if ( safe.logLevel > 1 ) {
686+
safe.uboLog(logPrefix, `Matched "propsToMatch":\n\t${matched.join('\n\t')}`);
697687
}
698688
}
699-
if ( logall === false && outcome === 'nomatch' ) { return fetchPromise; }
700-
if ( safe.logLevel > 1 && outcome !== 'nomatch' && propNeedles.size !== 0 ) {
701-
safe.uboLog(logPrefix, `Matched optional "propsToMatch"\n${extraArgs.propsToMatch}`);
702-
}
703689
return fetchPromise.then(responseBefore => {
704690
const response = responseBefore.clone();
705691
return response.json().then(objBefore => {
@@ -772,7 +758,6 @@ function replaceFetchResponseFn(
772758
apply: function(target, thisArg, args) {
773759
const fetchPromise = Reflect.apply(target, thisArg, args);
774760
if ( pattern === '' ) { return fetchPromise; }
775-
let outcome = 'match';
776761
if ( propNeedles.size !== 0 ) {
777762
const objs = [ args[0] instanceof Object ? args[0] : { url: args[0] } ];
778763
if ( objs[0] instanceof Request ) {
@@ -786,23 +771,20 @@ function replaceFetchResponseFn(
786771
if ( args[1] instanceof Object ) {
787772
objs.push(args[1]);
788773
}
789-
if ( matchObjectProperties(propNeedles, ...objs) === false ) {
790-
outcome = 'nomatch';
774+
const matched = matchObjectProperties(propNeedles, ...objs);
775+
if ( matched === undefined ) { return fetchPromise; }
776+
if ( safe.logLevel > 1 ) {
777+
safe.uboLog(logPrefix, `Matched "propsToMatch":\n\t${matched.join('\n\t')}`);
791778
}
792779
}
793-
if ( outcome === 'nomatch' ) { return fetchPromise; }
794-
if ( safe.logLevel > 1 ) {
795-
safe.uboLog(logPrefix, `Matched "propsToMatch"\n${propsToMatch}`);
796-
}
797780
return fetchPromise.then(responseBefore => {
798781
const response = responseBefore.clone();
799782
return response.text().then(textBefore => {
800783
if ( reIncludes && reIncludes.test(textBefore) === false ) {
801784
return responseBefore;
802785
}
803786
const textAfter = textBefore.replace(rePattern, replacement);
804-
const outcome = textAfter !== textBefore ? 'match' : 'nomatch';
805-
if ( outcome === 'nomatch' ) { return responseBefore; }
787+
if ( textAfter === textBefore ) { return responseBefore; }
806788
safe.uboLog(logPrefix, 'Replaced');
807789
const responseAfter = new Response(textAfter, {
808790
status: responseBefore.status,
@@ -1399,7 +1381,7 @@ function jsonPruneXhrResponse(
13991381
const xhrDetails = { method, url };
14001382
let outcome = 'match';
14011383
if ( propNeedles.size !== 0 ) {
1402-
if ( matchObjectProperties(propNeedles, xhrDetails) === false ) {
1384+
if ( matchObjectProperties(propNeedles, xhrDetails) === undefined ) {
14031385
outcome = 'nomatch';
14041386
}
14051387
}
@@ -2838,7 +2820,7 @@ function trustedReplaceXhrResponse(
28382820
const xhrDetails = { method, url };
28392821
let outcome = 'match';
28402822
if ( propNeedles.size !== 0 ) {
2841-
if ( matchObjectProperties(propNeedles, xhrDetails) === false ) {
2823+
if ( matchObjectProperties(propNeedles, xhrDetails) === undefined ) {
28422824
outcome = 'nomatch';
28432825
}
28442826
}

0 commit comments

Comments
 (0)