diff options
Diffstat (limited to 'src/webpack')
-rw-r--r-- | src/webpack/patchWebpack.ts | 10 | ||||
-rw-r--r-- | src/webpack/webpack.ts | 23 |
2 files changed, 20 insertions, 13 deletions
diff --git a/src/webpack/patchWebpack.ts b/src/webpack/patchWebpack.ts index 19ca951..697ce94 100644 --- a/src/webpack/patchWebpack.ts +++ b/src/webpack/patchWebpack.ts @@ -92,9 +92,11 @@ function patchPush() { return; } + const numberId = Number(id); + for (const callback of listeners) { try { - callback(exports); + callback(exports, numberId); } catch (err) { logger.error("Error in webpack listener", err); } @@ -104,17 +106,17 @@ function patchPush() { try { if (filter(exports)) { subscriptions.delete(filter); - callback(exports); + callback(exports, numberId); } else if (typeof exports === "object") { if (exports.default && filter(exports.default)) { subscriptions.delete(filter); - callback(exports.default); + 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]); + callback(exports[nested], numberId); } } } diff --git a/src/webpack/webpack.ts b/src/webpack/webpack.ts index 98a0ea8..0d95587 100644 --- a/src/webpack/webpack.ts +++ b/src/webpack/webpack.ts @@ -57,7 +57,7 @@ export const filters = { export const subscriptions = new Map<FilterFn, CallbackFn>(); export const listeners = new Set<CallbackFn>(); -export type CallbackFn = (mod: any) => void; +export type CallbackFn = (mod: any, id: number) => void; export function _initWebpack(instance: typeof window.webpackChunkdiscord_app) { if (cache !== void 0) throw "no."; @@ -86,18 +86,23 @@ export const find = traceFunction("find", function find(filter: FilterFn, getDef const mod = cache[key]; if (!mod?.exports) continue; - if (filter(mod.exports)) - return mod.exports; + if (filter(mod.exports)) { + return isWaitFor ? [mod.exports, Number(key)] : mod.exports; + } if (typeof mod.exports !== "object") continue; - if (mod.exports.default && filter(mod.exports.default)) - return getDefault ? mod.exports.default : mod.exports; + if (mod.exports.default && filter(mod.exports.default)) { + const found = getDefault ? mod.exports.default : mod.exports; + return isWaitFor ? [found, Number(key)] : found; + } // the length check makes search about 20% faster for (const nestedMod in mod.exports) if (nestedMod.length <= 3) { const nested = mod.exports[nestedMod]; - if (nested && filter(nested)) return nested; + if (nested && filter(nested)) { + return isWaitFor ? [nested, Number(key)] : nested; + } } } @@ -112,7 +117,7 @@ export const find = traceFunction("find", function find(filter: FilterFn, getDef } } - return null; + return isWaitFor ? [null, null] : null; }); /** @@ -347,8 +352,8 @@ export function waitFor(filter: string | string[] | FilterFn, callback: Callback else if (typeof filter !== "function") throw new Error("filter must be a string, string[] or function, got " + typeof filter); - const existing = find(filter!, true, true); - if (existing) return void callback(existing); + const [existing, id] = find(filter!, true, true); + if (existing) return void callback(existing, id); subscriptions.set(filter, callback); } |