// Chat+ ยท Keywords in chats. Two word lists in the settings: a message with a highlight word gets // an accent, a message with a mute word folds into a thin "hidden by Chat+" line that opens on a // click. Both are attributes on X's message row: the message text itself is never edited, and // nothing but the two word lists is stored. A message whose text cannot be read stays as it is. import type { FeatureContext, FeatureModule } from '../../../../../waybetter/packages/runtime/src/featureModules.ts'; import type { SettingsSchema } from '../../../../../waybetter/packages/runtime/src/settingsForm.ts'; import type { PluginApi } from '../../inject/pluginApi.ts'; import { clearAttribute } from './ui.ts'; import { CONVERSATION_PROBES, CHAT_PAGE_PROBE, chatRoot, chatSelectors, conversationOf, renderedRows, rowText } from './x.ts'; export const KEYWORDS_SCHEMA: SettingsSchema = [ { key: 'highlight', type: 'list', default: [], label: 'highlight', hint: 'highlightHint', maxItems: 100, maxLength: 80 }, { key: 'mute', type: 'list', default: [], label: 'mute', hint: 'muteHint', maxItems: 100, maxLength: 80 }, ]; export type Matcher = (text: string) => boolean; /** * A test for "one of these words is in the text": whole words or phrases, any letter case, in any * script. An empty list matches nothing. */ export function wordMatcher(words: readonly string[]): Matcher { const parts = [...new Set(words.map(word => word.trim()).filter(Boolean))].map(word => word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replace(/\s+/g, '\\s+')); if (!parts.length) return () => false; const pattern = new RegExp(`(? pattern.test(text); } export type Verdict = 'muted' | 'hit' | null; /** What a message's text asks for: muted wins over highlighted; no text, no verdict. */ export const verdictOf = (text: string, highlight: Matcher, mute: Matcher): Verdict => (!text ? null : mute(text) ? 'muted' : highlight(text) ? 'hit' : null); const MARK = 'data-bt-chat-kw'; const LABEL = 'data-bt-chat-kw-label'; const ACCENT = 'var(--wb-accent, rgb(29, 155, 240))'; const css = (row: string) => ` ${row}[${MARK}="hit"] { box-shadow: inset 3px 0 0 ${ACCENT}; background: color-mix(in srgb, ${ACCENT} 12%, transparent); border-radius: 6px; } ${row}[${MARK}="muted"] { display: block; cursor: pointer; } ${row}[${MARK}="muted"] > * { display: none !important; } ${row}[${MARK}="muted"]::before { content: attr(${LABEL}); display: block; padding: 2px 12px; text-align: center; font: 500 12px/1.5 var(--wb-font, system-ui, sans-serif); color: var(--wb-muted, inherit); opacity: .7; } ${row}[${MARK}="muted"]:hover::before { opacity: 1; text-decoration: underline; } `; type Ctx = FeatureContext; export const keywords: FeatureModule = { id: 'chat.keywords', probes: { ...CONVERSATION_PROBES, ...CHAT_PAGE_PROBE }, settings: KEYWORDS_SCHEMA, start(ctx: Ctx) { const { win } = ctx; const doc = win.document; const chatScope = () => chatRoot(doc); const sel = chatSelectors(ctx, CONVERSATION_PROBES); let highlight: Matcher = () => false; let mute: Matcher = () => false; let conversation: string | null = null; /** Muted messages the user opened (by message id), until the conversation changes. */ const opened = new Set(); const compile = () => { const values = ctx.settings.get(); highlight = wordMatcher(values.highlight as string[]); mute = wordMatcher(values.mute as string[]); }; const scan = () => { const open = conversationOf(win.location.pathname); if (open !== conversation) { conversation = open; opened.clear(); } for (const row of renderedRows(chatScope(), sel)) { let verdict = verdictOf(rowText(row, sel), highlight, mute); if (verdict === 'muted' && opened.has(row.getAttribute('data-dm-message-row') ?? '')) verdict = null; if ((row.getAttribute(MARK) ?? null) === verdict) continue; if (verdict) row.setAttribute(MARK, verdict); else row.removeAttribute(MARK); if (verdict === 'muted') row.setAttribute(LABEL, ctx.text('hidden')); else row.removeAttribute(LABEL); } }; compile(); ctx.css(css(sel.row)); const watch = ctx.watch(scan); ctx.settings.onChange(() => { compile(); watch.schedule(); }); ctx.onRouteChange(() => watch.schedule()); // A click on a folded message opens it (and is not a click on the message). ctx.on(doc, 'click', event => { const row = ((event.composedPath?.()[0] ?? event.target) as Element | null)?.closest?.(`${sel.row}[${MARK}="muted"]`); if (!row) return; event.preventDefault(); event.stopPropagation(); opened.add(row.getAttribute('data-dm-message-row') ?? ''); watch.schedule(); }, true); ctx.defer(() => { for (const place of new Set([doc, chatScope()])) { clearAttribute(place, MARK); clearAttribute(place, LABEL); } }); watch.schedule(); }, };