-
Notifications
You must be signed in to change notification settings - Fork 30
/
json-prune-fetch-response.ts
233 lines (218 loc) · 7.32 KB
/
json-prune-fetch-response.ts
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
import {
hit,
logMessage,
getFetchData,
objectToString,
matchRequestProps,
jsonPruner,
getPrunePath,
forgeResponse,
type FetchResource,
isPruningNeeded,
matchStackTrace,
toRegExp,
isValidStrPattern,
escapeRegExp,
isEmptyObject,
getRequestData,
getRequestProps,
parseMatchProps,
isValidParsedData,
getMatchPropsData,
getWildcardPropertyInChain,
shouldAbortInlineOrInjectedScript,
getNativeRegexpTest,
backupRegExpValues,
restoreRegExpValues,
} from '../helpers';
import { type Source } from './scriptlets';
/**
* @scriptlet json-prune-fetch-response
*
* @description
* Removes specified properties from the JSON response of a fetch call.
*
* Related UBO scriptlet:
* https://github.com/gorhill/uBlock/commit/749cec0f095f659d6c0b90eb89b729e9deb07c87
*
* ### Syntax
*
* ```text
* example.org#%#//scriptlet('json-prune-fetch-response'[, propsToRemove[, obligatoryProps[, propsToMatch[, 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
* - `propsToMatch` — optional, string of space-separated properties to match; possible props:
* - string or regular expression for matching the URL passed to fetch call;
* empty string, wildcard `*` or invalid regular expression will match all fetch calls
* - colon-separated pairs `name:value` where
* <!-- markdownlint-disable-next-line line-length -->
* - `name` is [`init` option name](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#parameters)
* - `value` is string or regular expression for matching the value of the option passed to fetch call;
* invalid regular expression will cause any value matching
* - `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`.
*
* > Usage with with only propsToMatch argument will log fetch calls to browser console.
* > It may be useful for debugging but it is not allowed for prod versions of filter lists.
*
* > Scriptlet does nothing if response body can't be converted to JSON.
*
* ### Examples
*
* 1. Removes property `example` from the JSON response of any fetch call
*
* ```adblock
* example.org#%#//scriptlet('json-prune-fetch-response', 'example')
* ```
*
* For instance, if the JSON response of a fetch call is:
*
* ```js
* {one: 1, example: true}
* ```
*
* then the response will be modified to:
*
* ```js
* {one: 1}
* ```
*
* 2. A property in a list of properties can be a chain of properties
*
* ```adblock
* example.org#%#//scriptlet('json-prune-fetch-response', 'a.b', 'ads.url.first')
* ```
*
* 3. Removes property `content.ad` from the JSON response of a fetch call if URL contains `content.json`
*
* ```adblock
* example.org#%#//scriptlet('json-prune-fetch-response', 'content.ad', '', 'content.json')
* ```
*
* 4. Removes property `content.ad` from the JSON response of a fetch call if its error stack trace contains `test.js`
*
* ```adblock
* example.org#%#//scriptlet('json-prune-fetch-response', 'content.ad', '', '', 'test.js')
* ```
*
* 5. A property in a list of properties can be a chain of properties with wildcard in it
*
* ```adblock
* example.org#%#//scriptlet('json-prune-fetch-response', 'content.*.media.src', 'content.*.media.ad')
* ```
*
* 6. Log all JSON responses of a fetch call
*
* ```adblock
* example.org#%#//scriptlet('json-prune-fetch-response')
* ```
*
* @added v1.10.25.
*/
export function jsonPruneFetchResponse(
source: Source,
propsToRemove: string,
obligatoryProps: string,
propsToMatch = '',
stack = '',
) {
// do nothing if browser does not support fetch or Proxy (e.g. Internet Explorer)
// https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
if (typeof fetch === 'undefined'
|| typeof Proxy === 'undefined'
|| typeof Response === 'undefined') {
return;
}
const prunePaths = getPrunePath(propsToRemove);
const requiredPaths = getPrunePath(obligatoryProps);
const nativeStringify = window.JSON.stringify;
const nativeRequestClone = window.Request.prototype.clone;
const nativeResponseClone = window.Response.prototype.clone;
const nativeFetch = window.fetch;
const fetchHandlerWrapper = async (
target: typeof fetch,
thisArg: any,
args: [FetchResource, RequestInit],
): Promise<Response> => {
const fetchData = getFetchData(args, nativeRequestClone);
if (!matchRequestProps(source, propsToMatch, fetchData)) {
return Reflect.apply(target, thisArg, args);
}
let originalResponse;
let clonedResponse;
try {
// eslint-disable-next-line prefer-spread
originalResponse = await nativeFetch.apply(null, args);
clonedResponse = nativeResponseClone.call(originalResponse);
} catch {
logMessage(source, `Could not make an original fetch request: ${fetchData.url}`);
return Reflect.apply(target, thisArg, args);
}
let json;
try {
json = await originalResponse.json();
} catch (e) {
const message = `Response body can't be converted to json: ${objectToString(fetchData)}`;
logMessage(source, message);
return clonedResponse;
}
const modifiedJson = jsonPruner(source, json, prunePaths, requiredPaths, stack, {
nativeStringify,
nativeRequestClone,
nativeResponseClone,
nativeFetch,
});
const forgedResponse = forgeResponse(
originalResponse,
nativeStringify(modifiedJson),
);
hit(source);
return forgedResponse;
};
const fetchHandler = {
apply: fetchHandlerWrapper,
};
window.fetch = new Proxy(window.fetch, fetchHandler);
}
export const jsonPruneFetchResponseNames = [
'json-prune-fetch-response',
// aliases are needed for matching the related scriptlet converted into our syntax
'json-prune-fetch-response.js',
'ubo-json-prune-fetch-response.js',
'ubo-json-prune-fetch-response',
];
// eslint-disable-next-line prefer-destructuring
jsonPruneFetchResponse.primaryName = jsonPruneFetchResponseNames[0];
jsonPruneFetchResponse.injections = [
hit,
logMessage,
getFetchData,
objectToString,
matchRequestProps,
jsonPruner,
getPrunePath,
forgeResponse,
isPruningNeeded,
matchStackTrace,
toRegExp,
isValidStrPattern,
escapeRegExp,
isEmptyObject,
getRequestData,
getRequestProps,
parseMatchProps,
isValidParsedData,
getMatchPropsData,
getWildcardPropertyInChain,
shouldAbortInlineOrInjectedScript,
getNativeRegexpTest,
backupRegExpValues,
restoreRegExpValues,
];