aboutsummaryrefslogtreecommitdiff
path: root/src/webpack/webpack.ts
diff options
context:
space:
mode:
authorVendicated <vendicated@riseup.net>2022-10-14 21:34:35 +0200
committerVendicated <vendicated@riseup.net>2022-10-14 21:34:35 +0200
commit296336535f6d23c586c7e83edf0420b58cbf011a (patch)
treeaa2a12bd071b36d44f43da992aa9fddfdd11bf50 /src/webpack/webpack.ts
parent563f2fb1dc2067345e4fbd71ce8da7d60d2928e1 (diff)
downloadVencord-296336535f6d23c586c7e83edf0420b58cbf011a.tar.gz
Vencord-296336535f6d23c586c7e83edf0420b58cbf011a.tar.bz2
Vencord-296336535f6d23c586c7e83edf0420b58cbf011a.zip
Fix modals, add wp.mapMangledModule
Diffstat (limited to 'src/webpack/webpack.ts')
-rw-r--r--src/webpack/webpack.ts46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/webpack/webpack.ts b/src/webpack/webpack.ts
index ca987d0..b8ccca9 100644
--- a/src/webpack/webpack.ts
+++ b/src/webpack/webpack.ts
@@ -1,4 +1,5 @@
import type { WebpackInstance } from "discord-types/other";
+import { proxyLazy } from "../utils/proxyLazy";
export let _resolveReady: () => void;
/**
@@ -92,6 +93,51 @@ export function findAll(filter: FilterFn, getDefault = true) {
return ret;
}
+/**
+ * Finds a mangled module by the provided code "code" (must be unique and can be anywhere in the module)
+ * then maps it into an easily usable module via the specified mappers
+ * @param code Code snippet
+ * @param mappers Mappers to create the non mangled exports
+ * @returns Unmangled exports as specified in mappers
+ *
+ * @example mapMangledModule("headerIdIsManaged:", {
+ * openModal: filters.byCode("headerIdIsManaged:"),
+ * closeModal: filters.byCode("key==")
+ * })
+ */
+export function mapMangledModule<S extends string>(code: string, mappers: Record<S, FilterFn>): Record<S, any> {
+ const exports = {} as Record<S, any>;
+
+ // search every factory function
+ for (const id in wreq.m) {
+ const src = wreq.m[id].toString() as string;
+ if (src.includes(code)) {
+ const mod = wreq(id as any as number);
+ outer:
+ for (const key in mod) {
+ const member = mod[key];
+ for (const newName in mappers) {
+ // if the current mapper matches this module
+ if (mappers[newName](member)) {
+ exports[newName] = member;
+ continue outer;
+ }
+ }
+ }
+ break;
+ }
+ }
+
+ return exports;
+}
+
+/**
+ * Same as {@link mapMangledModule} but lazy
+ */
+export function mapMangledModuleLazy<S extends string>(code: string, mappers: Record<S, FilterFn>): Record<S, any> {
+ return proxyLazy(() => mapMangledModule(code, mappers));
+}
+
export function findByProps(...props: string[]) {
return find(filters.byProps(props));
}