project-client/src/shared/utils/monkeyPatch.ts

24 lines
738 B
TypeScript
Raw Normal View History

2023-04-10 05:49:50 +09:00
/*
* SPDX-License-Identifier: GPL-3.0
* Vencord Desktop, a desktop app aiming to give you a snappier Discord Experience
* Copyright (c) 2023 Vendicated and Vencord contributors
*/
2023-04-09 07:49:47 +09:00
type Func = (...args: any[]) => any;
2023-04-10 05:49:50 +09:00
export function monkeyPatch<O extends object>(
object: O,
key: keyof O,
replacement: (original: Func, ...args: any[]) => any
): void {
2023-04-09 07:49:47 +09:00
const original = object[key] as Func;
2023-04-10 05:49:50 +09:00
const replacer = (object[key] = function (this: unknown, ...args: any[]) {
2023-04-09 07:49:47 +09:00
return replacement.call(this, original, ...args);
2023-04-10 05:49:50 +09:00
} as any);
2023-04-09 07:49:47 +09:00
Object.defineProperties(replacer, Object.getOwnPropertyDescriptors(original));
replacer.toString = () => original.toString();
replacer.$$original = original;
}