2026-08-01 14:37:08 -04:00
|
|
|
import { React } from "@vendetta/metro/common";
|
2026-08-09 02:52:10 -04:00
|
|
|
import { after } from "@vendetta/patcher";
|
2026-08-01 14:37:08 -04:00
|
|
|
|
|
|
|
|
interface Intercept {
|
|
|
|
|
replacement: React.ComponentType<any>;
|
|
|
|
|
extraProps?: Record<string, any>;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-01 15:28:50 -04:00
|
|
|
interface PropsIntercept {
|
|
|
|
|
predicate: (props: any) => boolean;
|
|
|
|
|
replacement: React.ComponentType<any> | null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-09 02:20:50 -04:00
|
|
|
interface PropsTransform {
|
|
|
|
|
predicate: (props: any) => boolean;
|
|
|
|
|
transform: (props: any) => any;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-01 21:05:22 -04:00
|
|
|
interface TypeDetector {
|
2026-08-09 02:20:50 -04:00
|
|
|
key: string;
|
2026-08-01 21:05:22 -04:00
|
|
|
predicate: (type: any) => boolean;
|
|
|
|
|
onDetected: (type: any) => void;
|
2026-08-09 02:20:50 -04:00
|
|
|
persistent: boolean;
|
|
|
|
|
justFired?: boolean;
|
2026-08-01 21:05:22 -04:00
|
|
|
}
|
|
|
|
|
|
2026-08-01 14:37:08 -04:00
|
|
|
const intercepts = new Map<React.ComponentType<any>, Intercept>();
|
2026-08-01 15:28:50 -04:00
|
|
|
const propsIntercepts: PropsIntercept[] = [];
|
2026-08-09 02:20:50 -04:00
|
|
|
const propsTransforms: PropsTransform[] = [];
|
2026-08-01 21:05:22 -04:00
|
|
|
let typeDetectors: TypeDetector[] = [];
|
2026-08-09 02:20:50 -04:00
|
|
|
const detectorKeys = new Set<string>();
|
2026-08-01 14:37:08 -04:00
|
|
|
let isPatched = false;
|
|
|
|
|
|
|
|
|
|
export function registerIntercept(
|
|
|
|
|
original: React.ComponentType<any>,
|
|
|
|
|
replacement: React.ComponentType<any>,
|
|
|
|
|
extraProps?: Record<string, any>,
|
|
|
|
|
) {
|
|
|
|
|
intercepts.set(original, { replacement, extraProps });
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 22:56:28 -04:00
|
|
|
// Matches on a props predicate instead of an exact type reference, for components with no
|
|
|
|
|
// reliable name. Pass replacement: null to render nothing.
|
2026-08-01 15:28:50 -04:00
|
|
|
export function registerPropsIntercept(predicate: (props: any) => boolean, replacement: React.ComponentType<any> | null) {
|
|
|
|
|
propsIntercepts.push({ predicate, replacement });
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 22:56:28 -04:00
|
|
|
// Rewrites props in place, keeping the same type.
|
2026-08-09 02:20:50 -04:00
|
|
|
export function registerPropsTransform(predicate: (props: any) => boolean, transform: (props: any) => any) {
|
|
|
|
|
propsTransforms.push({ predicate, transform });
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 22:56:28 -04:00
|
|
|
// Fires onDetected(type) the first time a matching element is created. For components with no
|
|
|
|
|
// stable module export to search for after the fact. persistent keeps firing on every match
|
|
|
|
|
// instead of once, for references that change across navigation.
|
2026-08-09 02:20:50 -04:00
|
|
|
export function registerTypeDetector(
|
|
|
|
|
key: string,
|
|
|
|
|
predicate: (type: any) => boolean,
|
|
|
|
|
onDetected: (type: any) => void,
|
|
|
|
|
options?: { persistent?: boolean },
|
|
|
|
|
) {
|
|
|
|
|
if (detectorKeys.has(key)) return;
|
|
|
|
|
detectorKeys.add(key);
|
|
|
|
|
typeDetectors.push({ key, predicate, onDetected, persistent: options?.persistent ?? false });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function hasTypeDetector(key: string): boolean {
|
|
|
|
|
return detectorKeys.has(key);
|
2026-08-01 21:05:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function runTypeDetectors(type: any) {
|
|
|
|
|
if (typeDetectors.length === 0) return;
|
2026-08-09 02:20:50 -04:00
|
|
|
|
|
|
|
|
let consumed = false;
|
2026-08-01 21:05:22 -04:00
|
|
|
for (const detector of typeDetectors) {
|
|
|
|
|
let matched = false;
|
|
|
|
|
try {
|
|
|
|
|
matched = detector.predicate(type);
|
|
|
|
|
} catch {
|
2026-08-11 22:56:28 -04:00
|
|
|
// Ignore
|
2026-08-01 21:05:22 -04:00
|
|
|
}
|
2026-08-09 02:20:50 -04:00
|
|
|
if (!matched) continue;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
detector.onDetected(type);
|
|
|
|
|
} catch {
|
2026-08-11 22:56:28 -04:00
|
|
|
// Ignore
|
2026-08-01 21:05:22 -04:00
|
|
|
}
|
2026-08-09 02:20:50 -04:00
|
|
|
if (!detector.persistent) {
|
|
|
|
|
detector.justFired = true;
|
|
|
|
|
consumed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (consumed) {
|
|
|
|
|
typeDetectors = typeDetectors.filter((d) => d.persistent || !d.justFired);
|
2026-08-01 21:05:22 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-01 15:43:09 -04:00
|
|
|
/** Returns a replacement type if this element should be intercepted, `null` to render nothing, or `undefined` to pass through unchanged. */
|
2026-08-09 02:20:50 -04:00
|
|
|
function resolveReplacement(type: any, props: any, rest: any[]): { type: any; props: any } | null | undefined {
|
2026-08-01 21:05:22 -04:00
|
|
|
runTypeDetectors(type);
|
|
|
|
|
|
2026-08-09 02:20:50 -04:00
|
|
|
let effectiveProps = props;
|
|
|
|
|
if (effectiveProps) {
|
|
|
|
|
for (const { predicate, transform } of propsTransforms) {
|
|
|
|
|
try {
|
|
|
|
|
if (predicate(effectiveProps, type, rest)) {
|
|
|
|
|
effectiveProps = transform(effectiveProps);
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
2026-08-11 22:56:28 -04:00
|
|
|
// Ignore
|
2026-08-09 02:20:50 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (effectiveProps) {
|
2026-08-01 15:43:09 -04:00
|
|
|
for (const { predicate, replacement } of propsIntercepts) {
|
|
|
|
|
try {
|
2026-08-09 02:20:50 -04:00
|
|
|
if (predicate(effectiveProps, type, rest)) {
|
|
|
|
|
return replacement ? { type: replacement, props: effectiveProps } : null;
|
2026-08-01 15:43:09 -04:00
|
|
|
}
|
|
|
|
|
} catch {
|
2026-08-11 22:56:28 -04:00
|
|
|
// Ignore
|
2026-08-01 15:43:09 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const entry = intercepts.get(type);
|
|
|
|
|
if (entry) {
|
2026-08-09 02:20:50 -04:00
|
|
|
return { type: entry.replacement, props: entry.extraProps ? { ...effectiveProps, ...entry.extraProps } : effectiveProps };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (effectiveProps !== props) {
|
|
|
|
|
return { type, props: effectiveProps };
|
2026-08-01 15:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 22:56:28 -04:00
|
|
|
// after() only sees the return value, not the call itself, so we mutate the element in place
|
|
|
|
|
// rather than substituting args beforehand.
|
2026-08-09 02:52:10 -04:00
|
|
|
function applyResolved(res: any, type: any, props: any, rest: any[]) {
|
|
|
|
|
if (!res || typeof res !== "object") return res;
|
|
|
|
|
const resolved = resolveReplacement(type, props, rest);
|
|
|
|
|
if (resolved === null) {
|
|
|
|
|
res.type = () => null;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
if (resolved) {
|
|
|
|
|
res.type = resolved.type;
|
|
|
|
|
res.props = resolved.props ?? null;
|
|
|
|
|
}
|
|
|
|
|
return res;
|
2026-08-09 02:20:50 -04:00
|
|
|
}
|
|
|
|
|
|
2026-08-01 14:37:08 -04:00
|
|
|
export function patchCreateElement(cleanups: (() => void)[]) {
|
|
|
|
|
if (isPatched) return;
|
2026-08-09 02:52:10 -04:00
|
|
|
isPatched = true;
|
2026-08-01 14:37:08 -04:00
|
|
|
|
2026-08-09 02:52:10 -04:00
|
|
|
if (React.createElement) {
|
|
|
|
|
cleanups.push(
|
|
|
|
|
after("createElement", React, (args: any[], res: any) => applyResolved(res, args[0], args[1], args.slice(2)))
|
|
|
|
|
);
|
|
|
|
|
} else {
|
2026-08-01 21:05:22 -04:00
|
|
|
console.warn("[createElementIntercept] React.createElement is null, skipping patch");
|
2026-08-01 14:37:08 -04:00
|
|
|
}
|
|
|
|
|
|
2026-08-11 22:56:28 -04:00
|
|
|
// Discord compiles JSX through jsx/jsxs/jsxDEV, not React.createElement - scan for those
|
|
|
|
|
// runtimes by shape (names are mangled) and keep scanning since Metro registers lazily.
|
2026-08-09 02:20:50 -04:00
|
|
|
const patchedJsxRuntimes = new WeakSet<any>();
|
|
|
|
|
|
|
|
|
|
function isJsxRuntime(m: any): boolean {
|
|
|
|
|
return typeof m?.jsx === "function" || typeof m?.jsxs === "function" || typeof m?.jsxDEV === "function";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function patchJsxObject(runtime: any) {
|
|
|
|
|
if (patchedJsxRuntimes.has(runtime)) return;
|
|
|
|
|
patchedJsxRuntimes.add(runtime);
|
|
|
|
|
for (const key of ["jsx", "jsxs", "jsxDEV"] as const) {
|
2026-08-09 02:52:10 -04:00
|
|
|
if (typeof runtime[key] !== "function") continue;
|
|
|
|
|
cleanups.push(
|
|
|
|
|
after(key, runtime, (args: any[], res: any) => applyResolved(res, args[0], args[1], args.slice(2)))
|
|
|
|
|
);
|
2026-08-09 02:20:50 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function patchJsxModule(def: any) {
|
|
|
|
|
if (!def?.publicModule?.exports) return;
|
|
|
|
|
const exports = def.publicModule.exports;
|
|
|
|
|
|
|
|
|
|
try {
|
2026-08-09 02:52:10 -04:00
|
|
|
if (isJsxRuntime(exports)) patchJsxObject(exports);
|
2026-08-09 02:20:50 -04:00
|
|
|
|
|
|
|
|
const dflt = exports.default;
|
2026-08-09 02:52:10 -04:00
|
|
|
if (dflt != null && isJsxRuntime(dflt)) patchJsxObject(dflt);
|
2026-08-09 02:20:50 -04:00
|
|
|
} catch {
|
2026-08-11 22:56:28 -04:00
|
|
|
// Ignore
|
2026-08-09 02:20:50 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function scanAndPatchJsxRuntimes() {
|
|
|
|
|
const modules = (window as any)?.modules;
|
|
|
|
|
if (!modules) return;
|
|
|
|
|
for (const id in modules) {
|
|
|
|
|
const def = modules[id];
|
|
|
|
|
if (!def?.isInitialized) continue;
|
|
|
|
|
patchJsxModule(def);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scanAndPatchJsxRuntimes();
|
|
|
|
|
|
2026-08-11 22:56:28 -04:00
|
|
|
// Fast while the app boots, then slow, then stop.
|
2026-08-09 02:20:50 -04:00
|
|
|
let ticks = 0;
|
|
|
|
|
let timer: ReturnType<typeof setInterval> | undefined = setInterval(() => {
|
|
|
|
|
scanAndPatchJsxRuntimes();
|
|
|
|
|
if (++ticks === 50 && timer) { // ~5s at 100ms
|
|
|
|
|
clearInterval(timer);
|
|
|
|
|
timer = setInterval(() => {
|
|
|
|
|
scanAndPatchJsxRuntimes();
|
|
|
|
|
if (++ticks >= 75 && timer) { // + ~25s at 1s
|
|
|
|
|
clearInterval(timer);
|
|
|
|
|
timer = undefined;
|
|
|
|
|
}
|
|
|
|
|
}, 1000);
|
|
|
|
|
}
|
|
|
|
|
}, 100);
|
2026-08-01 15:43:09 -04:00
|
|
|
|
2026-08-01 14:37:08 -04:00
|
|
|
cleanups.push(() => {
|
2026-08-09 02:20:50 -04:00
|
|
|
if (timer) clearInterval(timer);
|
|
|
|
|
timer = undefined;
|
2026-08-09 02:52:10 -04:00
|
|
|
isPatched = false;
|
2026-08-01 14:37:08 -04:00
|
|
|
intercepts.clear();
|
2026-08-01 15:28:50 -04:00
|
|
|
propsIntercepts.length = 0;
|
2026-08-09 02:20:50 -04:00
|
|
|
propsTransforms.length = 0;
|
2026-08-01 21:05:22 -04:00
|
|
|
typeDetectors = [];
|
2026-08-09 02:20:50 -04:00
|
|
|
detectorKeys.clear();
|
2026-08-01 14:37:08 -04:00
|
|
|
});
|
|
|
|
|
}
|