import config from "@/utils/config.json"; /** * Detects any username mentions in a forum post and turns them into clickable links. */ export function forumMentions() { const textBlocks = document.querySelectorAll("p:not(.text-muted):not(.mb-0)"); const regex = /@([\w.]+)/g; textBlocks.forEach((text) => { const walker = document.createTreeWalker(text, NodeFilter.SHOW_TEXT, null); let node; while ((node = walker.nextNode())) { const fragment = document.createDocumentFragment(); let lastIndex = 0; let match; while ((match = regex.exec(node.nodeValue!)) !== null) { const username = match[1]; const link = document.createElement("a"); link.href = `/u/${username}`; link.className = "polyplus-mention"; link.textContent = match[0]; fragment.appendChild( document.createTextNode( node.nodeValue!.substring(lastIndex, match.index), ), ); fragment.appendChild(link); lastIndex = match.index + match[0].length; } fragment.appendChild( document.createTextNode(node.nodeValue!.substring(lastIndex)), ); node.parentNode!.replaceChild(fragment, node); } }); } export function generativeAI() { for (const userID of config.users.generativeAI) { const cards = document.querySelectorAll(".card"); cards.forEach((card) => { const user = card.querySelector(".forum-user-container"); if (user) { const link = user.querySelector(`[href^="/users/${userID}"]`); if (link) { const textBlock = card.querySelector(".user-forum-content"); if (textBlock) { const tag = document.createElement("span"); tag.classList.add("badge", "bg-secondary"); tag.innerText = "This content may have been generated using AI. This information may not be factual."; textBlock.appendChild(tag); } } } }); } }