-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtrusted-prevent-xhr.ts
More file actions
144 lines (140 loc) · 4.73 KB
/
Copy pathtrusted-prevent-xhr.ts
File metadata and controls
144 lines (140 loc) · 4.73 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
import {
hit,
objectToString,
createPreventXhrCore,
generateResponseContent,
matchRequestProps,
getXhrData,
logMessage,
toRegExp,
isValidStrPattern,
escapeRegExp,
isEmptyObject,
getNumberFromString,
nativeIsFinite,
nativeIsNaN,
parseMatchProps,
isValidParsedData,
getMatchPropsData,
getRequestProps,
getRandomIntInclusive,
getRandomStrByLength,
} from '../helpers';
import { type Source } from './scriptlets';
/* eslint-disable max-len */
/**
* @trustedScriptlet trusted-prevent-xhr
*
* @description
* Prevents `xhr` calls if **all** given parameters match.
*
* Trusted version of [prevent-xhr](./about-scriptlets.md#prevent-xhr).
* In addition to everything `prevent-xhr` supports, `trusted-prevent-xhr`
* can return **arbitrary literal text** as the response body.
*
* Related UBO scriptlet:
* https://github.com/gorhill/uBlock/wiki/Resources-Library#no-xhr-ifjs-
*
* ### Syntax
*
* ```text
* example.org#%#//scriptlet('trusted-prevent-xhr'[, propsToMatch[, directive]])
* ```
*
* - `propsToMatch` — optional, string of space-separated properties to match; possible props:
* - string or regular expression for matching the URL passed to `XMLHttpRequest.open()` call;
* empty string or wildcard `*` for all `XMLHttpRequest.open()` calls match
* - colon-separated pairs `name:value` where
* - `name` is XMLHttpRequest object property name
* - `value` is string or regular expression for matching the value of the option
* passed to `XMLHttpRequest.open()` call
* - `directive` — defaults to `false` for empty responseText,
* optional argument to set responseText and response of matched XMLHttpRequest's response; possible values:
* - `true` to randomize responseText and response, random alphanumeric string of 10 symbols
* - `emptyObj` to set responseText and response to `{}`
* - `emptyArr` to set responseText and response to `[]`
* - `emptyStr` to set responseText and response to an empty string
* - colon-separated pair `name:value` string value to customize responseText and response data where
* - `name` — only `length` supported for now
* - `value` — single number (e.g. `50`) or range of numbers (e.g. `100-300`), values above 500000 are rejected
* - any other string is treated as **literal text** and returned as the response body as-is
* - `length:` can be combined with literal text in a single directive to
* repeat the text to the specified length, e.g.
* `'length:8000-10000 body ins.adsbygoogle'` or
* `'body ins.adsbygoogle length:8000-10000'`
*
* > 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-prevent-xhr')
* ```
*
* 1. Prevent XMLHttpRequests for specific url
*
* ```adblock
* example.org#%#//scriptlet('trusted-prevent-xhr', 'example.org')
* ```
*
* 1. Prevent XMLHttpRequests for specific url and set literal response text
*
* ```adblock
* example.org#%#//scriptlet('trusted-prevent-xhr', 'example.org', '{"blocked":true}')
* ```
*
* 1. Prevent XMLHttpRequests for specific url and randomize response text
*
* ```adblock
* example.org#%#//scriptlet('trusted-prevent-xhr', 'example.org', 'true')
* ```
*
* 1. Prevent XMLHttpRequests for specific url and set response to empty array
*
* ```adblock
* example.org#%#//scriptlet('trusted-prevent-xhr', 'example.org', 'emptyArr')
* ```
*
* 1. Prevent XMLHttpRequests and set response with fixed length
*
* ```adblock
* example.org#%#//scriptlet('trusted-prevent-xhr', 'example.org', 'length:100')
* ```
*
* @added v2.5.0.
*/
/* eslint-enable max-len */
export function trustedPreventXhr(source: Source, propsToMatch?: string, directive?: string): void {
createPreventXhrCore(source, propsToMatch, true, directive);
}
export const trustedPreventXhrNames = [
'trusted-prevent-xhr',
// trusted scriptlets support no aliases
];
// eslint-disable-next-line prefer-destructuring
trustedPreventXhr.primaryName = trustedPreventXhrNames[0];
trustedPreventXhr.injections = [
createPreventXhrCore,
generateResponseContent,
hit,
objectToString,
matchRequestProps,
getXhrData,
logMessage,
toRegExp,
isValidStrPattern,
escapeRegExp,
isEmptyObject,
getNumberFromString,
nativeIsFinite,
nativeIsNaN,
parseMatchProps,
isValidParsedData,
getMatchPropsData,
getRequestProps,
getRandomIntInclusive,
getRandomStrByLength,
];