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

feat: Forum Mentions

Index 8b7db0da 912667ef

Changed files
+48 -1
entrypoints
forum.content
places.content
+15
entrypoints/forum.content/index.ts
···
+
import { preferences } from "@/utils/storage";
+
import * as view from "./view";
+
+
export default defineContentScript({
+
matches: ['*://polytoria.com/forum/*'],
+
main() {
+
preferences.getPreferences()
+
.then((values) => {
+
if (window.location.pathname.includes('post')) {
+
// View
+
if (values.forumMentions.enabled) view.forumMentions();
+
}
+
});
+
}
+
});
+32
entrypoints/forum.content/view.ts
···
+
import { preferences } from "@/utils/storage";
+
+
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);
+
}
+
});
+
}
+1 -1
entrypoints/places.content/index.ts
···
export default defineContentScript({
matches: ['*://polytoria.com/places/*'],
main() {
-
preferences.getValue()
+
preferences.getPreferences()
.then((values) => {
if (window.location.pathname.split('/')[2]) {
// View