-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathjson-prune.js
391 lines (381 loc) · 8.6 KB
/
json-prune.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import {
hit,
matchStackTrace,
getWildcardPropertyInChain,
logMessage,
isPruningNeeded,
jsonPruner,
getPrunePath,
toRegExp,
getNativeRegexpTest,
shouldAbortInlineOrInjectedScript,
backupRegExpValues,
restoreRegExpValues,
nativeIsNaN,
isKeyInObject,
} from '../helpers';
/* eslint-disable max-len */
/**
* @scriptlet json-prune
*
* @description
* Removes specified properties from the result of calling JSON.parse and returns the caller.
*
* Related UBO scriptlet:
* https://github.com/gorhill/uBlock/wiki/Resources-Library#json-prunejs-
*
* Related ABP source:
* https://gitlab.com/eyeo/snippets/-/blob/main/source/behavioral/json-prune.js
*
* ### Syntax
*
* ```text
* example.org#%#//scriptlet('json-prune'[, propsToRemove [, obligatoryProps [, stack]]])
* ```
*
* - `propsToRemove` — optional, string of space-separated properties to remove
* - `obligatoryProps` — optional, string of space-separated properties
* which must be all present for the pruning to occur
* - `stack` — optional, string or regular expression that must match the current function call stack trace;
* if regular expression is invalid it will be skipped
*
* > Note please that you can use wildcard `*` for chain property name,
* > e.g. `ad.*.src` instead of `ad.0.src ad.1.src ad.2.src`.
*
* ### Examples
*
* 1. Removes property `example` from the results of JSON.parse call
*
* ```adblock
* example.org#%#//scriptlet('json-prune', 'example')
* ```
*
* JSON.parse call:
*
* ```html
* JSON.parse('{"one":1,"example":true}')
* ```
*
* Input JSON:
*
* ```json
* {
* "one": 1,
* "example": true
* }
* ```
*
* Output:
*
* ```json
* {
* "one": 1
* }
* ```
*
* 1. If there are no specified properties in the result of JSON.parse call, pruning will NOT occur
*
* ```adblock
* example.org#%#//scriptlet('json-prune', 'one', 'obligatoryProp')
* ```
*
* JSON.parse call:
*
* ```html
* JSON.parse('{"one":1,"two":2}')
* ```
*
* Input JSON:
*
* ```json
* {
* "one": 1,
* "two": 2
* }
* ```
*
* Output:
*
* ```json
* {
* "one": 1,
* "two": 2
* }
* ```
*
* 1. A property in a list of properties can be a chain of properties
*
* ```adblock
* example.org#%#//scriptlet('json-prune', 'a.b', 'ads.url.first')
* ```
*
* JSON.parse call:
*
* ```html
* JSON.parse('{"a":{"b":123},"ads":{"url":{"first":"abc"}}}')
* ```
*
* Input JSON:
*
* ```json
* {
* "a": {
* "b": 123
* },
* "ads": {
* "url": {
* "first": "abc"
* }
* }
* }
* ```
*
* Output:
*
* ```json
* {
* "a": {},
* "ads": {
* "url": {
* "first": "abc"
* }
* }
* }
* ```
*
* 1. Removes property `content.ad` from the results of JSON.parse call if its error stack trace contains `test.js`
*
* ```adblock
* example.org#%#//scriptlet('json-prune', 'content.ad', '', 'test.js')
* ```
*
* JSON.parse call:
*
* ```html
* JSON.parse('{"content":{"ad":{"src":"a.js"}}}')
* ```
*
* Input JSON:
*
* ```json
* {
* "content": {
* "ad": {
* "src": "a.js"
* }
* }
* }
* ```
*
* Output:
*
* ```json
* {
* "content": {}
* }
* ```
*
* 1. A property in a list of properties can be a chain of properties with wildcard in it
*
* ```adblock
* example.org#%#//scriptlet('json-prune', 'content.*.media.src', 'content.*.media.ad')
* ```
*
* JSON.parse call:
*
* ```html
* JSON.parse('{"content":{"block1":{"media":{"src":"1.jpg","ad":true}},"block2":{"media":{"src":"2.jpg"}}}}')
* ```
*
* Input JSON:
*
* ```json
* {
* "content": {
* "block1": {
* "media": {
* "src": "1.jpg",
* "ad": true
* }
* },
* "block2": {
* "media": {
* "src": "2.jpg"
* }
* }
* }
* }
* ```
*
* Output:
*
* ```json
* {
* "content": {
* "block1": {
* "media": {
* "ad": true
* }
* },
* "block2": {
* "media": {}
* }
* }
* }
* ```
*
* 1. Removes every property from `videos` object if it has `isAd` key
*
* ```adblock
* example.org#%#//scriptlet('json-prune', 'videos.{-}.isAd')
* ```
*
* JSON.parse call:
*
* ```html
* JSON.parse('{"videos":{"video1":{"isAd":true,"src":"video1.mp4"},"video2":{"src":"video1.mp4"}}}')
* ```
*
* Input JSON:
*
* ```json
* {
* "videos": {
* "video1": {
* "isAd": true,
* "src": "video1.mp4"
* },
* "video2": {
* "src": "video1.mp4"
* }
* }
* }
* ```
*
* Output:
*
* ```json
* {
* "videos": {
* "video2": {
* "src": "video1.mp4"
* }
* }
* }
* ```
*
* 1. Removes every property from `videos` object if it has `isAd` key with `true` value
*
* ```adblock
* example.org#%#//scriptlet('json-prune', 'videos.{-}.isAd.[=].true')
* ```
*
* JSON.parse call:
*
* ```html
* JSON.parse('{"videos":{"video1":{"isAd":true,"src":"video1.mp4"},"video2":{"isAd":false,"src":"video1.mp4"}}}')
* ```
*
* Input JSON:
*
* ```json
* {
* "videos": {
* "video1": {
* "isAd": true,
* "src": "video1.mp4"
* },
* "video2": {
* "isAd": false,
* "src": "video1.mp4"
* }
* }
* }
* ```
*
* Output:
*
* ```json
* {
* "videos": {
* "video2": {
* "isAd": false,
* "src": "video1.mp4"
* }
* }
* }
* ```
*
* 1. Call with no arguments will log the current hostname and JSON payload at the console
*
* ```adblock
* example.org#%#//scriptlet('json-prune')
* ```
*
* 1. Call with only second argument will log the current hostname and matched json payload at the console
*
* ```adblock
* example.org#%#//scriptlet('json-prune', '', '"id":"117458"')
* ```
*
* @added v1.1.0.
*/
/* eslint-enable max-len */
export function jsonPrune(source, propsToRemove, requiredInitialProps, stack = '') {
const prunePaths = getPrunePath(propsToRemove);
const requiredPaths = getPrunePath(requiredInitialProps);
const nativeObjects = {
nativeStringify: window.JSON.stringify,
};
const nativeJSONParse = JSON.parse;
const jsonParseWrapper = (...args) => {
// dealing with stringified json in args, which should be parsed.
// so we call nativeJSONParse as JSON.parse which is bound to JSON object
const root = nativeJSONParse.apply(JSON, args);
return jsonPruner(source, root, prunePaths, requiredPaths, stack, nativeObjects);
};
// JSON.parse mocking
jsonParseWrapper.toString = nativeJSONParse.toString.bind(nativeJSONParse);
JSON.parse = jsonParseWrapper;
const nativeResponseJson = Response.prototype.json;
// eslint-disable-next-line func-names
const responseJsonWrapper = function () {
const promise = nativeResponseJson.apply(this);
return promise.then((obj) => {
return jsonPruner(source, obj, prunePaths, requiredPaths, stack, nativeObjects);
});
};
// do nothing if browser does not support Response (e.g. Internet Explorer)
// https://developer.mozilla.org/en-US/docs/Web/API/Response
if (typeof Response === 'undefined') {
return;
}
Response.prototype.json = responseJsonWrapper;
}
export const jsonPruneNames = [
'json-prune',
// aliases are needed for matching the related scriptlet converted into our syntax
'json-prune.js',
'ubo-json-prune.js',
'ubo-json-prune',
'abp-json-prune',
];
// eslint-disable-next-line prefer-destructuring
jsonPrune.primaryName = jsonPruneNames[0];
jsonPrune.injections = [
hit,
matchStackTrace,
getWildcardPropertyInChain,
logMessage,
isPruningNeeded,
jsonPruner,
getPrunePath,
// following helpers are needed for helpers above
toRegExp,
getNativeRegexpTest,
shouldAbortInlineOrInjectedScript,
backupRegExpValues,
restoreRegExpValues,
nativeIsNaN,
isKeyInObject,
];