-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathtrusted-json-set-fetch-response.ts
More file actions
277 lines (257 loc) · 8.25 KB
/
trusted-json-set-fetch-response.ts
File metadata and controls
277 lines (257 loc) · 8.25 KB
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
import {
logMessage,
getFetchData,
objectToString,
matchRequestProps,
jsonSetter,
getPrunePath,
forgeResponse,
type FetchResource,
isPruningNeeded,
matchStackTrace,
toRegExp,
isValidStrPattern,
escapeRegExp,
isEmptyObject,
getRequestData,
getRequestProps,
parseMatchProps,
isValidParsedData,
getMatchPropsData,
getWildcardPropertyInChain,
shouldAbortInlineOrInjectedScript,
getNativeRegexpTest,
backupRegExpValues,
restoreRegExpValues,
isKeyInObject,
noopArray,
noopObject,
noopCallbackFunc,
noopFunc,
trueFunc,
falseFunc,
throwFunc,
noopPromiseReject,
noopPromiseResolve,
nativeIsNaN,
extractRegexAndReplacement,
getJsonSetValue,
hit,
parseJsonSetArgumentValue,
} from '../helpers';
import { type Source } from './scriptlets';
/* eslint-disable max-len */
/**
* @trustedScriptlet trusted-json-set-fetch-response
*
* @description
* Sets a property at the given path in the JSON response of a fetch call.
* If the path does not exist, it is created together with any missing intermediate objects.
*
* ### Syntax
*
* <!-- markdownlint-disable line-length -->
*
* ```text
* example.org#%#//scriptlet('trusted-json-set-fetch-response', propsPath, argumentValue[, requiredInitialProps[, propsToMatch[, stack[, verbose]]]])
* ```
*
* <!-- markdownlint-enable line-length -->
*
* - `propsPath` — required, dot-separated path to the property to set.
* Supports wildcards `*` and `[]`, and value filtering with `.[=].value`.
* - `argumentValue` — required, value to write at the target path.
* Supports the same constants, `json:{...}`, and `replace:/regex/replacement/` syntax
* as `trusted-json-set`.
* - `requiredInitialProps` — optional, space-separated list of property paths
* which must all be present for the modification 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;
* - colon-separated pairs `name:value` for matching fetch init options.
* - `stack` — optional, string or regular expression that must match the current function call stack trace.
* - `verbose` — optional, if set to `true`, the scriptlet will log the original and modified JSON content.
*
* > Scriptlet does nothing if response body cannot be converted to JSON.
*
* ### Examples
*
* 1. Sets `ads.enabled` to `false` in the JSON response of any fetch call
*
* ```adblock
* example.org#%#//scriptlet('trusted-json-set-fetch-response', 'ads.enabled', 'false')
* ```
*
* 1. Creates `config.flags.blocked` path in matching fetch responses
*
* ```adblock
* example.org#%#//scriptlet('trusted-json-set-fetch-response', 'config.flags.blocked', 'true', '', 'api/config')
* ```
*
* 1. Merges a parsed JSON object into an existing response object property
*
* ```adblock
* example.org#%#//scriptlet('trusted-json-set-fetch-response', 'foo', 'json:{"a":{"test":1},"b":{"c":1}}')
* ```
*
* 1. Replaces a value in the JSON response using a regular expression
*
* ```adblock
* example.org#%#//scriptlet('trusted-json-set-fetch-response', 'foo', 'replace:/advertisement/article/')
* ```
*
* @added v2.3.0.
*/
/* eslint-enable max-len */
export function trustedJsonSetFetchResponse(
source: Source,
propsPath: string,
argumentValue: any,
requiredInitialProps = '',
propsToMatch = '',
stack = '',
verbose = '',
) {
if (!propsPath || argumentValue === undefined) {
return;
}
if (
typeof fetch === 'undefined'
|| typeof Proxy === 'undefined'
|| typeof Response === 'undefined'
) {
return;
}
const parsedArgumentValue = parseJsonSetArgumentValue(
source,
argumentValue,
window.JSON.parse,
);
if (!parsedArgumentValue) {
return;
}
const shouldLogContent = verbose === 'true';
const parsedSetPaths = getPrunePath(propsPath);
const setPathObj = parsedSetPaths[0];
const requiredPaths = getPrunePath(requiredInitialProps);
const nativeStringify = window.JSON.stringify;
const nativeRequestClone = window.Request.prototype.clone;
const nativeResponseClone = window.Response.prototype.clone;
const nativeFetch = window.fetch;
const getValueToSet = (currentValue: any): any => getJsonSetValue(currentValue, parsedArgumentValue);
// TODO: Consider to move it to helper and share it with json-prune-fetch-response scriptlet
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();
if (shouldLogContent) {
// eslint-disable-next-line max-len
logMessage(source, `Original content:\n${window.location.hostname}\n${nativeStringify(json, null, 2)}\nStack trace:\n${new Error().stack || ''}`, true);
logMessage(source, json, true, false);
}
} catch {
const message = `Response body can't be converted to json: ${objectToString(fetchData)}`;
logMessage(source, message);
return clonedResponse;
}
const modifiedJson = jsonSetter(
source,
json,
setPathObj?.path || '',
setPathObj?.value,
getValueToSet,
requiredPaths,
stack,
{
nativeStringify,
nativeRequestClone,
nativeResponseClone,
nativeFetch,
},
);
if (shouldLogContent) {
// eslint-disable-next-line max-len
logMessage(source, `Modified content:\n${window.location.hostname}\n${nativeStringify(modifiedJson, null, 2)}\nStack trace:\n${new Error().stack || ''}`, true);
logMessage(source, modifiedJson, true, false);
}
return forgeResponse(
originalResponse,
nativeStringify(modifiedJson),
);
};
const getWrapper = (target: typeof fetch, propName: string, receiver: any) => {
if (propName === 'toString') {
return target.toString.bind(target);
}
return Reflect.get(target, propName, receiver);
};
const fetchHandler = {
apply: fetchHandlerWrapper,
get: getWrapper,
};
window.fetch = new Proxy(window.fetch, fetchHandler);
}
export const trustedJsonSetFetchResponseNames = [
'trusted-json-set-fetch-response',
// trusted scriptlets support no aliases
];
// eslint-disable-next-line prefer-destructuring
trustedJsonSetFetchResponse.primaryName = trustedJsonSetFetchResponseNames[0];
trustedJsonSetFetchResponse.injections = [
logMessage,
getFetchData,
objectToString,
matchRequestProps,
jsonSetter,
getPrunePath,
forgeResponse,
isPruningNeeded,
matchStackTrace,
toRegExp,
isValidStrPattern,
escapeRegExp,
isEmptyObject,
getRequestData,
getRequestProps,
parseMatchProps,
isValidParsedData,
getMatchPropsData,
getWildcardPropertyInChain,
shouldAbortInlineOrInjectedScript,
getNativeRegexpTest,
backupRegExpValues,
restoreRegExpValues,
isKeyInObject,
noopArray,
noopObject,
noopCallbackFunc,
noopFunc,
trueFunc,
falseFunc,
throwFunc,
noopPromiseReject,
noopPromiseResolve,
nativeIsNaN,
extractRegexAndReplacement,
getJsonSetValue,
hit,
parseJsonSetArgumentValue,
];