|
236 | 236 | })();
|
237 | 237 |
|
238 | 238 |
|
| 239 | +/// json-prune.js |
| 240 | +(function() { |
| 241 | + const log = console.log.bind(console); |
| 242 | + const rawPrunePaths = '{{1}}'; |
| 243 | + const rawNeedlePaths = '{{2}}'; |
| 244 | + const prunePaths = rawPrunePaths !== '{{1}}' && rawPrunePaths !== '' |
| 245 | + ? rawPrunePaths.split(/ +/) |
| 246 | + : []; |
| 247 | + const needlePaths = rawNeedlePaths !== '{{2}}' && rawNeedlePaths !== '' |
| 248 | + ? rawNeedlePaths.split(/ +/) |
| 249 | + : []; |
| 250 | + const findOwner = function(root, path) { |
| 251 | + let owner = root; |
| 252 | + let chain = path; |
| 253 | + for (;;) { |
| 254 | + if ( owner instanceof Object === false ) { return; } |
| 255 | + const pos = chain.indexOf('.'); |
| 256 | + if ( pos === -1 ) { |
| 257 | + return owner.hasOwnProperty(chain) |
| 258 | + ? [ owner, chain ] |
| 259 | + : undefined; |
| 260 | + } |
| 261 | + const prop = chain.slice(0, pos); |
| 262 | + if ( owner.hasOwnProperty(prop) === false ) { return; } |
| 263 | + owner = owner[prop]; |
| 264 | + chain = chain.slice(pos + 1); |
| 265 | + } |
| 266 | + }; |
| 267 | + const mustProcess = function(root) { |
| 268 | + for ( const needlePath of needlePaths ) { |
| 269 | + const details = findOwner(root, needlePath); |
| 270 | + if ( details === undefined ) { return false; } |
| 271 | + } |
| 272 | + return true; |
| 273 | + }; |
| 274 | + JSON.parse = new Proxy(JSON.parse, { |
| 275 | + apply: function() { |
| 276 | + const r = Reflect.apply(...arguments); |
| 277 | + if ( prunePaths.length === 0 ) { |
| 278 | + log(location.hostname, r); |
| 279 | + return r; |
| 280 | + } |
| 281 | + if ( mustProcess(r) === false ) { return r; } |
| 282 | + for ( const path of prunePaths ) { |
| 283 | + const details = findOwner(r, path); |
| 284 | + if ( details !== undefined ) { |
| 285 | + delete details[0][details[1]]; |
| 286 | + } |
| 287 | + } |
| 288 | + return r; |
| 289 | + }, |
| 290 | + }); |
| 291 | +})(); |
| 292 | + |
| 293 | + |
239 | 294 | // Imported from:
|
240 | 295 | // https://github.com/NanoAdblocker/NanoFilters/blob/1f3be7211bb0809c5106996f52564bf10c4525f7/NanoFiltersSource/NanoResources.txt#L126
|
241 | 296 | //
|
|
0 commit comments