-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (61 loc) · 2.12 KB
/
index.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
Object.defineProperty(Symbol, "patcherSymbol", {
enumerable: false,
writable: false,
configurable: false,
value: Symbol("Patcher.Symbol")
})
function hookModule(module, fn) {
let originalFunction = module[fn]
if (!originalFunction) originalFunction = () => {}
let hook = originalFunction[Symbol.patcherSymbol]
if (hook) return hook
Object.defineProperty(module, fn, {
writable: true,
configurable: true,
value: function() {
if (!hook) hook = module[fn][Symbol.patcherSymbol]
for (const { callback } of module[fn].before) callback(arguments, this)
let instead = originalFunction
for (const { callback } of module[fn].instead) {
const res = callback(arguments, instead, this)
if (typeof res === "function") instead = res
}
const res = instead.apply(this, arguments)
for (const { callback } of module[fn].after) callback(arguments, res, this)
return res
}
})
Object.defineProperty(module[fn], "toString", {
enumerable: false,
writable: true,
configurable: true,
value: () => originalFunction.toString()
})
Object.defineProperty(module[fn].toString, "toString", {
enumerable: false,
writable: true,
configurable: true,
value: () => originalFunction.toString.toString()
})
Object.assign(module[fn], originalFunction)
return module[fn][Symbol.patcherSymbol] = {
original: originalFunction,
after: [],
instead: [],
before: []
}
}
function patch(module, functionToPatch, callback, type) {
const hook = hookModule(module, functionToPatch)
const sym = Symbol()
hook[type].push({ sym, callback })
return () => {
const found = hook[type].find((patches) => patches.sym === sym)
if (!found) return
const index = hook[type].indexOf(found)
hook[type].splice(index, 1)
}
}
exports.after = (module, functionToPatch, callback) => patch(module, functionToPatch, callback, "after")
exports.instead = (module, functionToPatch, callback) => patch(module, functionToPatch, callback, "instead")
exports.before = (module, functionToPatch, callback) => patch(module, functionToPatch, callback, "before")