Skip to content

Commit e527a8f

Browse files
committed
Support logging details of calls to json-prune-fetch-response
Example: example.com##+js(json-prune-fetch-response) This will output to the logger details of all fetch() with a Response.json() call. Related discussion: uBlockOrigin/uAssets#22556
1 parent fef26e2 commit e527a8f

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

assets/resources/scriptlets.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,10 +1198,10 @@ function jsonPruneFetchResponseFn(
11981198
const extraArgs = safe.getExtraArgs(Array.from(arguments), 2);
11991199
const propNeedles = parsePropertiesToMatch(extraArgs.propsToMatch, 'url');
12001200
const stackNeedle = safe.initPattern(extraArgs.stackToMatch || '', { canNegate: true });
1201+
const logall = rawPrunePaths === '';
12011202
const applyHandler = function(target, thisArg, args) {
12021203
const fetchPromise = Reflect.apply(target, thisArg, args);
1203-
if ( rawPrunePaths === '' ) { return fetchPromise; }
1204-
let outcome = 'match';
1204+
let outcome = logall ? 'nomatch' : 'match';
12051205
if ( propNeedles.size !== 0 ) {
12061206
const objs = [ args[0] instanceof Object ? args[0] : { url: args[0] } ];
12071207
if ( objs[0] instanceof Request ) {
@@ -1218,14 +1218,18 @@ function jsonPruneFetchResponseFn(
12181218
outcome = 'nomatch';
12191219
}
12201220
}
1221-
if ( outcome === 'nomatch' ) { return fetchPromise; }
1222-
if ( safe.logLevel > 1 ) {
1221+
if ( logall === false && outcome === 'nomatch' ) { return fetchPromise; }
1222+
if ( safe.logLevel > 1 && outcome !== 'nomatch' && propNeedles.size !== 0 ) {
12231223
safe.uboLog(logPrefix, `Matched optional "propsToMatch"\n${extraArgs.propsToMatch}`);
12241224
}
12251225
return fetchPromise.then(responseBefore => {
12261226
const response = responseBefore.clone();
12271227
return response.json().then(objBefore => {
12281228
if ( typeof objBefore !== 'object' ) { return responseBefore; }
1229+
if ( logall ) {
1230+
safe.uboLog(logPrefix, safe.JSON_stringify(objBefore, null, 2));
1231+
return responseBefore;
1232+
}
12291233
const objAfter = objectPruneFn(
12301234
objBefore,
12311235
rawPrunePaths,

0 commit comments

Comments
 (0)