From 1b199ec5d8e0ca3805a9960323ddd267561b4cf6 Mon Sep 17 00:00:00 2001 From: Nuckyz <61953774+Nuckyz@users.noreply.github.com> Date: Wed, 8 Mar 2023 01:59:50 -0300 Subject: feat: Context Menu API (#496) --- src/webpack/webpack.ts | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'src/webpack/webpack.ts') 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(); export const listeners = new Set(); -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); } -- cgit