diff options
author | Vendicated <vendicated@riseup.net> | 2022-10-14 21:34:35 +0200 |
---|---|---|
committer | Vendicated <vendicated@riseup.net> | 2022-10-14 21:34:35 +0200 |
commit | 296336535f6d23c586c7e83edf0420b58cbf011a (patch) | |
tree | aa2a12bd071b36d44f43da992aa9fddfdd11bf50 /src/webpack/webpack.ts | |
parent | 563f2fb1dc2067345e4fbd71ce8da7d60d2928e1 (diff) | |
download | Vencord-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.ts | 46 |
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)); } |