From f74da73086b2b361490e028c999c80b3ac2ba76a Mon Sep 17 00:00:00 2001
From: Nuckyz <61953774+Nuckyz@users.noreply.github.com>
Date: Tue, 14 May 2024 23:57:43 -0300
Subject: [PATCH] feat: Allow finds to use regex (#2452)
---
.../VencordSettings/PatchHelperTab.tsx | 44 ++++++++++++++-----
src/utils/types.ts | 8 +++-
src/webpack/patchWebpack.ts | 7 ++-
3 files changed, 46 insertions(+), 13 deletions(-)
diff --git a/src/components/VencordSettings/PatchHelperTab.tsx b/src/components/VencordSettings/PatchHelperTab.tsx
index 064c872a..9e2980e7 100644
--- a/src/components/VencordSettings/PatchHelperTab.tsx
+++ b/src/components/VencordSettings/PatchHelperTab.tsx
@@ -180,7 +180,8 @@ function ReplacementInput({ replacement, setReplacement, replacementError }) {
return (
<>
- replacement
+ {/* FormTitle adds a class if className is not set, so we set it to an empty string to prevent that */}
+ replacement
{!isFunc && (
-
Cheat Sheet
+
Cheat Sheet
{Object.entries({
"\\i": "Special regex escape sequence that matches identifiers (varnames, classnames, etc.)",
"$$": "Insert a $",
@@ -220,11 +221,12 @@ function ReplacementInput({ replacement, setReplacement, replacementError }) {
interface FullPatchInputProps {
setFind(v: string): void;
+ setParsedFind(v: string | RegExp): void;
setMatch(v: string): void;
setReplacement(v: string | ReplaceFn): void;
}
-function FullPatchInput({ setFind, setMatch, setReplacement }: FullPatchInputProps) {
+function FullPatchInput({ setFind, setParsedFind, setMatch, setReplacement }: FullPatchInputProps) {
const [fullPatch, setFullPatch] = React.useState
("");
const [fullPatchError, setFullPatchError] = React.useState("");
@@ -233,6 +235,7 @@ function FullPatchInput({ setFind, setMatch, setReplacement }: FullPatchInputPro
setFullPatchError("");
setFind("");
+ setParsedFind("");
setMatch("");
setReplacement("");
return;
@@ -256,7 +259,8 @@ function FullPatchInput({ setFind, setMatch, setReplacement }: FullPatchInputPro
if (!parsed.replacement.match) throw new Error("No 'replacement.match' field");
if (!parsed.replacement.replace) throw new Error("No 'replacement.replace' field");
- setFind(parsed.find);
+ setFind(parsed.find instanceof RegExp ? parsed.find.toString() : parsed.find);
+ setParsedFind(parsed.find);
setMatch(parsed.replacement.match instanceof RegExp ? parsed.replacement.match.source : parsed.replacement.match);
setReplacement(parsed.replacement.replace);
setFullPatchError("");
@@ -266,7 +270,7 @@ function FullPatchInput({ setFind, setMatch, setReplacement }: FullPatchInputPro
}
return <>
- Paste your full JSON patch here to fill out the fields
+ Paste your full JSON patch here to fill out the fields
{fullPatchError !== "" && {fullPatchError}}
>;
@@ -274,6 +278,7 @@ function FullPatchInput({ setFind, setMatch, setReplacement }: FullPatchInputPro
function PatchHelper() {
const [find, setFind] = React.useState("");
+ const [parsedFind, setParsedFind] = React.useState("");
const [match, setMatch] = React.useState("");
const [replacement, setReplacement] = React.useState("");
@@ -285,20 +290,34 @@ function PatchHelper() {
const code = React.useMemo(() => {
return `
{
- find: ${JSON.stringify(find)},
+ find: ${parsedFind instanceof RegExp ? parsedFind.toString() : JSON.stringify(parsedFind)},
replacement: {
match: /${match.replace(/(?full patch
- find
+ find
- match
+ match
+
(p: P & Record string;
export interface PatchReplacement {
+ /** The match for the patch replacement. If you use a string it will be implicitly converted to a RegExp */
match: string | RegExp;
+ /** The replacement string or function which returns the string for the patch replacement */
replace: string | ReplaceFn;
+ /** A function which returns whether this patch replacement should be applied */
predicate?(): boolean;
}
export interface Patch {
plugin: string;
- find: string;
+ /** A string or RegExp which is only include/matched in the module code you wish to patch. Prefer only using a RegExp if a simple string test is not enough */
+ find: string | RegExp;
+ /** The replacement(s) for the module being patched */
replacement: PatchReplacement | PatchReplacement[];
/** Whether this patch should apply to multiple modules */
all?: boolean;
@@ -44,6 +49,7 @@ export interface Patch {
noWarn?: boolean;
/** Only apply this set of replacements if all of them succeed. Use this if your replacements depend on each other */
group?: boolean;
+ /** A function which returns whether this patch should be applied */
predicate?(): boolean;
}
diff --git a/src/webpack/patchWebpack.ts b/src/webpack/patchWebpack.ts
index d3de2072..c7e42467 100644
--- a/src/webpack/patchWebpack.ts
+++ b/src/webpack/patchWebpack.ts
@@ -257,7 +257,12 @@ function patchFactories(factories: Record