A rewrite of Poly+, my quality-of-life browser extension for Polytoria. Built entirely fresh using the WXT extension framework, Typescript, and with added better overall code quality.
extension
1import { preferences } from "@/utils/storage";
2
3export function forumMentions() {
4 const textBlocks = document.querySelectorAll('p:not(.text-muted):not(.mb-0)');
5 const regex = /@([\w.]+)/g;
6
7 textBlocks.forEach(text => {
8 const walker = document.createTreeWalker(text, NodeFilter.SHOW_TEXT, null);
9 let node;
10
11 while ((node = walker.nextNode())) {
12 const fragment = document.createDocumentFragment();
13 let lastIndex = 0;
14 let match;
15
16 while ((match = regex.exec(node.nodeValue!)) !== null) {
17 const username = match[1];
18 const link = document.createElement('a');
19 link.href = `/u/${username}`;
20 link.className = 'polyplus-mention';
21 link.textContent = match[0];
22
23 fragment.appendChild(document.createTextNode(node.nodeValue!.substring(lastIndex, match.index)));
24 fragment.appendChild(link);
25 lastIndex = match.index + match[0].length;
26 }
27
28 fragment.appendChild(document.createTextNode(node.nodeValue!.substring(lastIndex)));
29 node.parentNode!.replaceChild(fragment, node);
30 }
31 });
32}