From 296336535f6d23c586c7e83edf0420b58cbf011a Mon Sep 17 00:00:00 2001 From: Vendicated Date: Fri, 14 Oct 2022 21:34:35 +0200 Subject: Fix modals, add wp.mapMangledModule --- src/webpack/webpack.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'src/webpack/webpack.ts') 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(code: string, mappers: Record): Record { + const exports = {} as Record; + + // 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(code: string, mappers: Record): Record { + return proxyLazy(() => mapMangledModule(code, mappers)); +} + export function findByProps(...props: string[]) { return find(filters.byProps(props)); } -- cgit