-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooklib.js
More file actions
161 lines (135 loc) · 4.83 KB
/
hooklib.js
File metadata and controls
161 lines (135 loc) · 4.83 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
class hookLibrary {
constructor() {
//hide getter and setter
var _this = this,
_Object = Object,
original_descriptors = Object.getOwnPropertyDescriptors,
original_descriptor = Object.getOwnPropertyDescriptor;
Object.getOwnPropertyDescriptors = function (_object) {
var result = original_descriptors.apply(this, arguments),
scopeIndex = _this.scopes.indexOf(_object);
if (scopeIndex > -1) {
var values = _this.scopev[scopeIndex],
entries = _Object.entries(result);
for (var i = 0; i < entries.length; i++) {
var key = 0, value = 1,
element = entries[i];
if (element[key] in values)
entries[i][value] = {
configurable: true,
enumerable: true,
value: values[element[key]],
writable: true
};
}
result = _Object.fromEntries(entries);
}
return result;
}
Object.getOwnPropertyDescriptor = function (_object, key) {
var result = original_descriptor.apply(this, arguments),
scopeIndex = _this.scopes.indexOf(_object);
if (scopeIndex > -1) {
var values = _this.scopev[scopeIndex];
if (key in values) {
result = {
configurable: true,
enumerable: true,
value: values[key],
writable: true
};
}
}
return result;
}
this.hides.conceal_string(original_descriptors, Object.getOwnPropertyDescriptors);
this.hides.conceal_string(original_descriptor, Object.getOwnPropertyDescriptor);
//keep original
this.original_descriptor = original_descriptor;
}
def(type, obj, key, func, configurable = true) {
//remove
if (key in obj && obj[key]) {
var val = obj[key];
delete obj[key];
}
Object.defineProperty(obj, key,{
[type]: func,
configurable
});
//restore
if (val && type == 'set')
obj[key] = val;
}
hides = {
orientedRecursion(value){
var _static = new Proxy(value, {
get() { return _static; }
});
return _static;
},
conceal_string(original, hooked) {
var original_res = original.toString();
hooked.toString = function () {
return original_res;
};
hooked.toString.toString = this.orientedRecursion(function() {
return "function toString() { [native code] }"
});
},
conceal_lookup(_object) {
//hide lookup getter / setter
for (var tmp of "GS") {
var method = `__lookup${tmp}etter__`,
orig = _object[method];
_object[method] = function (key) {}; //return nothing
this.conceal_string(orig, _object[method]);
}
}
}
//for tracking objects and scopes
scopes = [];
scopev = [];
catch(scope, key, callback) {
var descriptor = this.original_descriptor(scope, key);
//:)
if (descriptor && descriptor.configurable == false)
throw Error(`"${key}" is unconfigurable!`);
var scopeIndex = this.scopes.indexOf(scope),
notFound = scopeIndex < 0;
if (notFound) {
this.scopes.push(scope);
this.scopev.push({});
this.hides.conceal_lookup(scope);
}
var _this = this.scopev[notFound + scopeIndex];
function scopeManager(av) {
return ({
//set
true(value){
_this[key] = callback(value);
},
//get
false(){
return _this[key];
}
})[0 in arguments](av);
}
for (var type of ['set','get'])
this.def(type, scope, key, scopeManager);
}
// I actually iterate the argument object.
// the last two paramerter names are for ease of use
// scope, ...keys, callback
catchLast(scope, u_iterated_keys, u_callback) {
var args = [...arguments],
callback = args.pop(),
keys = args.slice(1);
var index = 0,
_this = this;
void function recursive(value) {
_this.catch(value, keys[index], index++ < keys.length - 1 ? recursive : callback);
return value;
}(scope);
}
}