Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
titaniumnetwork-dev
GitHub Repository: titaniumnetwork-dev/Ultraviolet
Path: blob/main/src/client/native/function.js
305 views
1
import EventEmitter from "events";
2
import HookEvent from "../hook.js";
3
4
/**
5
* @typedef {import('../index').default} UVClient
6
*/
7
8
class FunctionHook extends EventEmitter {
9
/**
10
*
11
* @param {UVClient} ctx
12
*/
13
constructor(ctx) {
14
super();
15
this.ctx = ctx;
16
this.window = ctx.window;
17
this.Function = this.window.Function;
18
this.fnProto = this.Function.prototype;
19
this.toString = this.fnProto.toString;
20
this.fnStrings = ctx.fnStrings;
21
this.call = this.fnProto.call;
22
this.apply = this.fnProto.apply;
23
this.bind = this.fnProto.bind;
24
}
25
overrideFunction() {
26
this.ctx.override(
27
this.window,
28
"Function",
29
(target, that, args) => {
30
if (!args.length) return target.apply(that, args);
31
32
let script = args[args.length - 1];
33
let fnArgs = [];
34
35
for (let i = 0; i < args.length - 1; i++) {
36
fnArgs.push(args[i]);
37
}
38
39
const event = new HookEvent({ script, args: fnArgs }, target, that);
40
this.emit("function", event);
41
42
if (event.intercepted) return event.returnValue;
43
return event.target.call(
44
event.that,
45
...event.data.args,
46
event.data.script
47
);
48
},
49
true
50
);
51
}
52
overrideToString() {
53
this.ctx.override(this.fnProto, "toString", (target, that) => {
54
const event = new HookEvent({ fn: that }, target, that);
55
this.emit("toString", event);
56
57
if (event.intercepted) return event.returnValue;
58
return event.target.call(event.data.fn);
59
});
60
}
61
}
62
63
export default FunctionHook;
64
65