@@ -15,6 +15,12 @@ import {
15
15
parseMatchProps ,
16
16
isValidParsedData ,
17
17
getMatchPropsData ,
18
+ generateRandomResponse ,
19
+ nativeIsFinite ,
20
+ nativeIsNaN ,
21
+ getNumberFromString ,
22
+ getRandomIntInclusive ,
23
+ getRandomStrByLength ,
18
24
} from '../helpers' ;
19
25
20
26
/* eslint-disable max-len */
@@ -46,6 +52,10 @@ import {
46
52
* - `emptyObj` — empty object
47
53
* - `emptyArr` — empty array
48
54
* - `emptyStr` — empty string
55
+ * - `true` — random alphanumeric string of 10 symbols
56
+ * - colon-separated pair `name:value` string value to customize `responseBody` where
57
+ * - `name` — only `length` supported for now
58
+ * - `value` — range on numbers, for example `100-300`, limited to 500000 characters
49
59
* - `responseType` — optional, string for defining response type,
50
60
* original response type is used if not specified. Possible values:
51
61
* - `basic`
@@ -97,6 +107,12 @@ import {
97
107
*
98
108
* ! Specify response body for all fetch calls
99
109
* example.org#%#//scriptlet('prevent-fetch', '', 'emptyArr')
110
+ *
111
+ * ! Specify response body to random alphanumeric string of 10 symbols for all fetch calls
112
+ * example.org#%#//scriptlet('prevent-fetch', '', 'true')
113
+ *
114
+ * ! Specify response body to random alphanumeric string with specific range for all fetch calls
115
+ * example.org#%#//scriptlet('prevent-fetch', '', 'length:100-300')
100
116
* ```
101
117
*
102
118
* 1. Prevent all fetch calls and specify response type value
@@ -128,6 +144,8 @@ export function preventFetch(source, propsToMatch, responseBody = 'emptyObj', re
128
144
strResponseBody = '[]' ;
129
145
} else if ( responseBody === 'emptyStr' ) {
130
146
strResponseBody = '' ;
147
+ } else if ( responseBody === 'true' || responseBody . match ( / ^ l e n g t h : \d + - \d + $ / ) ) {
148
+ strResponseBody = generateRandomResponse ( responseBody ) ;
131
149
} else {
132
150
logMessage ( source , `Invalid responseBody parameter: '${ responseBody } '` ) ;
133
151
return ;
@@ -249,4 +267,10 @@ preventFetch.injections = [
249
267
parseMatchProps ,
250
268
isValidParsedData ,
251
269
getMatchPropsData ,
270
+ generateRandomResponse ,
271
+ nativeIsFinite ,
272
+ nativeIsNaN ,
273
+ getNumberFromString ,
274
+ getRandomIntInclusive ,
275
+ getRandomStrByLength ,
252
276
] ;
0 commit comments