From e9a1b762e7a806300a50c5db97f1699d51ca077c Mon Sep 17 00:00:00 2001 From: easrng Date: Tue, 15 Feb 2022 19:42:16 -0500 Subject: [PATCH] only emojify actual emojis --- frontend/lib/chat.mjs | 4 ++-- frontend/lib/emojis.mjs | 21 +++++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/frontend/lib/chat.mjs b/frontend/lib/chat.mjs index 4a48e3b..0807bde 100644 --- a/frontend/lib/chat.mjs +++ b/frontend/lib/chat.mjs @@ -286,7 +286,7 @@ const formatTime = (ms) => { }:${seconds < 10 ? "0" + seconds : seconds}`; }; -export const logEventToChat = (event) => { +export const logEventToChat = async (event) => { if (checkDebounce(event)) { return; } @@ -313,7 +313,7 @@ export const logEventToChat = (event) => { case "ChatMessage": { const messageContent = document.createElement("span"); messageContent.classList.add("message-content"); - messageContent.append(...emojify(event.data)); + messageContent.append(...(await emojify(event.data))); printChatMessage( "chat-message", event.user, diff --git a/frontend/lib/emojis.mjs b/frontend/lib/emojis.mjs index 8a7c55b..7729889 100644 --- a/frontend/lib/emojis.mjs +++ b/frontend/lib/emojis.mjs @@ -1,16 +1,21 @@ -export function emojify(text) { +export async function emojify(text) { + const emojiList = await emojis; let last = 0; let nodes = []; text.replace(/:([^\s:]+):/g, (match, name, index) => { if (last <= index) nodes.push(document.createTextNode(text.slice(last, index))); - nodes.push( - Object.assign(new Image(), { - src: `/emojis/${name}.png`, - className: "emoji", - alt: name, - }) - ); + if (!emojiList.includes(name)) { + nodes.push(document.createTextNode(match)); + } else { + nodes.push( + Object.assign(new Image(), { + src: `/emojis/${name}.png`, + className: "emoji", + alt: name, + }) + ); + } last = index + match.length; }); if (last < text.length) nodes.push(document.createTextNode(text.slice(last)));