2022-10-21 19:17:06 -04:00
|
|
|
/*
|
|
|
|
* Vencord, a modification for Discord's desktop app
|
|
|
|
* Copyright (c) 2022 Vendicated and contributors
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2023-05-05 19:36:00 -04:00
|
|
|
import { proxyLazy } from "@utils/lazy";
|
|
|
|
import { Logger } from "@utils/Logger";
|
2022-09-05 13:54:02 -04:00
|
|
|
import type { WebpackInstance } from "discord-types/other";
|
2022-10-22 12:18:41 -04:00
|
|
|
|
2022-11-07 15:05:33 -05:00
|
|
|
import { traceFunction } from "../debug/Tracer";
|
2022-09-05 13:54:02 -04:00
|
|
|
|
2022-11-03 14:12:50 -04:00
|
|
|
const logger = new Logger("Webpack");
|
|
|
|
|
2022-09-30 18:42:50 -04:00
|
|
|
export let _resolveReady: () => void;
|
|
|
|
/**
|
|
|
|
* Fired once a gateway connection to Discord has been established.
|
|
|
|
* This indicates that the core webpack modules have been initialised
|
|
|
|
*/
|
|
|
|
export const onceReady = new Promise<void>(r => _resolveReady = r);
|
|
|
|
|
2022-09-05 13:54:02 -04:00
|
|
|
export let wreq: WebpackInstance;
|
|
|
|
export let cache: WebpackInstance["c"];
|
2022-08-31 14:47:07 -04:00
|
|
|
|
|
|
|
export type FilterFn = (mod: any) => boolean;
|
|
|
|
|
|
|
|
export const filters = {
|
2022-10-30 15:45:18 -04:00
|
|
|
byProps: (...props: string[]): FilterFn =>
|
2022-08-31 14:47:07 -04:00
|
|
|
props.length === 1
|
|
|
|
? m => m[props[0]] !== void 0
|
|
|
|
: m => props.every(p => m[p] !== void 0),
|
2022-10-30 15:45:18 -04:00
|
|
|
|
2022-09-27 10:57:46 -04:00
|
|
|
byCode: (...code: string[]): FilterFn => m => {
|
|
|
|
if (typeof m !== "function") return false;
|
|
|
|
const s = Function.prototype.toString.call(m);
|
|
|
|
for (const c of code) {
|
|
|
|
if (!s.includes(c)) return false;
|
|
|
|
}
|
|
|
|
return true;
|
2022-09-28 06:15:30 -04:00
|
|
|
},
|
2023-02-10 16:33:34 -05:00
|
|
|
byStoreName: (name: string): FilterFn => m =>
|
2023-02-08 15:48:26 -05:00
|
|
|
m.constructor?.displayName === name
|
2022-08-31 14:47:07 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
export const subscriptions = new Map<FilterFn, CallbackFn>();
|
|
|
|
export const listeners = new Set<CallbackFn>();
|
|
|
|
|
2023-03-07 23:59:50 -05:00
|
|
|
export type CallbackFn = (mod: any, id: number) => void;
|
2022-08-31 14:47:07 -04:00
|
|
|
|
|
|
|
export function _initWebpack(instance: typeof window.webpackChunkdiscord_app) {
|
2022-09-05 13:54:02 -04:00
|
|
|
if (cache !== void 0) throw "no.";
|
2022-08-31 14:47:07 -04:00
|
|
|
|
2023-10-24 18:17:11 -04:00
|
|
|
instance.push([[Symbol("Vencord")], {}, r => wreq = r]);
|
2023-10-26 16:51:03 -04:00
|
|
|
instance.pop();
|
2023-10-24 18:17:11 -04:00
|
|
|
if (!wreq) return false;
|
|
|
|
|
2022-09-05 13:54:02 -04:00
|
|
|
cache = wreq.c;
|
2023-10-24 18:17:11 -04:00
|
|
|
|
|
|
|
for (const id in cache) {
|
|
|
|
const { exports } = cache[id];
|
|
|
|
if (!exports) continue;
|
|
|
|
|
|
|
|
const numberId = Number(id);
|
|
|
|
|
|
|
|
for (const callback of listeners) {
|
|
|
|
try {
|
|
|
|
callback(exports, numberId);
|
|
|
|
} catch (err) {
|
|
|
|
logger.error("Error in webpack listener", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const [filter, callback] of subscriptions) {
|
|
|
|
try {
|
|
|
|
if (filter(exports)) {
|
|
|
|
subscriptions.delete(filter);
|
|
|
|
callback(exports, numberId);
|
|
|
|
} else if (typeof exports === "object") {
|
|
|
|
if (exports.default && filter(exports.default)) {
|
|
|
|
subscriptions.delete(filter);
|
|
|
|
callback(exports.default, numberId);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const nested in exports) if (nested.length <= 3) {
|
|
|
|
if (exports[nested] && filter(exports[nested])) {
|
|
|
|
subscriptions.delete(filter);
|
|
|
|
callback(exports[nested], numberId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
logger.error("Error while firing callback for webpack chunk", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2022-08-31 14:47:07 -04:00
|
|
|
}
|
|
|
|
|
2023-04-03 19:16:29 -04:00
|
|
|
if (IS_DEV && IS_DISCORD_DESKTOP) {
|
2022-11-03 15:36:17 -04:00
|
|
|
var devToolsOpen = false;
|
|
|
|
// At this point in time, DiscordNative has not been exposed yet, so setImmediate is needed
|
|
|
|
setTimeout(() => {
|
|
|
|
DiscordNative/* just to make sure */?.window.setDevtoolsCallbacks(() => devToolsOpen = true, () => devToolsOpen = false);
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
|
2023-09-25 12:27:18 -04:00
|
|
|
function handleModuleNotFound(method: string, ...filter: unknown[]) {
|
|
|
|
const err = new Error(`webpack.${method} found no module`);
|
|
|
|
logger.error(err, "Filter:", filter);
|
|
|
|
|
|
|
|
// Strict behaviour in DevBuilds to fail early and make sure the issue is found
|
|
|
|
if (IS_DEV && !devToolsOpen)
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2022-11-25 13:25:35 -05:00
|
|
|
/**
|
|
|
|
* Find the first module that matches the filter
|
|
|
|
*/
|
2023-09-25 12:27:18 -04:00
|
|
|
export const find = traceFunction("find", function find(filter: FilterFn, { isIndirect = false, isWaitFor = false }: { isIndirect?: boolean; isWaitFor?: boolean; } = {}) {
|
2022-08-31 14:47:07 -04:00
|
|
|
if (typeof filter !== "function")
|
2022-09-30 20:27:28 -04:00
|
|
|
throw new Error("Invalid filter. Expected a function got " + typeof filter);
|
2022-08-31 14:47:07 -04:00
|
|
|
|
2022-09-05 13:54:02 -04:00
|
|
|
for (const key in cache) {
|
|
|
|
const mod = cache[key];
|
2022-09-27 08:34:57 -04:00
|
|
|
if (!mod?.exports) continue;
|
|
|
|
|
2023-03-07 23:59:50 -05:00
|
|
|
if (filter(mod.exports)) {
|
|
|
|
return isWaitFor ? [mod.exports, Number(key)] : mod.exports;
|
|
|
|
}
|
2022-09-28 16:49:46 -04:00
|
|
|
|
|
|
|
if (typeof mod.exports !== "object") continue;
|
|
|
|
|
2023-03-07 23:59:50 -05:00
|
|
|
if (mod.exports.default && filter(mod.exports.default)) {
|
2023-09-25 12:27:18 -04:00
|
|
|
const found = mod.exports.default;
|
2023-03-07 23:59:50 -05:00
|
|
|
return isWaitFor ? [found, Number(key)] : found;
|
|
|
|
}
|
2022-09-28 16:49:46 -04:00
|
|
|
|
|
|
|
// the length check makes search about 20% faster
|
2022-10-09 16:15:14 -04:00
|
|
|
for (const nestedMod in mod.exports) if (nestedMod.length <= 3) {
|
2022-09-27 08:34:57 -04:00
|
|
|
const nested = mod.exports[nestedMod];
|
2023-03-07 23:59:50 -05:00
|
|
|
if (nested && filter(nested)) {
|
|
|
|
return isWaitFor ? [nested, Number(key)] : nested;
|
|
|
|
}
|
2022-09-27 08:34:57 -04:00
|
|
|
}
|
2022-08-31 14:47:07 -04:00
|
|
|
}
|
|
|
|
|
2023-09-25 12:27:18 -04:00
|
|
|
if (!isIndirect) {
|
|
|
|
handleModuleNotFound("find", filter);
|
2022-10-30 15:45:18 -04:00
|
|
|
}
|
|
|
|
|
2023-03-07 23:59:50 -05:00
|
|
|
return isWaitFor ? [null, null] : null;
|
2022-11-07 15:05:33 -05:00
|
|
|
});
|
2022-08-31 14:47:07 -04:00
|
|
|
|
2022-11-28 07:37:55 -05:00
|
|
|
/**
|
|
|
|
* find but lazy
|
|
|
|
*/
|
2023-09-25 12:27:18 -04:00
|
|
|
export function findLazy(filter: FilterFn) {
|
|
|
|
return proxyLazy(() => find(filter));
|
2022-11-28 07:37:55 -05:00
|
|
|
}
|
|
|
|
|
2023-09-25 12:27:18 -04:00
|
|
|
export function findAll(filter: FilterFn) {
|
2022-10-30 15:45:18 -04:00
|
|
|
if (typeof filter !== "function")
|
|
|
|
throw new Error("Invalid filter. Expected a function got " + typeof filter);
|
2022-08-31 14:47:07 -04:00
|
|
|
|
|
|
|
const ret = [] as any[];
|
2022-09-05 13:54:02 -04:00
|
|
|
for (const key in cache) {
|
|
|
|
const mod = cache[key];
|
2022-09-30 18:42:50 -04:00
|
|
|
if (!mod?.exports) continue;
|
|
|
|
|
|
|
|
if (filter(mod.exports))
|
|
|
|
ret.push(mod.exports);
|
|
|
|
else if (typeof mod.exports !== "object")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (mod.exports.default && filter(mod.exports.default))
|
2023-09-25 12:27:18 -04:00
|
|
|
ret.push(mod.exports.default);
|
2022-10-09 16:15:14 -04:00
|
|
|
else for (const nestedMod in mod.exports) if (nestedMod.length <= 3) {
|
2022-09-30 18:42:50 -04:00
|
|
|
const nested = mod.exports[nestedMod];
|
|
|
|
if (nested && filter(nested)) ret.push(nested);
|
|
|
|
}
|
2022-08-31 14:47:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-10-14 15:34:35 -04:00
|
|
|
/**
|
2022-11-03 14:12:50 -04:00
|
|
|
* Same as {@link find} but in bulk
|
2022-11-15 18:40:46 -05:00
|
|
|
* @param filterFns Array of filters. Please note that this array will be modified in place, so if you still
|
2022-11-03 14:12:50 -04:00
|
|
|
* need it afterwards, pass a copy.
|
|
|
|
* @returns Array of results in the same order as the passed filters
|
|
|
|
*/
|
2022-11-15 18:40:46 -05:00
|
|
|
export const findBulk = traceFunction("findBulk", function findBulk(...filterFns: FilterFn[]) {
|
2022-11-03 14:12:50 -04:00
|
|
|
if (!Array.isArray(filterFns))
|
|
|
|
throw new Error("Invalid filters. Expected function[] got " + typeof filterFns);
|
2022-10-14 15:34:35 -04:00
|
|
|
|
2022-11-03 14:12:50 -04:00
|
|
|
const { length } = filterFns;
|
|
|
|
|
|
|
|
if (length === 0)
|
|
|
|
throw new Error("Expected at least two filters.");
|
|
|
|
|
|
|
|
if (length === 1) {
|
|
|
|
if (IS_DEV) {
|
|
|
|
throw new Error("bulk called with only one filter. Use find");
|
|
|
|
}
|
|
|
|
return find(filterFns[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const filters = filterFns as Array<FilterFn | undefined>;
|
|
|
|
|
|
|
|
let found = 0;
|
|
|
|
const results = Array(length);
|
|
|
|
|
|
|
|
outer:
|
|
|
|
for (const key in cache) {
|
|
|
|
const mod = cache[key];
|
|
|
|
if (!mod?.exports) continue;
|
|
|
|
|
|
|
|
for (let j = 0; j < length; j++) {
|
|
|
|
const filter = filters[j];
|
|
|
|
// Already done
|
|
|
|
if (filter === undefined) continue;
|
|
|
|
|
|
|
|
if (filter(mod.exports)) {
|
|
|
|
results[j] = mod.exports;
|
|
|
|
filters[j] = undefined;
|
|
|
|
if (++found === length) break outer;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof mod.exports !== "object")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (mod.exports.default && filter(mod.exports.default)) {
|
|
|
|
results[j] = mod.exports.default;
|
|
|
|
filters[j] = undefined;
|
|
|
|
if (++found === length) break outer;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const nestedMod in mod.exports)
|
|
|
|
if (nestedMod.length <= 3) {
|
|
|
|
const nested = mod.exports[nestedMod];
|
|
|
|
if (nested && filter(nested)) {
|
|
|
|
results[j] = nested;
|
|
|
|
filters[j] = undefined;
|
|
|
|
if (++found === length) break outer;
|
2022-10-14 15:34:35 -04:00
|
|
|
continue outer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-03 14:12:50 -04:00
|
|
|
if (found !== length) {
|
|
|
|
const err = new Error(`Got ${length} filters, but only found ${found} modules!`);
|
|
|
|
if (IS_DEV) {
|
2022-11-03 15:36:17 -04:00
|
|
|
if (!devToolsOpen)
|
|
|
|
// Strict behaviour in DevBuilds to fail early and make sure the issue is found
|
|
|
|
throw err;
|
|
|
|
} else {
|
|
|
|
logger.warn(err);
|
2022-11-03 14:12:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
2022-11-15 18:40:46 -05:00
|
|
|
});
|
2022-11-03 14:12:50 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the id of a module by its code
|
|
|
|
* @param code Code
|
|
|
|
* @returns number or null
|
|
|
|
*/
|
2022-11-07 15:05:33 -05:00
|
|
|
export const findModuleId = traceFunction("findModuleId", function findModuleId(code: string) {
|
2022-11-03 14:12:50 -04:00
|
|
|
for (const id in wreq.m) {
|
|
|
|
if (wreq.m[id].toString().includes(code)) {
|
|
|
|
return Number(id);
|
|
|
|
}
|
|
|
|
}
|
2022-10-30 15:45:18 -04:00
|
|
|
|
2022-11-03 14:12:50 -04:00
|
|
|
const err = new Error("Didn't find module with code:\n" + code);
|
|
|
|
if (IS_DEV) {
|
2022-11-03 15:36:17 -04:00
|
|
|
if (!devToolsOpen)
|
|
|
|
// Strict behaviour in DevBuilds to fail early and make sure the issue is found
|
|
|
|
throw err;
|
|
|
|
} else {
|
|
|
|
logger.warn(err);
|
2022-11-03 14:12:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2022-11-07 15:05:33 -05:00
|
|
|
});
|
2022-11-03 14:12:50 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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==")
|
|
|
|
* })
|
|
|
|
*/
|
2022-11-07 15:05:33 -05:00
|
|
|
export const mapMangledModule = traceFunction("mapMangledModule", function mapMangledModule<S extends string>(code: string, mappers: Record<S, FilterFn>): Record<S, any> {
|
2022-11-03 14:12:50 -04:00
|
|
|
const exports = {} as Record<S, any>;
|
|
|
|
|
|
|
|
const id = findModuleId(code);
|
|
|
|
if (id === null)
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
const mod = wreq(id);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-14 15:34:35 -04:00
|
|
|
return exports;
|
2022-11-07 15:05:33 -05:00
|
|
|
});
|
2022-10-14 15:34:35 -04:00
|
|
|
|
|
|
|
/**
|
2022-11-03 14:12:50 -04:00
|
|
|
* Same as {@link mapMangledModule} but lazy
|
|
|
|
*/
|
2022-10-14 15:34:35 -04:00
|
|
|
export function mapMangledModuleLazy<S extends string>(code: string, mappers: Record<S, FilterFn>): Record<S, any> {
|
|
|
|
return proxyLazy(() => mapMangledModule(code, mappers));
|
|
|
|
}
|
|
|
|
|
2022-11-25 13:25:35 -05:00
|
|
|
/**
|
|
|
|
* Find the first module that has the specified properties
|
|
|
|
*/
|
2022-08-31 14:47:07 -04:00
|
|
|
export function findByProps(...props: string[]) {
|
2023-09-25 12:27:18 -04:00
|
|
|
const res = find(filters.byProps(...props), { isIndirect: true });
|
|
|
|
if (!res)
|
|
|
|
handleModuleNotFound("findByProps", ...props);
|
|
|
|
return res;
|
2022-08-31 14:47:07 -04:00
|
|
|
}
|
|
|
|
|
2022-11-28 07:37:55 -05:00
|
|
|
/**
|
|
|
|
* findByProps but lazy
|
|
|
|
*/
|
|
|
|
export function findByPropsLazy(...props: string[]) {
|
2023-09-25 12:27:18 -04:00
|
|
|
return proxyLazy(() => findByProps(...props));
|
2022-11-28 07:37:55 -05:00
|
|
|
}
|
|
|
|
|
2022-11-25 13:25:35 -05:00
|
|
|
/**
|
|
|
|
* Find a function by its code
|
|
|
|
*/
|
2022-11-06 12:37:01 -05:00
|
|
|
export function findByCode(...code: string[]) {
|
2023-09-25 12:27:18 -04:00
|
|
|
const res = find(filters.byCode(...code), { isIndirect: true });
|
|
|
|
if (!res)
|
|
|
|
handleModuleNotFound("findByCode", ...code);
|
|
|
|
return res;
|
2022-11-06 12:37:01 -05:00
|
|
|
}
|
|
|
|
|
2022-11-28 07:37:55 -05:00
|
|
|
/**
|
|
|
|
* findByCode but lazy
|
|
|
|
*/
|
|
|
|
export function findByCodeLazy(...code: string[]) {
|
2023-09-25 12:27:18 -04:00
|
|
|
return proxyLazy(() => findByCode(...code));
|
2022-11-28 07:37:55 -05:00
|
|
|
}
|
|
|
|
|
2023-02-08 15:48:26 -05:00
|
|
|
/**
|
|
|
|
* Find a store by its displayName
|
|
|
|
*/
|
2023-02-10 16:33:34 -05:00
|
|
|
export function findStore(name: string) {
|
2023-09-25 12:27:18 -04:00
|
|
|
const res = find(filters.byStoreName(name), { isIndirect: true });
|
|
|
|
if (!res)
|
|
|
|
handleModuleNotFound("findStore", name);
|
|
|
|
return res;
|
2023-02-08 15:48:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-11-22 00:49:08 -05:00
|
|
|
* findStore but lazy
|
2023-02-08 15:48:26 -05:00
|
|
|
*/
|
2023-02-10 16:33:34 -05:00
|
|
|
export function findStoreLazy(name: string) {
|
2023-09-25 12:27:18 -04:00
|
|
|
return proxyLazy(() => findStore(name));
|
2023-02-08 15:48:26 -05:00
|
|
|
}
|
|
|
|
|
2022-11-25 13:25:35 -05:00
|
|
|
/**
|
|
|
|
* Wait for a module that matches the provided filter to be registered,
|
|
|
|
* then call the callback with the module as the first argument
|
|
|
|
*/
|
2022-08-31 14:47:07 -04:00
|
|
|
export function waitFor(filter: string | string[] | FilterFn, callback: CallbackFn) {
|
2022-10-30 15:45:18 -04:00
|
|
|
if (typeof filter === "string")
|
|
|
|
filter = filters.byProps(filter);
|
|
|
|
else if (Array.isArray(filter))
|
|
|
|
filter = filters.byProps(...filter);
|
|
|
|
else if (typeof filter !== "function")
|
|
|
|
throw new Error("filter must be a string, string[] or function, got " + typeof filter);
|
|
|
|
|
2023-09-25 12:27:18 -04:00
|
|
|
const [existing, id] = find(filter!, { isIndirect: true, isWaitFor: true });
|
2023-03-07 23:59:50 -05:00
|
|
|
if (existing) return void callback(existing, id);
|
2022-08-31 14:47:07 -04:00
|
|
|
|
|
|
|
subscriptions.set(filter, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addListener(callback: CallbackFn) {
|
|
|
|
listeners.add(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function removeListener(callback: CallbackFn) {
|
|
|
|
listeners.delete(callback);
|
2022-09-16 15:43:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-03 14:12:50 -04:00
|
|
|
* Search modules by keyword. This searches the factory methods,
|
|
|
|
* meaning you can search all sorts of things, displayName, methodName, strings somewhere in the code, etc
|
|
|
|
* @param filters One or more strings or regexes
|
|
|
|
* @returns Mapping of found modules
|
|
|
|
*/
|
2022-09-16 15:43:38 -04:00
|
|
|
export function search(...filters: Array<string | RegExp>) {
|
|
|
|
const results = {} as Record<number, Function>;
|
|
|
|
const factories = wreq.m;
|
|
|
|
outer:
|
|
|
|
for (const id in factories) {
|
2022-10-01 19:05:15 -04:00
|
|
|
const factory = factories[id].original ?? factories[id];
|
2022-09-16 15:43:38 -04:00
|
|
|
const str: string = factory.toString();
|
|
|
|
for (const filter of filters) {
|
|
|
|
if (typeof filter === "string" && !str.includes(filter)) continue outer;
|
|
|
|
if (filter instanceof RegExp && !filter.test(str)) continue outer;
|
|
|
|
}
|
|
|
|
results[id] = factory;
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-03 14:12:50 -04:00
|
|
|
* Extract a specific module by id into its own Source File. This has no effect on
|
|
|
|
* the code, it is only useful to be able to look at a specific module without having
|
|
|
|
* to view a massive file. extract then returns the extracted module so you can jump to it.
|
|
|
|
* As mentioned above, note that this extracted module is not actually used,
|
|
|
|
* so putting breakpoints or similar will have no effect.
|
|
|
|
* @param id The id of the module to extract
|
|
|
|
*/
|
2022-09-16 15:43:38 -04:00
|
|
|
export function extract(id: number) {
|
|
|
|
const mod = wreq.m[id] as Function;
|
|
|
|
if (!mod) return null;
|
|
|
|
|
|
|
|
const code = `
|
|
|
|
// [EXTRACTED] WebpackModule${id}
|
|
|
|
// WARNING: This module was extracted to be more easily readable.
|
|
|
|
// This module is NOT ACTUALLY USED! This means putting breakpoints will have NO EFFECT!!
|
|
|
|
|
2023-10-25 09:28:50 -04:00
|
|
|
0,${mod.toString()}
|
2022-09-16 15:43:38 -04:00
|
|
|
//# sourceURL=ExtractedWebpackModule${id}
|
|
|
|
`;
|
|
|
|
const extracted = (0, eval)(code);
|
|
|
|
return extracted as Function;
|
2022-09-16 16:59:34 -04:00
|
|
|
}
|