opencompletion.com/templates/chat.html

109 lines
3.5 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chatroom</title>
<!-- Include highlight.js library -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/highlight.min.js"></script>
<!-- Include a highlight.js theme (replace 'default' with your preferred theme) -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/styles/default.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f7f7f7;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 75vh;
margin: 0;
}
#chat-container {
background-color: #ffffff;
border-radius: 5px;
padding: 15px;
width: 100%;
max-width: 600px;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}
#chat {
height: 300px;
overflow-y: scroll;
border: 1px solid #e1e1e1;
border-radius: 5px;
padding: 10px;
margin-bottom: 10px;
}
#message {
width: 98%;
border: 1px solid #e1e1e1;
border-radius: 5px;
padding: 5px;
}
</style>
</head>
<body>
<div id="chat-container">
<h2>{{ room }}</h2>
<div id="chat"></div>
<form id="message-form">
<input id="message" type="text" placeholder="Type your message...">
</form>
</div>
<script>
const socket = io.connect('http://' + document.domain + ':' + location.port);
const urlParams = new URLSearchParams(window.location.search);
const username = urlParams.get('username');
const room = '{{ room }}';
socket.on('connect', () => {
socket.emit('join', {username: username, room: room});
});
$('#message-form').submit((e) => {
e.preventDefault();
const message = $('#message').val();
socket.emit('message', {'username': username, 'message': message, room: room});
$('#message').val('');
});
socket.on('message', (message) => {
var newMessage = document.createElement('p');
newMessage.innerHTML = message;
$('#chat').append(newMessage);
// Apply syntax highlighting to the new message
newMessage.querySelectorAll('pre code').forEach((block) => {
hljs.highlightElement(block);
});
// Scroll to the bottom of the chat container
$('#chat').scrollTop($('#chat')[0].scrollHeight);
});
socket.on('previous_messages', (data) => {
$('#chat').append('<p>' + data.username + ': ' + data.message + '</p>');
$('pre code').each((i, block) => {
hljs.highlightBlock(block);
});
});
socket.on('delete_processing_message', (data) => {
const tempMessage = document.getElementById("processing");
if (tempMessage) {
tempMessage.remove();
}
});
</script>
</body>
</html>