Future proof ContextMenuAPI against mangled export
This commit is contained in:
parent
a01ee40591
commit
426c949ee4
|
@ -121,7 +121,7 @@ export function findGroupChildrenByChildId(id: string | string[], children: Arra
|
|||
}
|
||||
|
||||
interface ContextMenuProps {
|
||||
contextMenuApiArguments?: Array<any>;
|
||||
contextMenuAPIArguments?: Array<any>;
|
||||
navId: string;
|
||||
children: Array<ReactElement | null>;
|
||||
"aria-label": string;
|
||||
|
@ -135,7 +135,7 @@ export function _usePatchContextMenu(props: ContextMenuProps) {
|
|||
children: cloneMenuChildren(props.children),
|
||||
};
|
||||
|
||||
props.contextMenuApiArguments ??= [];
|
||||
props.contextMenuAPIArguments ??= [];
|
||||
const contextMenuPatches = navPatches.get(props.navId);
|
||||
|
||||
if (!Array.isArray(props.children)) props.children = [props.children];
|
||||
|
@ -143,7 +143,7 @@ export function _usePatchContextMenu(props: ContextMenuProps) {
|
|||
if (contextMenuPatches) {
|
||||
for (const patch of contextMenuPatches) {
|
||||
try {
|
||||
patch(props.children, ...props.contextMenuApiArguments);
|
||||
patch(props.children, ...props.contextMenuAPIArguments);
|
||||
} catch (err) {
|
||||
ContextMenuLogger.error(`Patch for ${props.navId} errored,`, err);
|
||||
}
|
||||
|
@ -152,7 +152,7 @@ export function _usePatchContextMenu(props: ContextMenuProps) {
|
|||
|
||||
for (const patch of globalPatches) {
|
||||
try {
|
||||
patch(props.navId, props.children, ...props.contextMenuApiArguments);
|
||||
patch(props.navId, props.children, ...props.contextMenuAPIArguments);
|
||||
} catch (err) {
|
||||
ContextMenuLogger.error("Global patch errored,", err);
|
||||
}
|
||||
|
|
|
@ -18,6 +18,38 @@
|
|||
|
||||
import { Devs } from "@utils/constants";
|
||||
import definePlugin from "@utils/types";
|
||||
import { filters, waitFor, waitForSubscriptions } from "@webpack";
|
||||
|
||||
/**
|
||||
* The last var name which the ContextMenu module was WebpackRequire'd and assigned to
|
||||
*/
|
||||
let lastVarName = "";
|
||||
|
||||
/**
|
||||
* The key exporting the ContextMenu module "Menu"
|
||||
*/
|
||||
let exportKey: PropertyKey = "";
|
||||
|
||||
/**
|
||||
* The id of the module exporting the ContextMenu module "Menu"
|
||||
*/
|
||||
let modId: PropertyKey = "";
|
||||
|
||||
let mangledCallback: (...args: any[]) => any;
|
||||
waitFor(filters.byCode("Menu API only allows Items and groups of Items as children."), mangledCallback = (_, modInfo) => {
|
||||
exportKey = modInfo.exportKey;
|
||||
modId = modInfo.id;
|
||||
|
||||
waitForSubscriptions.delete(nonMangledCallback);
|
||||
});
|
||||
|
||||
let nonMangledCallback: (...args: any[]) => any;
|
||||
waitFor(filters.byProps("Menu", "MenuItem"), nonMangledCallback = (_, modInfo) => {
|
||||
exportKey = "Menu";
|
||||
modId = modInfo.id;
|
||||
|
||||
waitForSubscriptions.delete(mangledCallback);
|
||||
});
|
||||
|
||||
export default definePlugin({
|
||||
name: "ContextMenuAPI",
|
||||
|
@ -34,12 +66,26 @@ export default definePlugin({
|
|||
}
|
||||
},
|
||||
{
|
||||
find: ".Menu,{",
|
||||
find: "navId:",
|
||||
all: true,
|
||||
replacement: {
|
||||
match: /Menu,{(?<=\.jsxs?\)\(\i\.Menu,{)/g,
|
||||
replace: "$&contextMenuApiArguments:typeof arguments!=='undefined'?arguments:[],"
|
||||
}
|
||||
noWarn: true,
|
||||
replacement: [
|
||||
{
|
||||
get match() {
|
||||
return RegExp(`${String(modId)}(?<=(\\i)=.+?)`);
|
||||
},
|
||||
replace: (id, varName) => {
|
||||
lastVarName = varName;
|
||||
return id;
|
||||
}
|
||||
},
|
||||
{
|
||||
get match() {
|
||||
return RegExp(`${String(exportKey)},{(?<=${lastVarName}\\.${String(exportKey)},{)`, "g");
|
||||
},
|
||||
replace: "$&contextMenuAPIArguments:typeof arguments!=='undefined'?arguments:[],"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
});
|
||||
|
|
|
@ -24,7 +24,7 @@ import { WebpackInstance } from "discord-types/other";
|
|||
|
||||
import { traceFunction } from "../debug/Tracer";
|
||||
import { patches } from "../plugins";
|
||||
import { _initWebpack, beforeInitListeners, factoryListeners, moduleListeners, subscriptions, wreq } from ".";
|
||||
import { _initWebpack, beforeInitListeners, factoryListeners, moduleListeners, waitForSubscriptions, wreq } from ".";
|
||||
|
||||
const logger = new Logger("WebpackInterceptor", "#8caaee");
|
||||
const initCallbackRegex = canonicalizeMatch(/{return \i\(".+?"\)}/);
|
||||
|
@ -204,8 +204,7 @@ function patchFactories(factories: Record<string, (module: any, exports: any, re
|
|||
}
|
||||
|
||||
exports = module.exports;
|
||||
|
||||
if (!exports) return;
|
||||
if (exports == null) return;
|
||||
|
||||
// There are (at the time of writing) 11 modules exporting the window
|
||||
// Make these non enumerable to improve webpack search performance
|
||||
|
@ -240,32 +239,37 @@ function patchFactories(factories: Record<string, (module: any, exports: any, re
|
|||
|
||||
for (const callback of moduleListeners) {
|
||||
try {
|
||||
callback(exports, id);
|
||||
callback(exports, { id, factory: originalMod });
|
||||
} catch (err) {
|
||||
logger.error("Error in Webpack module listener:\n", err, callback);
|
||||
}
|
||||
}
|
||||
|
||||
for (const [filter, callback] of subscriptions) {
|
||||
for (const [filter, callback] of waitForSubscriptions) {
|
||||
try {
|
||||
if (exports && filter(exports)) {
|
||||
subscriptions.delete(filter);
|
||||
callback(exports, id);
|
||||
} else if (typeof exports === "object") {
|
||||
if (exports.default && filter(exports.default)) {
|
||||
subscriptions.delete(filter);
|
||||
callback(exports.default, id);
|
||||
} else {
|
||||
for (const nested in exports) if (nested.length <= 3) {
|
||||
if (exports[nested] && filter(exports[nested])) {
|
||||
subscriptions.delete(filter);
|
||||
callback(exports[nested], id);
|
||||
}
|
||||
}
|
||||
if (filter(exports)) {
|
||||
waitForSubscriptions.delete(filter);
|
||||
callback(exports, { id, factory: originalMod, exportKey: "" });
|
||||
continue;
|
||||
}
|
||||
|
||||
if (typeof exports !== "object") continue;
|
||||
|
||||
if (exports.default != null && filter(exports.default)) {
|
||||
waitForSubscriptions.delete(filter);
|
||||
callback(exports.default, { id, factory: originalMod, exportKey: "default" });
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const key in exports) if (key.length <= 3) {
|
||||
if (exports[key] != null && filter(exports[key])) {
|
||||
waitForSubscriptions.delete(filter);
|
||||
callback(exports[key], { id, factory: originalMod, exportKey: key });
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error("Error while firing callback for Webpack subscription:\n", err, filter, callback);
|
||||
logger.error("Error while firing callback for Webpack waitFor subscription:\n", err, filter, callback);
|
||||
}
|
||||
}
|
||||
} as any as { toString: () => string, original: any, (...args: any[]): void; };
|
||||
|
|
|
@ -68,11 +68,21 @@ export const filters = {
|
|||
}
|
||||
};
|
||||
|
||||
export type CallbackFn = (mod: any, id: string) => void;
|
||||
export type ModListenerInfo = {
|
||||
id: PropertyKey;
|
||||
factory: (module: any, exports: any, require: WebpackInstance) => void;
|
||||
};
|
||||
|
||||
export type ModCallbackInfo = ModListenerInfo & {
|
||||
exportKey: PropertyKey;
|
||||
};
|
||||
|
||||
export type ModListenerFn = (module: any, info: ModListenerInfo) => void;
|
||||
export type ModCallbackFn = (module: any, info: ModCallbackInfo) => void;
|
||||
|
||||
export const subscriptions = new Map<FilterFn, CallbackFn>();
|
||||
export const moduleListeners = new Set<CallbackFn>();
|
||||
export const factoryListeners = new Set<(factory: (module: any, exports: any, require: WebpackInstance) => void) => void>();
|
||||
export const moduleListeners = new Set<ModListenerFn>();
|
||||
export const waitForSubscriptions = new Map<FilterFn, ModCallbackFn>();
|
||||
export const beforeInitListeners = new Set<(wreq: WebpackInstance) => void>();
|
||||
|
||||
export function _initWebpack(webpackRequire: WebpackInstance) {
|
||||
|
@ -106,7 +116,7 @@ export const find = traceFunction("find", function find(filter: FilterFn, { isIn
|
|||
|
||||
for (const key in cache) {
|
||||
const mod = cache[key];
|
||||
if (!mod.loaded || !mod?.exports) continue;
|
||||
if (!mod?.loaded || mod?.exports == null) continue;
|
||||
|
||||
if (filter(mod.exports)) {
|
||||
return isWaitFor ? [mod.exports, key] : mod.exports;
|
||||
|
@ -114,16 +124,13 @@ export const find = traceFunction("find", function find(filter: FilterFn, { isIn
|
|||
|
||||
if (typeof mod.exports !== "object") continue;
|
||||
|
||||
if (mod.exports.default && filter(mod.exports.default)) {
|
||||
const found = mod.exports.default;
|
||||
return isWaitFor ? [found, key] : found;
|
||||
if (mod.exports.default != null && filter(mod.exports.default)) {
|
||||
return isWaitFor ? [mod.exports.default, key] : mod.exports.default;
|
||||
}
|
||||
|
||||
// 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 isWaitFor ? [nested, key] : nested;
|
||||
for (const key in mod.exports) if (key.length <= 3) {
|
||||
if (mod.exports[key] != null && filter(mod.exports[key])) {
|
||||
return isWaitFor ? [mod.exports[key], key] : mod.exports[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -142,18 +149,25 @@ export function findAll(filter: FilterFn) {
|
|||
const ret = [] as any[];
|
||||
for (const key in cache) {
|
||||
const mod = cache[key];
|
||||
if (!mod.loaded || !mod?.exports) continue;
|
||||
if (!mod?.loaded || mod?.exports == null) continue;
|
||||
|
||||
if (filter(mod.exports))
|
||||
if (filter(mod.exports)) {
|
||||
ret.push(mod.exports);
|
||||
else if (typeof mod.exports !== "object")
|
||||
continue;
|
||||
}
|
||||
|
||||
if (mod.exports.default && filter(mod.exports.default))
|
||||
if (typeof mod.exports !== "object") continue;
|
||||
|
||||
if (mod.exports.default != null && filter(mod.exports.default)) {
|
||||
ret.push(mod.exports.default);
|
||||
else for (const nestedMod in mod.exports) if (nestedMod.length <= 3) {
|
||||
const nested = mod.exports[nestedMod];
|
||||
if (nested && filter(nested)) ret.push(nested);
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const key in mod.exports) if (key.length <= 3) {
|
||||
if (mod.exports[key] && filter(mod.exports[key])) {
|
||||
ret.push(mod.exports[key]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -190,7 +204,7 @@ export const findBulk = traceFunction("findBulk", function findBulk(...filterFns
|
|||
outer:
|
||||
for (const key in cache) {
|
||||
const mod = cache[key];
|
||||
if (!mod.loaded || !mod?.exports) continue;
|
||||
if (!mod.loaded || mod?.exports == null) continue;
|
||||
|
||||
for (let j = 0; j < length; j++) {
|
||||
const filter = filters[j];
|
||||
|
@ -204,26 +218,23 @@ export const findBulk = traceFunction("findBulk", function findBulk(...filterFns
|
|||
break;
|
||||
}
|
||||
|
||||
if (typeof mod.exports !== "object")
|
||||
continue;
|
||||
if (typeof mod.exports !== "object") continue;
|
||||
|
||||
if (mod.exports.default && filter(mod.exports.default)) {
|
||||
results[j] = mod.exports.default;
|
||||
filters[j] = undefined;
|
||||
if (++found === length) break outer;
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const nestedMod in mod.exports)
|
||||
if (nestedMod.length <= 3) {
|
||||
const nested = mod.exports[nestedMod];
|
||||
if (nested && filter(nested)) {
|
||||
results[j] = nested;
|
||||
filters[j] = undefined;
|
||||
if (++found === length) break outer;
|
||||
continue outer;
|
||||
}
|
||||
for (const key in mod.exports) if (key.length <= 3) {
|
||||
if (mod.exports[key] && filter(mod.exports[key])) {
|
||||
results[j] = mod.exports[key];
|
||||
filters[j] = undefined;
|
||||
if (++found === length) break outer;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -293,7 +304,7 @@ export const lazyWebpackSearchHistory = [] as Array<["find" | "findByProps" | "f
|
|||
* Note that the example below exists already as an api, see {@link findByPropsLazy}
|
||||
* @example const mod = proxyLazy(() => findByProps("blah")); console.log(mod.blah);
|
||||
*/
|
||||
export function proxyLazyWebpack<T = any>(factory: () => any, attempts?: number) {
|
||||
export function proxyLazyWebpack<T = any>(factory: () => T, attempts?: number) {
|
||||
if (IS_REPORTER) lazyWebpackSearchHistory.push(["proxyLazyWebpack", [factory]]);
|
||||
|
||||
return proxyLazy<T>(factory, attempts);
|
||||
|
@ -446,25 +457,27 @@ export function findExportedComponentLazy<T extends object = any>(...props: stri
|
|||
* })
|
||||
*/
|
||||
export const mapMangledModule = traceFunction("mapMangledModule", function mapMangledModule<S extends string>(code: string, mappers: Record<S, FilterFn>): Record<S, any> {
|
||||
const exports = {} as Record<S, any>;
|
||||
const mapping = {} as Record<S, any>;
|
||||
|
||||
const id = findModuleId(code);
|
||||
if (id === null)
|
||||
return exports;
|
||||
if (id === null) return mapping;
|
||||
|
||||
const exports = wreq(id as any);
|
||||
|
||||
const mod = wreq(id as any);
|
||||
outer:
|
||||
for (const key in mod) {
|
||||
const member = mod[key];
|
||||
for (const key in exports) {
|
||||
const value = exports[key];
|
||||
|
||||
for (const newName in mappers) {
|
||||
// if the current mapper matches this module
|
||||
if (mappers[newName](member)) {
|
||||
exports[newName] = member;
|
||||
if (mappers[newName](value)) {
|
||||
mapping[newName] = value;
|
||||
continue outer;
|
||||
}
|
||||
}
|
||||
}
|
||||
return exports;
|
||||
|
||||
return mapping;
|
||||
});
|
||||
|
||||
/**
|
||||
|
@ -570,7 +583,7 @@ export function extractAndLoadChunksLazy(code: string[], matcher = DefaultExtrac
|
|||
* Wait for a module that matches the provided filter to be registered,
|
||||
* then call the callback with the module as the first argument
|
||||
*/
|
||||
export function waitFor(filter: string | string[] | FilterFn, callback: CallbackFn, { isIndirect = false }: { isIndirect?: boolean; } = {}) {
|
||||
export function waitFor(filter: string | string[] | FilterFn, callback: ModCallbackFn, { isIndirect = false }: { isIndirect?: boolean; } = {}) {
|
||||
if (IS_REPORTER && !isIndirect) lazyWebpackSearchHistory.push(["waitFor", Array.isArray(filter) ? filter : [filter]]);
|
||||
|
||||
if (typeof filter === "string")
|
||||
|
@ -585,7 +598,7 @@ export function waitFor(filter: string | string[] | FilterFn, callback: Callback
|
|||
if (existing) return void callback(existing, id);
|
||||
}
|
||||
|
||||
subscriptions.set(filter, callback);
|
||||
waitForSubscriptions.set(filter, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue