Skip to content

Commit

Permalink
Support logging details of calls to json-prune-fetch-response
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gorhill committed Feb 19, 2024
1 parent fef26e2 commit e527a8f
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -1198,10 +1198,10 @@ function jsonPruneFetchResponseFn(
const extraArgs = safe.getExtraArgs(Array.from(arguments), 2);
const propNeedles = parsePropertiesToMatch(extraArgs.propsToMatch, 'url');
const stackNeedle = safe.initPattern(extraArgs.stackToMatch || '', { canNegate: true });
const logall = rawPrunePaths === '';
const applyHandler = function(target, thisArg, args) {
const fetchPromise = Reflect.apply(target, thisArg, args);
if ( rawPrunePaths === '' ) { return fetchPromise; }
let outcome = 'match';
let outcome = logall ? 'nomatch' : 'match';
if ( propNeedles.size !== 0 ) {
const objs = [ args[0] instanceof Object ? args[0] : { url: args[0] } ];
if ( objs[0] instanceof Request ) {
Expand All @@ -1218,14 +1218,18 @@ function jsonPruneFetchResponseFn(
outcome = 'nomatch';
}
}
if ( outcome === 'nomatch' ) { return fetchPromise; }
if ( safe.logLevel > 1 ) {
if ( logall === false && outcome === 'nomatch' ) { return fetchPromise; }
if ( safe.logLevel > 1 && outcome !== 'nomatch' && propNeedles.size !== 0 ) {
safe.uboLog(logPrefix, `Matched optional "propsToMatch"\n${extraArgs.propsToMatch}`);
}
return fetchPromise.then(responseBefore => {
const response = responseBefore.clone();
return response.json().then(objBefore => {
if ( typeof objBefore !== 'object' ) { return responseBefore; }
if ( logall ) {
safe.uboLog(logPrefix, safe.JSON_stringify(objBefore, null, 2));
return responseBefore;
}
const objAfter = objectPruneFn(
objBefore,
rawPrunePaths,
Expand Down

0 comments on commit e527a8f

Please sign in to comment.