1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-08 12:57:57 +02:00

Improve logging abilities of object-prune.fn

This commit is contained in:
Raymond Hill 2023-08-09 11:05:53 -04:00
parent ec82dec757
commit cfac880cf5
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -639,7 +639,7 @@ function objectPrune(
stackNeedleDetails = { matchAll: true }, stackNeedleDetails = { matchAll: true },
extraArgs = {} extraArgs = {}
) { ) {
if ( typeof rawPrunePaths !== 'string' ) { return obj; } if ( typeof rawPrunePaths !== 'string' ) { return; }
const safe = safeSelf(); const safe = safeSelf();
const prunePaths = rawPrunePaths !== '' const prunePaths = rawPrunePaths !== ''
? rawPrunePaths.split(/ +/) ? rawPrunePaths.split(/ +/)
@ -648,11 +648,10 @@ function objectPrune(
? rawNeedlePaths.split(/ +/) ? rawNeedlePaths.split(/ +/)
: []; : [];
const logLevel = shouldLog({ log: rawPrunePaths === '' || extraArgs.log }); const logLevel = shouldLog({ log: rawPrunePaths === '' || extraArgs.log });
const log = logLevel ? ((...args) => { safe.uboLog(...args); }) : (( ) => { });
const reLogNeedle = safe.patternToRegex(logLevel === true ? rawNeedlePaths : ''); const reLogNeedle = safe.patternToRegex(logLevel === true ? rawNeedlePaths : '');
if ( stackNeedleDetails.matchAll !== true ) { if ( stackNeedleDetails.matchAll !== true ) {
if ( matchesStackTrace(stackNeedleDetails, extraArgs.logstack) === false ) { if ( matchesStackTrace(stackNeedleDetails, extraArgs.logstack) === false ) {
return obj; return;
} }
} }
if ( objectPrune.findOwner === undefined ) { if ( objectPrune.findOwner === undefined ) {
@ -666,15 +665,18 @@ function objectPrune(
if ( prune === false ) { if ( prune === false ) {
return owner.hasOwnProperty(chain); return owner.hasOwnProperty(chain);
} }
let modified = false;
if ( chain === '*' ) { if ( chain === '*' ) {
for ( const key in owner ) { for ( const key in owner ) {
if ( owner.hasOwnProperty(key) === false ) { continue; } if ( owner.hasOwnProperty(key) === false ) { continue; }
delete owner[key]; delete owner[key];
modified = true;
} }
} else if ( owner.hasOwnProperty(chain) ) { } else if ( owner.hasOwnProperty(chain) ) {
delete owner[chain]; delete owner[chain];
modified = true;
} }
return true; return modified;
} }
const prop = chain.slice(0, pos); const prop = chain.slice(0, pos);
if ( if (
@ -701,19 +703,28 @@ function objectPrune(
} }
return true; return true;
}; };
objectPrune.logJson = (json, logLevel, reNeedle) => {
if ( reNeedle.test(json) === false ) { return; }
safeSelf().uboLog(`objectPrune() log:${logLevel}`, location.hostname, json);
};
} }
const jsonBefore = logLevel ? JSON.stringify(obj, null, 2) : '';
if ( logLevel === true || logLevel === 'all' ) { if ( logLevel === true || logLevel === 'all' ) {
const json = JSON.stringify(obj, null, 2); objectPrune.logJson(jsonBefore, logLevel, reLogNeedle);
if ( reLogNeedle.test(json) ) { }
log(location.hostname, json); if ( prunePaths.length === 0 ) { return; }
let outcome = 'nomatch';
if ( objectPrune.mustProcess(obj, needlePaths) ) {
for ( const path of prunePaths ) {
if ( objectPrune.findOwner(obj, path, true) ) {
outcome = 'match';
}
} }
} }
if ( prunePaths.length === 0 ) { return obj; } if ( logLevel === outcome ) {
if ( objectPrune.mustProcess(obj, needlePaths) === false ) { return obj; } objectPrune.logJson(jsonBefore, logLevel, reLogNeedle);
for ( const path of prunePaths ) {
objectPrune.findOwner(obj, path, true);
} }
return obj; if ( outcome === 'match' ) { return obj; }
} }
/******************************************************************************/ /******************************************************************************/
@ -1214,16 +1225,15 @@ function jsonPrune(
) { ) {
JSON.parse = new Proxy(JSON.parse, { JSON.parse = new Proxy(JSON.parse, {
apply: function(target, thisArg, args) { apply: function(target, thisArg, args) {
if ( logLevel ) { const objBefore = Reflect.apply(target, thisArg, args);
safe.uboLog('json-prune / JSON.parse()'); const objAfter = objectPrune(
} objBefore,
return objectPrune(
Reflect.apply(target, thisArg, args),
rawPrunePaths, rawPrunePaths,
rawNeedlePaths, rawNeedlePaths,
stackNeedleDetails, stackNeedleDetails,
extraArgs extraArgs
); );
return objAfter || objBefore;
}, },
}); });
} }
@ -1244,13 +1254,16 @@ function jsonPrune(
); );
} }
if ( outcome === 'nomatch' ) { return dataPromise; } if ( outcome === 'nomatch' ) { return dataPromise; }
return dataPromise.then(data => objectPrune( return dataPromise.then(objBefore => {
data, const objAfter = objectPrune(
rawPrunePaths, objBefore,
rawNeedlePaths, rawPrunePaths,
stackNeedleDetails, rawNeedlePaths,
extraArgs stackNeedleDetails,
)); extraArgs
);
return objAfter || objBefore;
});
}, },
}); });
} }
@ -1272,11 +1285,12 @@ function evaldataPrune(
) { ) {
self.eval = new Proxy(self.eval, { self.eval = new Proxy(self.eval, {
apply(target, thisArg, args) { apply(target, thisArg, args) {
let data = Reflect.apply(target, thisArg, args); const before = Reflect.apply(target, thisArg, args);
if ( typeof data === 'object' ) { if ( typeof before === 'object' ) {
data = objectPrune(data, rawPrunePaths, rawNeedlePaths); const after = objectPrune(before, rawPrunePaths, rawNeedlePaths);
return after || before;
} }
return data; return before;
} }
}); });
} }