-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path5.spoofFunctionBind.js
79 lines (69 loc) · 2.5 KB
/
5.spoofFunctionBind.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
page.onInitialized = function () {
page.evaluate(function () {
function indexOfArray(a, b) {
var la = a.length;
for (var i = 0; i < la; i++) {
if (a[i] === b) {
return i;
}
}
return -1;
}
var bound = [];
var oldCall = Function.prototype.call;
var oldApply = Function.prototype.apply;
var slice = [].slice;
var concat = [].concat;
oldCall.call = oldCall;
oldCall.apply = oldApply;
oldApply.call = oldCall;
oldApply.apply = oldApply;
function call() {
return oldCall.apply(this, arguments);
}
Function.prototype.call = call;
function apply() {
return oldApply.apply(this, arguments);
}
Function.prototype.apply = apply;
function bind() {
var func = this;
var self = arguments[0];
var rest = oldCall.call(slice.call, slice, arguments, 1);
rest.concat = concat;
var result = (function () {
var args = oldCall.call(slice.call, slice, arguments, 0);
return func.apply(self, rest.concat(args));
});
bound.push(result);
return result;
}
Function.prototype.bind = bind;
var nativeFunctionString = Error.toString().replace(/Error/g, "bind");
var nativeToStringFunctionString = Error.toString().replace(/Error/g, "toString");
var nativeBoundFunctionString = Error.toString().replace(/Error/g, "");
var nativeCallFunctionString = Error.toString().replace(/Error/g, "call");
var nativeApplyFunctionString = Error.toString().replace(/Error/g, "apply");
var oldToString = Function.prototype.toString;
function functionToString() {
if (this === bind) {
return nativeFunctionString;
}
if (this === functionToString) {
return nativeToStringFunctionString;
}
if (this === call) {
return nativeCallFunctionString;
}
if (this === apply) {
return nativeApplyFunctionString;
}
var idx = indexOfArray(bound, this);
if (idx >= 0) {
return nativeBoundFunctionString;
}
return oldCall.call(oldToString, this);
}
Function.prototype.toString = functionToString;
});
};