From e527a8f9af7f9ca8f25f0cff19646d77d85a4edb Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Mon, 19 Feb 2024 10:59:12 -0500 Subject: [PATCH] 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: https://github.com/uBlockOrigin/uAssets/discussions/22556 --- assets/resources/scriptlets.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/assets/resources/scriptlets.js b/assets/resources/scriptlets.js index 2ffdf6172636b..9b079e68d6ff7 100644 --- a/assets/resources/scriptlets.js +++ b/assets/resources/scriptlets.js @@ -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 ) { @@ -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,