Fix: Canonicalize regex finds

This commit is contained in:
Nuckyz 2024-05-15 00:38:07 -03:00
parent f74da73086
commit 54f58cd7c9
No known key found for this signature in database
GPG key ID: 440BF8296E1C4AD9
3 changed files with 16 additions and 3 deletions

View file

@ -303,8 +303,10 @@ async function runtime(token: string) {
delete patch.predicate; delete patch.predicate;
delete patch.group; delete patch.group;
if (!Array.isArray(patch.replacement)) Vencord.Util.canonicalizeFind(patch);
if (!Array.isArray(patch.replacement)) {
patch.replacement = [patch.replacement]; patch.replacement = [patch.replacement];
}
patch.replacement.forEach(r => { patch.replacement.forEach(r => {
delete r.predicate; delete r.predicate;

View file

@ -20,6 +20,7 @@ import { registerCommand, unregisterCommand } from "@api/Commands";
import { addContextMenuPatch, removeContextMenuPatch } from "@api/ContextMenu"; import { addContextMenuPatch, removeContextMenuPatch } from "@api/ContextMenu";
import { Settings } from "@api/Settings"; import { Settings } from "@api/Settings";
import { Logger } from "@utils/Logger"; import { Logger } from "@utils/Logger";
import { canonicalizeFind } from "@utils/patches";
import { Patch, Plugin, StartAt } from "@utils/types"; import { Patch, Plugin, StartAt } from "@utils/types";
import { FluxDispatcher } from "@webpack/common"; import { FluxDispatcher } from "@webpack/common";
import { FluxEvents } from "@webpack/types"; import { FluxEvents } from "@webpack/types";
@ -83,8 +84,12 @@ for (const p of pluginsValues) {
if (p.patches && isPluginEnabled(p.name)) { if (p.patches && isPluginEnabled(p.name)) {
for (const patch of p.patches) { for (const patch of p.patches) {
patch.plugin = p.name; patch.plugin = p.name;
if (!Array.isArray(patch.replacement))
canonicalizeFind(patch);
if (!Array.isArray(patch.replacement)) {
patch.replacement = [patch.replacement]; patch.replacement = [patch.replacement];
}
patches.push(patch); patches.push(patch);
} }
} }

View file

@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { PatchReplacement, ReplaceFn } from "./types"; import { Patch, PatchReplacement, ReplaceFn } from "./types";
export function canonicalizeMatch<T extends RegExp | string>(match: T): T { export function canonicalizeMatch<T extends RegExp | string>(match: T): T {
if (typeof match === "string") return match; if (typeof match === "string") return match;
@ -55,3 +55,9 @@ export function canonicalizeReplacement(replacement: Pick<PatchReplacement, "mat
); );
Object.defineProperties(replacement, descriptors); Object.defineProperties(replacement, descriptors);
} }
export function canonicalizeFind(patch: Patch) {
const descriptors = Object.getOwnPropertyDescriptors(patch);
descriptors.find = canonicalizeDescriptor(descriptors.find, canonicalizeMatch);
Object.defineProperties(patch, descriptors);
}