feat: Context Menu API (#496)

This commit is contained in:
Nuckyz 2023-03-08 01:59:50 -03:00 committed by GitHub
parent 40395d562a
commit 1b199ec5d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 236 additions and 13 deletions

View file

@ -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);
}