-
Notifications
You must be signed in to change notification settings - Fork 30
/
json-prune.js
167 lines (157 loc) · 5.03 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
import {
hit,
matchStackTrace,
getWildcardPropertyInChain,
logMessage,
isPruningNeeded,
jsonPruner,
getPrunePath,
toRegExp,
getNativeRegexpTest,
shouldAbortInlineOrInjectedScript,
backupRegExpValues,
restoreRegExpValues,
} 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')
* ```
*
* For instance, the following call will return `{ one: 1}`
*
* ```html
* JSON.parse('{"one":1,"example":true}')
* ```
*
* 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')
* ```
*
* For instance, the following call will return `{ one: 1, two: 2}`
*
* ```html
* JSON.parse('{"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')
* ```
*
* 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')
* ```
*
* 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')
* ```
*
* 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,
];