update from demoncord source

master
Drake 2 years ago
parent a57413fd1e
commit f6582b4ee1

@ -1,18 +1,20 @@
function monkeyPatch(name, parentObj, patches) { function monkeyPatch(name, parentObj, patches) {
const injId = Math.random().toString(36).slice(2).toUpperCase() const injId = Symbol()
const before = (patches["before"] === undefined) ? (...args)=>args : patches["before"]; const before = (patches["before"] === undefined) ? (...args)=>args : patches["before"];
const instead = (patches["instead"] === undefined) ? (...args)=>args : patches["instead"]; const instead = (patches["instead"] === undefined) ? (res,...args)=>res(...args) : patches["instead"];
const after = (patches["after"] === undefined) ? (args, res)=>res : patches["after"]; const after = (patches["after"] === undefined) ? (args, res)=>res : patches["after"];
const handler = { const handler = {
apply: (target, thisArg, args) => { apply: (target, thisArg, [ctx, args]) => {
before(args); before.apply(ctx, args);
res = (patches["instead"] === undefined) ? target(...args) : instead(...args); res = instead(target.bind(ctx), args)
return after(...args, res); return after.apply(ctx, args.concat([res]));
} }
}; };
const prox = new Proxy(parentObj[name], handler); const prox = new Proxy(parentObj[name], handler);
const orig = parentObj[name]; const orig = parentObj[name];
parentObj[name] = prox; parentObj[name] = function() {
return prox(this, [arguments]);
};
const unpatch = () => { const unpatch = () => {
parentObj[name] = orig; parentObj[name] = orig;
} }
@ -23,6 +25,7 @@ function monkeyPatch(name, parentObj, patches) {
}; };
return unpatch; return unpatch;
} }
function before(name, parentObj, func) { function before(name, parentObj, func) {
return monkeyPatch(name, parentObj, {before: func}) return monkeyPatch(name, parentObj, {before: func})
} }
@ -41,3 +44,4 @@ module.exports = {
instead, instead,
after after
} }

Loading…
Cancel
Save