-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathprevent-eval-if.js
57 lines (51 loc) · 1.48 KB
/
prevent-eval-if.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
/* eslint-disable no-eval, no-extra-bind, func-names */
import { toRegExp, hit } from '../helpers';
/**
* @scriptlet prevent-eval-if
*
* @description
* Prevents page to use eval matching payload.
*
* Related UBO scriptlet:
* https://github.com/gorhill/uBlock/wiki/Resources-Library#noeval-ifjs-
*
* ### Syntax
*
* ```text
* example.org#%#//scriptlet('prevent-eval-if'[, search])
* ```
*
* - `search` — optional, string or regular expression matching the stringified eval payload;
* defaults to match all stringified eval payloads;
* invalid regular expression will cause exit and rule will not work
*
* ### Examples
*
* ```adblock
* ! Prevents eval if it matches 'test'
* example.org#%#//scriptlet('prevent-eval-if', 'test')
* ```
*
* @added v1.0.4.
*/
export function preventEvalIf(source, search) {
const searchRegexp = toRegExp(search);
const nativeEval = window.eval;
window.eval = function (payload) {
if (!searchRegexp.test(payload.toString())) {
return nativeEval.call(window, payload);
}
hit(source, payload);
return undefined;
}.bind(window);
}
export const preventEvalIfNames = [
'prevent-eval-if',
// aliases are needed for matching the related scriptlet converted into our syntax
'noeval-if.js',
'ubo-noeval-if.js',
'ubo-noeval-if',
];
// eslint-disable-next-line prefer-destructuring
preventEvalIf.primaryName = preventEvalIfNames[0];
preventEvalIf.injections = [toRegExp, hit];