2022-09-05 13:54:02 -04:00
|
|
|
import type { WebpackInstance } from "discord-types/other";
|
|
|
|
|
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 = {
|
|
|
|
byProps: (props: string[]): FilterFn =>
|
|
|
|
props.length === 1
|
|
|
|
? m => m[props[0]] !== void 0
|
|
|
|
: m => props.every(p => m[p] !== void 0),
|
2022-09-27 10:57:46 -04:00
|
|
|
byDisplayName: (deezNuts: string): FilterFn => m => m.default?.displayName === deezNuts,
|
|
|
|
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
|
|
|
},
|
2022-08-31 14:47:07 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
export const subscriptions = new Map<FilterFn, CallbackFn>();
|
|
|
|
export const listeners = new Set<CallbackFn>();
|
|
|
|
|
|
|
|
export type CallbackFn = (mod: any) => void;
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-09-05 13:54:02 -04:00
|
|
|
wreq = instance.push([[Symbol()], {}, (r) => r]);
|
|
|
|
cache = wreq.c;
|
2022-08-31 14:47:07 -04:00
|
|
|
instance.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function find(filter: FilterFn, getDefault = true) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (filter(mod.exports))
|
2022-08-31 14:47:07 -04:00
|
|
|
return mod.exports;
|
2022-09-28 16:49:46 -04:00
|
|
|
|
|
|
|
if (typeof mod.exports !== "object") continue;
|
|
|
|
|
2022-09-27 08:34:57 -04:00
|
|
|
if (mod.exports.default && filter(mod.exports.default))
|
2022-08-31 14:47:07 -04:00
|
|
|
return getDefault ? mod.exports.default : mod.exports;
|
2022-09-28 16:49:46 -04:00
|
|
|
|
|
|
|
// is 3 is the longest obfuscated export?
|
|
|
|
// the length check makes search about 20% faster
|
|
|
|
for (const nestedMod in mod.exports) if (nestedMod.length < 3) {
|
2022-09-27 08:34:57 -04:00
|
|
|
const nested = mod.exports[nestedMod];
|
|
|
|
if (nested && filter(nested)) return nested;
|
|
|
|
}
|
2022-08-31 14:47:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-09-27 08:34:57 -04:00
|
|
|
// TODO fix
|
2022-08-31 14:47:07 -04:00
|
|
|
export function findAll(filter: FilterFn, getDefault = true) {
|
2022-09-30 20:27:28 -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))
|
|
|
|
ret.push(getDefault ? mod.exports.default : mod.exports);
|
|
|
|
else for (const nestedMod in mod.exports) if (nestedMod.length < 3) {
|
|
|
|
const nested = mod.exports[nestedMod];
|
|
|
|
if (nested && filter(nested)) ret.push(nested);
|
|
|
|
}
|
2022-08-31 14:47:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function findByProps(...props: string[]) {
|
|
|
|
return find(filters.byProps(props));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function findAllByProps(...props: string[]) {
|
|
|
|
return findAll(filters.byProps(props));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function findByDisplayName(deezNuts: string) {
|
|
|
|
return find(filters.byDisplayName(deezNuts));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function waitFor(filter: string | string[] | FilterFn, callback: CallbackFn) {
|
|
|
|
if (typeof filter === "string") filter = filters.byProps([filter]);
|
|
|
|
else if (Array.isArray(filter)) filter = filters.byProps(filter);
|
2022-09-30 20:27:28 -04:00
|
|
|
else if (typeof filter !== "function") throw new Error("filter must be a string, string[] or function, got " + typeof filter);
|
2022-08-31 14:47:07 -04:00
|
|
|
|
|
|
|
const existing = find(filter!);
|
|
|
|
if (existing) return void callback(existing);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
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-09-16 16:12:34 -04:00
|
|
|
* Extract a specific module by id into its own Source File. This has no effect on
|
2022-09-16 15:43:38 -04:00
|
|
|
* 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.
|
2022-09-16 16:59:34 -04:00
|
|
|
* As mentioned above, note that this extracted module is not actually used,
|
2022-09-16 15:43:38 -04:00
|
|
|
* so putting breakpoints or similar will have no effect.
|
|
|
|
* @param id The id of the module to extract
|
|
|
|
*/
|
|
|
|
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!!
|
|
|
|
|
|
|
|
${mod.toString()}
|
|
|
|
//# sourceURL=ExtractedWebpackModule${id}
|
|
|
|
`;
|
|
|
|
const extracted = (0, eval)(code);
|
|
|
|
return extracted as Function;
|
2022-09-16 16:59:34 -04:00
|
|
|
}
|