commit bf1d5f0932dfc293c0d4aa7400fbc5ea729d3a27 Author: Ruthenic Date: Tue Jan 4 19:42:14 2022 -0500 literally just a patcher diff --git a/patcher.js b/patcher.js new file mode 100644 index 0000000..e8dbaad --- /dev/null +++ b/patcher.js @@ -0,0 +1,26 @@ +function monkeyPatch(name, parentObj, patches) { + const injId = Math.random().toString(36).slice(2).toUpperCase() + const before = (patches["before"] === undefined) ? (...args)=>args : patches["before"]; + const instead = (patches["instead"] === undefined) ? (...args)=>args : patches["instead"]; + const after = (patches["after"] === undefined) ? (args, res)=>res : patches["after"]; + const handler = { + apply: (target, thisArg, args) => { + before(args); + res = (patches["instead"] === undefined) ? target(...args) : instead(...args); + return after(...args, res); + } + }; + const prox = new Proxy(parentObj[name], handler); + const orig = parentObj[name]; + parentObj[name] = prox; + const unpatch = () => { + parentObj[name] = orig; + } + parentObj[injId] = { + name: name, + orig: orig, + unpatch: unpatch + }; + return unpatch; +} +module.exports = monkeyPatch