From f31fcdc7ac078fcc8864a744ef4560fd8369035a Mon Sep 17 00:00:00 2001 From: vyneer Date: Fri, 15 Nov 2024 16:28:23 +0300 Subject: [PATCH] feat: add ignored phrases to settings --- assets/chat/js/chat.js | 63 ++++++++++++++++++++---- assets/chat/js/const.js | 1 + assets/chat/js/menus/ChatSettingsMenu.js | 16 ++++++ assets/chat/js/regex.js | 5 ++ assets/views/embed.html | 10 ++++ 5 files changed, 85 insertions(+), 10 deletions(-) diff --git a/assets/chat/js/chat.js b/assets/chat/js/chat.js index cf74b956..5a462a07 100644 --- a/assets/chat/js/chat.js +++ b/assets/chat/js/chat.js @@ -53,6 +53,7 @@ import makeSafeForRegex, { nsfwregex, nsflregex, linkregex, + isRegexPhrase, } from './regex'; import { HashLinkConverter, MISSING_ARG_ERROR } from './hashlinkconverter'; @@ -692,6 +693,27 @@ class Chat { ? new RegExp(`\\b(?:${ignores.join('|')})\\b`, 'i') : null; + const ignoredPhrases = [...(this.settings.get('ignoredphrases') || [])] + .filter((phrase) => { + if (isRegexPhrase(phrase)) { + try { + // eslint-disable-next-line no-new + new RegExp(phrase, 'i'); + return true; + } catch { + return false; + } + } + return phrase !== ''; + }) + .map((phrase) => + isRegexPhrase(phrase) ? phrase.slice(1, -1) : makeSafeForRegex(phrase), + ); + this.ignoredPhrasesRegex = + ignoredPhrases.length > 0 + ? new RegExp(`${ignoredPhrases.join('|')}`, 'i') + : null; + // Highlight Regex const cust = [...(this.settings.get('customhighlight') || [])].filter( (a) => a !== '', @@ -954,17 +976,38 @@ class Chat { } ignored(username, text = null) { - const ignore = this.ignoring.has(username); - if (!ignore && text !== null) { - return ( - (this.settings.get('ignorementions') && - this.ignoreregex && - this.ignoreregex.test(text)) || - (this.settings.get('hidensfl') && nsflregex.test(text)) || - (this.settings.get('hidensfw') && nsfwregex.test(text)) - ); + if (this.ignoring.has(username)) { + return true; + } + + if (!text) { + return false; + } + + if ( + this.ignoredPhrasesRegex && + this.ignoredPhrasesRegex.test(`${username}: ${text}`) + ) { + return true; + } + + if ( + this.settings.get('ignorementions') && + this.ignoreregex && + this.ignoreregex.test(text) + ) { + return true; } - return ignore; + + if (this.settings.get('hidensfl') && nsflregex.test(text)) { + return true; + } + + if (this.settings.get('hidensfw') && nsfwregex.test(text)) { + return true; + } + + return false; } ignore(nick, ignore = true) { diff --git a/assets/chat/js/const.js b/assets/chat/js/const.js index 138a8004..dded2713 100644 --- a/assets/chat/js/const.js +++ b/assets/chat/js/const.js @@ -140,6 +140,7 @@ const settingsdefault = new Map( fontscale: 'auto', censorbadwords: false, disableanimations: false, + ignoredphrases: [], }), ); diff --git a/assets/chat/js/menus/ChatSettingsMenu.js b/assets/chat/js/menus/ChatSettingsMenu.js index d9052f92..be65d744 100644 --- a/assets/chat/js/menus/ChatSettingsMenu.js +++ b/assets/chat/js/menus/ChatSettingsMenu.js @@ -16,6 +16,22 @@ export default class ChatSettingsMenu extends ChatMenu { this.ui.on('keypress blur', 'textarea[name="customhighlight"]', (e) => this.onCustomHighlightChange(e), ); + this.ui.on('keypress blur', 'textarea[name="ignoredphrases"]', (e) => + this.onIgnoredPhrasesChange(e), + ); + } + + onIgnoredPhrasesChange(e) { + if (e.type === 'focusout' || isKeyCode(e, KEYCODES.ENTER)) { + const data = $(e.target) + .val() + .toString() + .split('\n') + .map((s) => s.trim()); + this.chat.settings.set('ignoredphrases', [...new Set(data)]); + this.chat.applySettings(false); + this.chat.commitSettings(); + } } onCustomHighlightChange(e) { diff --git a/assets/chat/js/regex.js b/assets/chat/js/regex.js index e1dbfc87..6ca082b6 100644 --- a/assets/chat/js/regex.js +++ b/assets/chat/js/regex.js @@ -44,6 +44,10 @@ const nickregex = /^\w{3,20}$/; const nsfwregex = /\bNSFW\b/i; const nsflregex = /\bNSFL\b/i; +function isRegexPhrase(str) { + return str.startsWith('/') && str.endsWith('/') && str.length > 2; +} + export { regexslashcmd, regextime, @@ -52,6 +56,7 @@ export { nsfwregex, nsflregex, linkregex, + isRegexPhrase, }; export default function makeSafeForRegex(str) { diff --git a/assets/views/embed.html b/assets/views/embed.html index a1e854db..da2c48e2 100644 --- a/assets/views/embed.html +++ b/assets/views/embed.html @@ -226,6 +226,16 @@

Highlights, Focus & Tags

placeholder="Comma separated ..." > +
+ + +

Autocomplete