-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtrusted-replace-xhr-response.js
295 lines (262 loc) · 10.3 KB
/
trusted-replace-xhr-response.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
import {
hit,
logMessage,
toRegExp,
objectToString,
matchRequestProps,
getXhrData,
getMatchPropsData,
getRequestProps,
isValidParsedData,
parseMatchProps,
isValidStrPattern,
escapeRegExp,
isEmptyObject,
} from '../helpers';
/* eslint-disable max-len */
/**
* @trustedScriptlet trusted-replace-xhr-response
*
* @description
* Replaces response content of `xhr` requests if **all** given parameters match.
*
* ### Syntax
*
* ```text
* example.org#%#//scriptlet('trusted-replace-xhr-response'[, pattern, replacement[, propsToMatch]])
* ```
*
* - `pattern` — optional, argument for matching contents of responseText that should be replaced.
* If set, `replacement` is required. Possible values:
* - `*` to match all text content
* - non-empty string
* - regular expression
* By default only first occurrence is replaced. To replace all occurrences use `g` flag in RegExp - `/pattern/g`.
* - `replacement` — optional, should be set if `pattern` is set. String to replace matched content with.
* Empty string to remove content.
* - `propsToMatch` — optional, string of space-separated properties to match for extra condition; possible props:
* - string or regular expression for matching the URL passed to `XMLHttpRequest.open()` call;
* - colon-separated pairs `name:value` where
* - `name` — string or regular expression for matching XMLHttpRequest property name
* - `value` — string or regular expression for matching the value of the option
* passed to `XMLHttpRequest.open()` call
* - `verbose` — optional, boolean, if set to 'true' will log original and modified text content of XMLHttpRequests.
*
* > `verbose` may be useful for debugging but it is not allowed for prod versions of filter lists.
*
* > Usage with no arguments will log XMLHttpRequest objects to browser console;
* > it may be useful for debugging but it is not allowed for prod versions of filter lists.
*
* ### Examples
*
* 1. Log all XMLHttpRequests
*
* ```adblock
* example.org#%#//scriptlet('trusted-replace-xhr-response')
* ```
*
* 1. Replace text content of XMLHttpRequests with specific url
*
* <!-- markdownlint-disable line-length -->
*
* ```adblock
* example.org#%#//scriptlet('trusted-replace-xhr-response', 'adb_detect:true', 'adb_detect:false', 'example.org')
* example.org#%#//scriptlet('trusted-replace-xhr-response', '/#EXT-X-VMAP-AD-BREAK[\s\S]*?/', '#EXT-X-ENDLIST', 'example.org')
* ```
*
* <!-- markdownlint-enable line-length -->
*
* 1. Remove all text content of XMLHttpRequests with specific request method
*
* ```adblock
* example.org#%#//scriptlet('trusted-replace-xhr-response', '*', '', 'method:GET')
* ```
*
* 1. Replace text content of XMLHttpRequests matching by URL regex and request methods
*
* <!-- markdownlint-disable line-length -->
*
* ```adblock
* example.org#%#//scriptlet('trusted-replace-xhr-response', '/#EXT-X-VMAP-AD-BREAK[\s\S]*?/', '#EXT-X-ENDLIST', '/\.m3u8/ method:/GET|HEAD/')
* ```
*
* <!-- markdownlint-enable line-length -->
*
* 1. Remove all text content of all XMLHttpRequests for example.com
*
* ```adblock
* example.org#%#//scriptlet('trusted-replace-xhr-response', '*', '', 'example.com')
* ```
*
* 1. Replace "foo" text content with "bar" of all XMLHttpRequests for example.com and log original and modified text content <!-- markdownlint-disable-line line-length -->
*
* ```adblock
* example.org#%#//scriptlet('trusted-replace-xhr-response', 'foo', 'bar', 'example.com', 'true')
* ```
*
* 1. Replace all "noAds=false" text content with "noAds=true" of all XMLHttpRequests for example.com and log original and modified text content <!-- markdownlint-disable-line line-length -->
*
* ```adblock
* example.org#%#//scriptlet('trusted-replace-xhr-response', '/noAds=false/g', 'noAds=true', 'example.com', 'true')
* ```
*
* @added v1.7.3.
*/
/* eslint-enable max-len */
export function trustedReplaceXhrResponse(source, pattern = '', replacement = '', propsToMatch = '', verbose = false) {
// do nothing if browser does not support Proxy (e.g. Internet Explorer)
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
if (typeof Proxy === 'undefined') {
return;
}
// Only allow pattern as empty string for logging purposes
if (pattern === '' && replacement !== '') {
const message = 'Pattern argument should not be empty string.';
logMessage(source, message);
return;
}
const shouldLog = pattern === '' && replacement === '';
const shouldLogContent = verbose === 'true';
const nativeOpen = window.XMLHttpRequest.prototype.open;
const nativeSend = window.XMLHttpRequest.prototype.send;
let xhrData;
const openWrapper = (target, thisArg, args) => {
// eslint-disable-next-line prefer-spread
xhrData = getXhrData.apply(null, args);
if (shouldLog) {
// Log if no propsToMatch given
const message = `xhr( ${objectToString(xhrData)} )`;
logMessage(source, message, true);
hit(source);
return Reflect.apply(target, thisArg, args);
}
if (matchRequestProps(source, propsToMatch, xhrData)) {
thisArg.shouldBePrevented = true;
thisArg.headersReceived = !!thisArg.headersReceived;
}
// Trap setRequestHeader of target xhr object to mimic request headers later
if (thisArg.shouldBePrevented && !thisArg.headersReceived) {
thisArg.headersReceived = true;
thisArg.collectedHeaders = [];
const setRequestHeaderWrapper = (target, thisArg, args) => {
// Collect headers
thisArg.collectedHeaders.push(args);
return Reflect.apply(target, thisArg, args);
};
const setRequestHeaderHandler = {
apply: setRequestHeaderWrapper,
};
// setRequestHeader can only be called on open xhr object,
// so we can safely proxy it here
thisArg.setRequestHeader = new Proxy(thisArg.setRequestHeader, setRequestHeaderHandler);
}
return Reflect.apply(target, thisArg, args);
};
const sendWrapper = (target, thisArg, args) => {
if (!thisArg.shouldBePrevented) {
return Reflect.apply(target, thisArg, args);
}
/**
* Create separate XHR request with original request's input
* to be able to collect response data without triggering
* listeners on original XHR object
*/
const forgedRequest = new XMLHttpRequest();
forgedRequest.addEventListener('readystatechange', () => {
if (forgedRequest.readyState !== 4) {
return;
}
const {
readyState,
response,
responseText,
responseURL,
responseXML,
status,
statusText,
} = forgedRequest;
// Extract content from response
const content = responseText || response;
if (typeof content !== 'string') {
return;
}
const patternRegexp = pattern === '*'
? /(\n|.)*/
: toRegExp(pattern);
if (shouldLogContent) {
logMessage(source, `Original text content: ${content}`);
}
const modifiedContent = content.replace(patternRegexp, replacement);
if (shouldLogContent) {
logMessage(source, `Modified text content: ${modifiedContent}`);
}
// Manually put required values into target XHR object
// as thisArg can't be redefined and XHR objects can't be (re)assigned or copied
Object.defineProperties(thisArg, {
// original values
readyState: { value: readyState, writable: false },
responseURL: { value: responseURL, writable: false },
responseXML: { value: responseXML, writable: false },
status: { value: status, writable: false },
statusText: { value: statusText, writable: false },
// modified values
response: { value: modifiedContent, writable: false },
responseText: { value: modifiedContent, writable: false },
});
// Mock events
setTimeout(() => {
const stateEvent = new Event('readystatechange');
thisArg.dispatchEvent(stateEvent);
const loadEvent = new Event('load');
thisArg.dispatchEvent(loadEvent);
const loadEndEvent = new Event('loadend');
thisArg.dispatchEvent(loadEndEvent);
}, 1);
hit(source);
});
nativeOpen.apply(forgedRequest, [xhrData.method, xhrData.url]);
// Mimic request headers before sending
// setRequestHeader can only be called on open request objects
thisArg.collectedHeaders.forEach((header) => {
const name = header[0];
const value = header[1];
forgedRequest.setRequestHeader(name, value);
});
thisArg.collectedHeaders = [];
try {
nativeSend.call(forgedRequest, args);
} catch {
return Reflect.apply(target, thisArg, args);
}
return undefined;
};
const openHandler = {
apply: openWrapper,
};
const sendHandler = {
apply: sendWrapper,
};
XMLHttpRequest.prototype.open = new Proxy(XMLHttpRequest.prototype.open, openHandler);
XMLHttpRequest.prototype.send = new Proxy(XMLHttpRequest.prototype.send, sendHandler);
}
export const trustedReplaceXhrResponseNames = [
'trusted-replace-xhr-response',
// trusted scriptlets support no aliases
];
// eslint-disable-next-line prefer-destructuring
trustedReplaceXhrResponse.primaryName = trustedReplaceXhrResponseNames[0];
trustedReplaceXhrResponse.injections = [
hit,
logMessage,
toRegExp,
objectToString,
matchRequestProps,
getXhrData,
getMatchPropsData,
getRequestProps,
isValidParsedData,
parseMatchProps,
isValidStrPattern,
escapeRegExp,
isEmptyObject,
];