Skip to content

Commit

Permalink
feat: add ignored phrases to settings
Browse files Browse the repository at this point in the history
  • Loading branch information
vyneer committed Nov 15, 2024
1 parent f9ee2fe commit f31fcdc
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 10 deletions.
63 changes: 53 additions & 10 deletions assets/chat/js/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import makeSafeForRegex, {
nsfwregex,
nsflregex,
linkregex,
isRegexPhrase,
} from './regex';

import { HashLinkConverter, MISSING_ARG_ERROR } from './hashlinkconverter';
Expand Down Expand Up @@ -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 !== '',
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions assets/chat/js/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ const settingsdefault = new Map(
fontscale: 'auto',
censorbadwords: false,
disableanimations: false,
ignoredphrases: [],
}),
);

Expand Down
16 changes: 16 additions & 0 deletions assets/chat/js/menus/ChatSettingsMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions assets/chat/js/regex.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions assets/views/embed.html
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,16 @@ <h4>Highlights, Focus &amp; Tags</h4>
placeholder="Comma separated ..."
></textarea>
</div>
<div class="form-group row">
<label title="Ignores whole phrases">Ignored Phrases</label>
<textarea
name="ignoredphrases"
style="resize: vertical"
class="form-control"
placeholder="OBAMNA LULW &#10; /Destiny: \bVeryPog\b/ &#10; Newline separated ..."
rows="3"
></textarea>
</div>

<h4>Autocomplete</h4>
<div class="form-group checkbox">
Expand Down

0 comments on commit f31fcdc

Please sign in to comment.