1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-03 02:37:21 +02:00

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
This commit is contained in:
Raymond Hill 2024-02-19 10:59:12 -05:00
parent fef26e234d
commit e527a8f9af
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -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,