Fix the webclient rendering the message.
This commit is contained in:
parent
4b7fe624bb
commit
1c185b309f
1 changed files with 14 additions and 2 deletions
|
|
@ -199,8 +199,13 @@
|
|||
// Listen for messages from the server
|
||||
socket.on('message', (data) => {
|
||||
const messageElement = document.createElement('div');
|
||||
|
||||
// Create a span for the metadata (timestamp, username, chat room)
|
||||
const metadataSpan = document.createElement('span');
|
||||
const timestamp = data.timestamp ? `[${data.timestamp}]` : '';
|
||||
const chatRoomInfo = data.chat_room ? `(${data.chat_room})` : '';
|
||||
metadataSpan.textContent = `${timestamp} ${data.username || 'Server'} ${chatRoomInfo}: `;
|
||||
metadataSpan.style.fontWeight = 'bold'; // Optional: Make metadata bold
|
||||
|
||||
// Render the message as HTML using marked.js
|
||||
const renderedMessage = marked.parse(data.message);
|
||||
|
|
@ -208,8 +213,15 @@
|
|||
// Sanitize the HTML using DOMPurify
|
||||
const sanitizedMessage = DOMPurify.sanitize(renderedMessage);
|
||||
|
||||
// Set the inner HTML of the message element
|
||||
messageElement.innerHTML = `${timestamp} ${data.username || 'Server'} ${chatRoomInfo}: ${sanitizedMessage}`;
|
||||
// Create a span for the message content
|
||||
const messageContentSpan = document.createElement('span');
|
||||
messageContentSpan.innerHTML = sanitizedMessage;
|
||||
|
||||
// Append the metadata and message content to the message element
|
||||
messageElement.appendChild(metadataSpan);
|
||||
messageElement.appendChild(messageContentSpan);
|
||||
|
||||
// Append the message element to the chat div
|
||||
chatDiv.appendChild(messageElement);
|
||||
chatDiv.scrollTop = chatDiv.scrollHeight; // Auto-scroll to the bottom
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue