-
Notifications
You must be signed in to change notification settings - Fork 30
/
log-eval.js
50 lines (42 loc) · 1.15 KB
/
log-eval.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
/* eslint-disable no-eval */
import { hit, logMessage } from '../helpers';
/**
* @scriptlet log-eval
*
* @description
* Logs all `eval()` or `new Function()` calls to the console.
*
* ### Syntax
*
* ```adblock
* example.org#%#//scriptlet('log-eval')
* ```
*
* @added v1.0.4.
*/
export function logEval(source) {
// wrap eval function
const nativeEval = window.eval;
function evalWrapper(str) {
hit(source);
logMessage(source, `eval("${str}")`, true);
return nativeEval(str);
}
window.eval = evalWrapper;
// wrap new Function
const nativeFunction = window.Function;
function FunctionWrapper(...args) {
hit(source);
logMessage(source, `new Function(${args.join(', ')})`, true);
return nativeFunction.apply(this, [...args]);
}
FunctionWrapper.prototype = Object.create(nativeFunction.prototype);
FunctionWrapper.prototype.constructor = FunctionWrapper;
window.Function = FunctionWrapper;
}
export const logEvalNames = [
'log-eval',
];
// eslint-disable-next-line prefer-destructuring
logEval.primaryName = logEvalNames[0];
logEval.injections = [hit, logMessage];