|
249 | 249 |
|
250 | 250 |
|
251 | 251 | /// json-prune.js
|
| 252 | +// |
| 253 | +// When no "prune paths" argument is provided, the scriptlet is |
| 254 | +// used for logging purpose and the "needle paths" argument is |
| 255 | +// used to filter logging output. |
252 | 256 | (function() {
|
253 |
| - const log = console.log.bind(console); |
254 | 257 | const rawPrunePaths = '{{1}}';
|
255 | 258 | const rawNeedlePaths = '{{2}}';
|
256 | 259 | const prunePaths = rawPrunePaths !== '{{1}}' && rawPrunePaths !== ''
|
257 | 260 | ? rawPrunePaths.split(/ +/)
|
258 | 261 | : [];
|
259 |
| - const needlePaths = rawNeedlePaths !== '{{2}}' && rawNeedlePaths !== '' |
260 |
| - ? rawNeedlePaths.split(/ +/) |
261 |
| - : []; |
| 262 | + let needlePaths; |
| 263 | + let log, reLogNeedle; |
| 264 | + if ( prunePaths.length !== 0 ) { |
| 265 | + needlePaths = prunePaths.length !== 0 && |
| 266 | + rawNeedlePaths !== '{{2}}' && rawNeedlePaths !== '' |
| 267 | + ? rawNeedlePaths.split(/ +/) |
| 268 | + : []; |
| 269 | + } else { |
| 270 | + log = console.log.bind(console); |
| 271 | + let needle; |
| 272 | + if ( rawNeedlePaths === '' || rawNeedlePaths === '{{2}}' ) { |
| 273 | + needle = '.?'; |
| 274 | + } else if ( rawNeedlePaths.charAt(0) === '/' && rawNeedlePaths.slice(-1) === '/' ) { |
| 275 | + needle = rawNeedlePaths.slice(1, -1); |
| 276 | + } else { |
| 277 | + needle = rawNeedlePaths.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
| 278 | + } |
| 279 | + reLogNeedle = new RegExp(needle); |
| 280 | + } |
262 | 281 | const findOwner = function(root, path) {
|
263 | 282 | let owner = root;
|
264 | 283 | let chain = path;
|
|
286 | 305 | JSON.parse = new Proxy(JSON.parse, {
|
287 | 306 | apply: function() {
|
288 | 307 | const r = Reflect.apply(...arguments);
|
289 |
| - if ( prunePaths.length === 0 ) { |
290 |
| - log(location.hostname, r); |
| 308 | + if ( log !== undefined ) { |
| 309 | + const json = JSON.stringify(r, null, 2); |
| 310 | + if ( reLogNeedle.test(json) ) { |
| 311 | + log(location.hostname, json); |
| 312 | + } |
291 | 313 | return r;
|
292 | 314 | }
|
293 | 315 | if ( mustProcess(r) === false ) { return r; }
|
|
0 commit comments