Update Redditlikechatter.HTML
This commit is contained in:
parent
e47330b1cc
commit
f0d49b301d
1 changed files with 16 additions and 13 deletions
|
@ -26,16 +26,13 @@
|
|||
const messageInput = document.getElementById('message');
|
||||
const sendButton = document.getElementById('send');
|
||||
|
||||
|
||||
let username = localStorage.getItem('username') || '';
|
||||
|
||||
|
||||
usernameInput.value = username;
|
||||
|
||||
const socket = new WebSocket('wss://free.blr2.piesocket.com/v3/1?api_key=C1ZujiijYNPNj8gvuGXhIPKsDR4WolCCKOBzMdbM¬ify_self=1');
|
||||
|
||||
socket.addEventListener('open', () => {
|
||||
sendButton.disabled = false;
|
||||
sendButton.disabled = false;
|
||||
});
|
||||
|
||||
socket.addEventListener('message', event => {
|
||||
|
@ -53,8 +50,7 @@
|
|||
|
||||
function sendMessage() {
|
||||
username = usernameInput.value;
|
||||
localStorage.setItem('username', username);
|
||||
|
||||
localStorage.setItem('username', username);
|
||||
const message = messageInput.value;
|
||||
|
||||
if (username.trim() === '' || message.trim() === '') return;
|
||||
|
@ -65,10 +61,7 @@
|
|||
message: message
|
||||
};
|
||||
|
||||
|
||||
socket.send(JSON.stringify(data));
|
||||
|
||||
|
||||
messageInput.value = '';
|
||||
}
|
||||
}
|
||||
|
@ -80,14 +73,24 @@
|
|||
const messageData = JSON.parse(data);
|
||||
|
||||
usernameElement.textContent = messageData.username + ': ';
|
||||
messageElement.textContent = messageData.message;
|
||||
|
||||
const messageWithLinks = convertURLsToLinks(messageData.message);
|
||||
const messageContentElement = document.createElement('div');
|
||||
messageContentElement.innerHTML = messageWithLinks;
|
||||
|
||||
messageElement.appendChild(usernameElement);
|
||||
messageElement.appendChild(messageContentElement);
|
||||
|
||||
messageElement.prepend(usernameElement);
|
||||
chatbox.appendChild(messageElement);
|
||||
|
||||
|
||||
chatbox.scrollTop = chatbox.scrollHeight;
|
||||
}
|
||||
|
||||
function convertURLsToLinks(text) {
|
||||
const urlRegex = /(https?:\/\/[^\s]+)/g;
|
||||
return text.replace(urlRegex, (url) => {
|
||||
return `<a href="${url}" target="_blank">${url}</a>`;
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Add table
Reference in a new issue