mirror of
https://github.com/gorhill/uBlock.git
synced 2024-10-06 09:37:12 +02:00
Improve logging capabilities of json-prune scriptlet
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>.
This commit is contained in:
parent
96343ecf1a
commit
578594bbd7
@ -249,16 +249,35 @@
|
|||||||
|
|
||||||
|
|
||||||
/// json-prune.js
|
/// json-prune.js
|
||||||
|
//
|
||||||
|
// When no "prune paths" argument is provided, the scriptlet is
|
||||||
|
// used for logging purpose and the "needle paths" argument is
|
||||||
|
// used to filter logging output.
|
||||||
(function() {
|
(function() {
|
||||||
const log = console.log.bind(console);
|
|
||||||
const rawPrunePaths = '{{1}}';
|
const rawPrunePaths = '{{1}}';
|
||||||
const rawNeedlePaths = '{{2}}';
|
const rawNeedlePaths = '{{2}}';
|
||||||
const prunePaths = rawPrunePaths !== '{{1}}' && rawPrunePaths !== ''
|
const prunePaths = rawPrunePaths !== '{{1}}' && rawPrunePaths !== ''
|
||||||
? rawPrunePaths.split(/ +/)
|
? rawPrunePaths.split(/ +/)
|
||||||
: [];
|
: [];
|
||||||
const needlePaths = rawNeedlePaths !== '{{2}}' && rawNeedlePaths !== ''
|
let needlePaths;
|
||||||
|
let log, reLogNeedle;
|
||||||
|
if ( prunePaths.length !== 0 ) {
|
||||||
|
needlePaths = prunePaths.length !== 0 &&
|
||||||
|
rawNeedlePaths !== '{{2}}' && rawNeedlePaths !== ''
|
||||||
? rawNeedlePaths.split(/ +/)
|
? 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) {
|
const findOwner = function(root, path) {
|
||||||
let owner = root;
|
let owner = root;
|
||||||
let chain = path;
|
let chain = path;
|
||||||
@ -286,8 +305,11 @@
|
|||||||
JSON.parse = new Proxy(JSON.parse, {
|
JSON.parse = new Proxy(JSON.parse, {
|
||||||
apply: function() {
|
apply: function() {
|
||||||
const r = Reflect.apply(...arguments);
|
const r = Reflect.apply(...arguments);
|
||||||
if ( prunePaths.length === 0 ) {
|
if ( log !== undefined ) {
|
||||||
log(location.hostname, r);
|
const json = JSON.stringify(r, null, 2);
|
||||||
|
if ( reLogNeedle.test(json) ) {
|
||||||
|
log(location.hostname, json);
|
||||||
|
}
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
if ( mustProcess(r) === false ) { return r; }
|
if ( mustProcess(r) === false ) { return r; }
|
||||||
|
Loading…
Reference in New Issue
Block a user