Allows you to alter what functions do.
Example module, that includes functions we can patch.
const myModule = {
myFunction() {
console.log("[Original]: ran")
return "myReturn"
},
myOtherFunction() {
console.log("[Original]: ran")
return "myOtherReturn"
}
}
patcher.type(module, function, callback)
Using the basic templat but for the type is Before
.
In the callback you get 2 arguments
arguments
is an array of the arguments passed to the function.this
is thethis
value of the function. You dont need to return any values (will not be used at any point).
patcher.before(myModule, "myFunction", (args, thisItem) => {
console.log("[Before]: ran")
})
Using the basic templat but for the type is instead
.
In the callback you get 3 arguments
arguments
is an array of the arguments passed to the function.original
is the original value of the function. (or a instead function)this
is thethis
value of the function. You need to return a function or nothing. (if nothing is returned the original function will be used)
patcher.instead(myModule, "myFunction", (args, original, thisItem) => {
console.log("[Instead]: ran")
// The returned function gives the result to 'after'
return () => {
const result = original.apply(thisItem, args)
console.log("[Instead]: return function ran")
return result
}
})
Using the basic templat but for the type is after
.
In the callback you get 3 arguments
arguments
is an array of the arguments passed to the function.returnValue
is the value returned by the function.this
is thethis
value of the function. You dont need to return any values (will not be used at any point).
patcher.after(myModule, "myFunction", (args, result, thisItem) => {
console.log("[After]: ran")
})
All patches return a undo value.
const undo = patcher.after(myModule, "myFunction", (args, result, thisItem) => {
console.log("[After]: ran")
})
// Later on
undo()
const myModule = {
myFunction() {
console.log("[Original]: ran")
return "myReturn"
},
myOtherFunction() {
console.log("[Original]: ran")
return "myOtherReturn"
}
}
const undo = patcher.before(myModule, "myFunction", (args, thisItem) => {
console.log("[Before]: ran")
})
// this will log both logs
myModule.myFunction("myArgument")
// undo
undo()
// this will log only the original log
myModule.myFunction("myArgument")