mirror of
https://github.com/Equicord/Equicord.git
synced 2025-06-14 00:53:04 -04:00
feat: Context Menu API (#496)
This commit is contained in:
parent
40395d562a
commit
1b199ec5d8
5 changed files with 236 additions and 13 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue