Skip to content

Commit

Permalink
Improve logging capabilities of json-prune scriptlet
Browse files Browse the repository at this point in the history
Specifically:

- Log entries as received by client code
- Prettier and more readable console output
- Ability to only log entries matching a
  specific needle

As per internal discussion at
<https://github.com/uBlockOrigin/uAssets>; limited
logging capabilities of json-prune originally raised
by <https://github.com/gwarser>.
  • Loading branch information
gorhill authored and JustOff committed Jun 14, 2020
1 parent ae2114d commit b981881
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions assets/resources/resources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1374,15 +1374,30 @@ addEventListener-logger.js application/javascript
# https://github.com/gorhill/uBlock/commit/2fd86a66fcc2665e5672cc5862e24b3782ee7504
json-prune.js application/javascript
(function() {
const log = console.log.bind(console);
const rawPrunePaths = '{{1}}';
const rawNeedlePaths = '{{2}}';
const prunePaths = rawPrunePaths !== '{{1}}' && rawPrunePaths !== ''
? rawPrunePaths.split(/ +/)
: [];
const needlePaths = rawNeedlePaths !== '{{2}}' && rawNeedlePaths !== ''
? rawNeedlePaths.split(/ +/)
: [];
let needlePaths;
let log, reLogNeedle;
if ( prunePaths.length !== 0 ) {
needlePaths = prunePaths.length !== 0 &&
rawNeedlePaths !== '{{2}}' && rawNeedlePaths !== ''
? rawNeedlePaths.split(/ +/)
: [];
} else {
log = console.log.bind(console);
let needle;
if ( rawNeedlePaths === '' || rawNeedlePaths === '{{2}}' ) {
needle = '.?';
} else if ( rawNeedlePaths.charAt(0) === '/' && rawNeedlePaths.slice(-1) === '/' ) {
needle = rawNeedlePaths.slice(1, -1);
} else {
needle = rawNeedlePaths.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
reLogNeedle = new RegExp(needle);
}
const findOwner = function(root, path) {
let owner = root;
let chain = path;
Expand Down Expand Up @@ -1410,8 +1425,11 @@ json-prune.js application/javascript
JSON.parse = new Proxy(JSON.parse, {
apply: function() {
const r = Reflect.apply(...arguments);
if ( prunePaths.length === 0 ) {
log(location.hostname, r);
if ( log !== undefined ) {
const json = JSON.stringify(r, null, 2);
if ( reLogNeedle.test(json) ) {
log(location.hostname, json);
}
return r;
}
if ( mustProcess(r) === false ) { return r; }
Expand Down

0 comments on commit b981881

Please sign in to comment.