1
0
Fork 1
mirror of https://github.com/Vendicated/Vencord.git synced 2025-01-26 09:16:24 +00:00

Ignore case toggle

This commit is contained in:
camila314 2024-06-24 02:46:52 -05:00
parent bac0ac1b6b
commit a572da18f5

View file

@ -7,7 +7,7 @@
import "./style.css"; import "./style.css";
import definePlugin, { OptionType } from "@utils/types"; import definePlugin, { OptionType } from "@utils/types";
import { Button, ChannelStore, Forms, Select,SelectedChannelStore, TabBar, TextInput, UserStore, UserUtils, useState } from "@webpack/common"; import { Button, ChannelStore, Forms, Select, Switch, SelectedChannelStore, TabBar, TextInput, UserStore, UserUtils, useState } from "@webpack/common";
import { classNameFactory } from "@api/Styles"; import { classNameFactory } from "@api/Styles";
import { DataStore } from "@api/index"; import { DataStore } from "@api/index";
import { definePluginSettings } from "@api/Settings"; import { definePluginSettings } from "@api/Settings";
@ -19,7 +19,9 @@ import { Margins } from "@utils/margins";
import { Message, User } from "discord-types/general/index.js"; import { Message, User } from "discord-types/general/index.js";
import { useForceUpdater } from "@utils/react"; import { useForceUpdater } from "@utils/react";
let keywordEntries: Array<{ regex: string, listIds: Array<string>, listType: ListType }> = []; type KeywordEntry = { regex: string, listIds: Array<string>, listType: ListType, ignoreCase: boolean };
let keywordEntries: Array<KeywordEntry> = [];
let currentUser: User; let currentUser: User;
let keywordLog: Array<any> = []; let keywordLog: Array<any> = [];
@ -33,7 +35,7 @@ const KEYWORD_LOG_KEY = "KeywordNotify_log";
const cl = classNameFactory("vc-keywordnotify-"); const cl = classNameFactory("vc-keywordnotify-");
async function addKeywordEntry(forceUpdate: () => void) { async function addKeywordEntry(forceUpdate: () => void) {
keywordEntries.push({ regex: "", listIds: [], listType: ListType.BlackList }); keywordEntries.push({ regex: "", listIds: [], listType: ListType.BlackList, ignoreCase: false });
await DataStore.set(KEYWORD_ENTRIES_KEY, keywordEntries); await DataStore.set(KEYWORD_ENTRIES_KEY, keywordEntries);
forceUpdate(); forceUpdate();
} }
@ -44,9 +46,9 @@ async function removeKeywordEntry(idx: number, forceUpdate: () => void) {
forceUpdate(); forceUpdate();
} }
function safeMatchesRegex(str: string, regex: string) { function safeMatchesRegex(str: string, regex: string, flags: string) {
try { try {
return str.match(new RegExp(regex)); return str.match(new RegExp(regex, flags));
} catch { } catch {
return false; return false;
} }
@ -57,29 +59,26 @@ enum ListType {
Whitelist = "Whitelist" Whitelist = "Whitelist"
} }
function highlightKeywords(str: string, regexes: Array<string>) { function highlightKeywords(str: string, entries: Array<KeywordEntry>) {
let regex: RegExp; let regexes: Array<RegExp>;
try { try {
regex = new RegExp(regexes.join("|"), "g"); regexes = entries.map(e => new RegExp(e.regex, "g" + (e.ignoreCase ? "i" : "")));
} catch { } catch (err) {
return [str]; return [str];
} }
const matches = str.match(regex); const matches = regexes.map(r => str.match(r)).flat().filter(e => e != null);
if (!matches) if (matches.length == 0) {
return [str]; return [str];
}
const parts = [...matches.map(e => { const idx = str.indexOf(matches[0]);
const idx = str.indexOf(e);
const before = str.substring(0, idx);
str = str.substring(idx + e.length);
return before;
}, str), str];
return parts.map(e => [ return [
(<span>{e}</span>), <span>{str.substring(0, idx)}</span>,
matches!.length ? (<span className="highlight">{matches!.splice(0, 1)[0]}</span>) : [] <span className="highlight">{matches[0]}</span>,
]); <span>{str.substring(idx + matches[0].length)}</span>
];
} }
function Collapsible({ title, children }) { function Collapsible({ title, children }) {
@ -206,6 +205,16 @@ function KeywordEntries() {
<DeleteIcon/> <DeleteIcon/>
</Button> </Button>
</Flex> </Flex>
<Switch
value={values[i].ignoreCase}
onChange={() => {
values[i].ignoreCase = !values[i].ignoreCase;
update();
}}
style={{ marginTop: "0.5em", marginRight: "40px" }}
>
Ignore Case
</Switch>
<Forms.FormDivider className={[Margins.top8, Margins.bottom8].join(" ") }/> <Forms.FormDivider className={[Margins.top8, Margins.bottom8].join(" ") }/>
<Forms.FormTitle tag="h5">Whitelist/Blacklist</Forms.FormTitle> <Forms.FormTitle tag="h5">Whitelist/Blacklist</Forms.FormTitle>
<Flex flexDirection="row"> <Flex flexDirection="row">
@ -325,20 +334,22 @@ export default definePlugin({
if (whitelistMode && !listed) { if (whitelistMode && !listed) {
return; return;
} }
if (settings.store.ignoreBots && m.author.bot && (!whitelistMode || !entry.listIds.includes(m.author.id))) { if (settings.store.ignoreBots && m.author.bot && (!whitelistMode || !entry.listIds.includes(m.author.id))) {
return; return;
} }
if (safeMatchesRegex(m.content, entry.regex)) { const flags = entry.ignoreCase ? "i" : "";
if (safeMatchesRegex(m.content, entry.regex, flags)) {
matches = true; matches = true;
} }
for (const embed of m.embeds as any) { for (const embed of m.embeds as any) {
if (safeMatchesRegex(embed.description, entry.regex) || safeMatchesRegex(embed.title, entry.regex)) { if (safeMatchesRegex(embed.description, entry.regex, flags) || safeMatchesRegex(embed.title, entry.regex, flags)) {
matches = true; matches = true;
} else if (embed.fields != null) { } else if (embed.fields != null) {
for (const field of embed.fields as Array<{ name: string, value: string }>) { for (const field of embed.fields as Array<{ name: string, value: string }>) {
if (safeMatchesRegex(field.value, entry.regex) || safeMatchesRegex(field.name, entry.regex)) { if (safeMatchesRegex(field.value, entry.regex, flags) || safeMatchesRegex(field.name, entry.regex, flags)) {
matches = true; matches = true;
} }
} }
@ -402,7 +413,7 @@ export default definePlugin({
e._keyword = true; e._keyword = true;
e.customRenderedContent = { e.customRenderedContent = {
content: highlightKeywords(e.content, keywordEntries.map(e => e.regex)) content: highlightKeywords(e.content, keywordEntries)
}; };
const msg = this.renderMsg({ const msg = this.renderMsg({