From 4013251a8e1fb026698435cefe2f4618c80f7d23 Mon Sep 17 00:00:00 2001 From: Index Date: Mon, 19 Aug 2024 18:23:41 -0500 Subject: [PATCH] fix: forum mentions feature breaking emojis --- js/forum/forum-view.js | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/js/forum/forum-view.js b/js/forum/forum-view.js index bada3c1..ceb3687 100644 --- a/js/forum/forum-view.js +++ b/js/forum/forum-view.js @@ -17,17 +17,25 @@ chrome.storage.sync.get(['PolyPlus_Settings'], function (result) { }); function ForumMentions() { - const Regex = /@([\w.]+)/g; + const Regex = /@([\w.]+)/g; - for (let text of ForumText) { - let FormattedText = text.innerHTML; - let match; - while ((match = Regex.exec(text.innerText)) !== null) { - const Username = match[0].substring(1); - FormattedText = FormattedText.replaceAll(match[0], `${match[0]}`); - } - text.innerHTML = FormattedText; - } + for (let text of ForumText) { + const TreeWalker = document.createTreeWalker(text, NodeFilter.SHOW_TEXT, null, false); + let Node; + + while ((Node = TreeWalker.nextNode())) { + let Match; + let Replacement = Node.nodeValue; + + while ((Match = Regex.exec(Node.nodeValue)) !== null) { + const Username = Match[0].substring(1); + const Mention = `${Match[0]}`; + Replacement = replacedText.replace(Match[0], Mention); + } + + Node.nodeValue = replacedText; + } + } } function ForumUnixTimestamps() { @@ -44,7 +52,6 @@ function ForumUnixTimestamps() { const Distance = new Intl.RelativeTimeFormat({numeric: 'auto', style: 'short'}).format(Math.floor((Timestamp - new Date()) / (60 * 1000)), 'day'); const Result = `${Months[Timestamp.getMonth()]} ${Timestamp.getDate()}, ${Timestamp.getFullYear()} (${['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'][Timestamp.getDay() - 1]}) at ${Timestamp.getHours() - 12}:${String(Timestamp.getMinutes()).padStart(2, '0')} (${Distance})`; FormattedText = FormattedText.replaceAll(match[0], Result); - console.log(FormattedText); } text.innerHTML = FormattedText; }