Spaces:
Sleeping
Sleeping
Commit
·
4f737fe
1
Parent(s):
ac0a9f3
Upload 3 files
Browse files- chatbox/index.html +20 -0
- chatbox/script.js +123 -0
- chatbox/style.css +98 -0
chatbox/index.html
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<title>Chat System</title>
|
| 6 |
+
<link rel="stylesheet" href="style.css">
|
| 7 |
+
</head>
|
| 8 |
+
<body>
|
| 9 |
+
<div class="chat-container">
|
| 10 |
+
<div class="chat-messages" id="messages"></div>
|
| 11 |
+
<div class="chat-input">
|
| 12 |
+
<input type="text" id="nameInputBox" placeholder="Your Name">
|
| 13 |
+
<input type="text" id="messageInputBox" placeholder="Type a message...">
|
| 14 |
+
<button onclick="postMessage()">Send</button>
|
| 15 |
+
</div>
|
| 16 |
+
</div>
|
| 17 |
+
|
| 18 |
+
<script src="script.js"></script>
|
| 19 |
+
</body>
|
| 20 |
+
</html>
|
chatbox/script.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
// sets the delay time for each request
|
| 3 |
+
const DELAYTIME = 500
|
| 4 |
+
// Variable to store the backend server data
|
| 5 |
+
let DATA;
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
// Fetch data from the backend server
|
| 9 |
+
function postMessage(url="https://jacobinathanialpeterson-chatbox.hf.space/postMessage") {
|
| 10 |
+
const controller = new AbortController();
|
| 11 |
+
const abortSignal = controller.signal;
|
| 12 |
+
fetch(url, {
|
| 13 |
+
method: 'POST',
|
| 14 |
+
headers: {
|
| 15 |
+
'Content-Type': 'application/json'
|
| 16 |
+
},
|
| 17 |
+
body: JSON.stringify({nameInput: document.getElementById("nameInputBox").value, messageInput: document.getElementById("messageInputBox").value}),
|
| 18 |
+
signal: abortSignal
|
| 19 |
+
})
|
| 20 |
+
.then(response => { if (!response.ok) { throw new Error('Network response was not ok.') } return response.json() })
|
| 21 |
+
.then(data => {
|
| 22 |
+
controller.abort();
|
| 23 |
+
})
|
| 24 |
+
.catch(error => { if (error.name === 'AbortError') { console.log('Fetch aborted:', error.message) } else { console.error('Error fetching data:', error.message) }});
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
function getMessages(url="https://jacobinathanialpeterson-chatbox.hf.space/messages") {
|
| 28 |
+
const controller = new AbortController();
|
| 29 |
+
const abortSignal = controller.signal;
|
| 30 |
+
fetch(url, {signal: abortSignal})
|
| 31 |
+
.then(response => { if (!response.ok) { throw new Error('Network response was not ok.') } return response.json() })
|
| 32 |
+
.then(data => {
|
| 33 |
+
// Get the keys of the object
|
| 34 |
+
const keys = Object.keys(data);
|
| 35 |
+
|
| 36 |
+
// Sort the keys based on their natural order
|
| 37 |
+
keys.sort((a, b) => {
|
| 38 |
+
// Extract numeric part of keys
|
| 39 |
+
const numA = parseInt(a.match(/\d+/)[0]);
|
| 40 |
+
const numB = parseInt(b.match(/\d+/)[0]);
|
| 41 |
+
return numA - numB;
|
| 42 |
+
});
|
| 43 |
+
|
| 44 |
+
// Create a new object with sorted keys
|
| 45 |
+
DATA = {};
|
| 46 |
+
keys.forEach(key => {
|
| 47 |
+
DATA[key] = data[key];
|
| 48 |
+
});
|
| 49 |
+
|
| 50 |
+
controller.abort();
|
| 51 |
+
})
|
| 52 |
+
.catch(error => { if (error.name === 'AbortError') { console.log('Fetch aborted:', error.message) } else { console.error('Error fetching data:', error.message) }});
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
// Variable to track if the user is at the bottom of the chat box
|
| 58 |
+
let atBottom = true;
|
| 59 |
+
|
| 60 |
+
// Update the UI with fetched messages
|
| 61 |
+
function updateUI() {
|
| 62 |
+
const messagesContainer = document.getElementById("messages");
|
| 63 |
+
const isAtBottom = messagesContainer.scrollHeight - messagesContainer.scrollTop === messagesContainer.clientHeight;
|
| 64 |
+
|
| 65 |
+
let elementValue = '';
|
| 66 |
+
if (DATA) {
|
| 67 |
+
Object.entries(DATA).forEach(([key, value]) => {
|
| 68 |
+
elementValue +=
|
| 69 |
+
`<div class="msg" id="${key}">
|
| 70 |
+
<div class="nme">${value.name}: </div>
|
| 71 |
+
<div class="body">${value.message}</div>
|
| 72 |
+
</div>`;
|
| 73 |
+
});
|
| 74 |
+
}
|
| 75 |
+
messagesContainer.innerHTML = elementValue;
|
| 76 |
+
|
| 77 |
+
if (atBottom || isAtBottom) {
|
| 78 |
+
messagesContainer.scrollTop = messagesContainer.scrollHeight;
|
| 79 |
+
}
|
| 80 |
+
atBottom = isAtBottom;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
// Your existing interval for getting messages and updating UI
|
| 84 |
+
setInterval(getMessages, 100);
|
| 85 |
+
setTimeout(() => {
|
| 86 |
+
setInterval(updateUI, 100);
|
| 87 |
+
}, DELAYTIME);
|
| 88 |
+
|
| 89 |
+
// Listen for scroll events
|
| 90 |
+
document.getElementById("messages").addEventListener("scroll", () => {
|
| 91 |
+
const messagesContainer = document.getElementById("messages");
|
| 92 |
+
const isAtBottom = messagesContainer.scrollHeight - messagesContainer.scrollTop === messagesContainer.clientHeight;
|
| 93 |
+
atBottom = isAtBottom;
|
| 94 |
+
});
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
// ... (your existing code)
|
| 98 |
+
|
| 99 |
+
// Function to handle sending message on "Enter" key press
|
| 100 |
+
function handleKeyPress(event) {
|
| 101 |
+
if (event.keyCode === 13) {
|
| 102 |
+
event.preventDefault(); // Prevent the default behavior (e.g., form submission)
|
| 103 |
+
postMessage(); // Call the postMessage function when "Enter" is pressed
|
| 104 |
+
}
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
// Add event listener to the message input field
|
| 108 |
+
document.getElementById("messageInputBox").addEventListener("keypress", handleKeyPress);
|
| 109 |
+
|
| 110 |
+
// ... (rest of your existing code)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
// document.getElementById("nameInputBox").value
|
| 114 |
+
// document.getElementById(messageInputBox).value
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
// function updateUI() {
|
| 119 |
+
// var targetElement = document.getElementById("messages"); // Replace "targetElementId" with the ID of the element
|
| 120 |
+
// targetElement.innerHTML = "<p>"+JSON.stringify(DATA)+"</p>";
|
| 121 |
+
// }
|
| 122 |
+
|
| 123 |
+
// setInterval(updateUI, 1000);
|
chatbox/style.css
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Dark color theme styles */
|
| 2 |
+
body {
|
| 3 |
+
font-family: Arial, sans-serif; /* Change the font family here */
|
| 4 |
+
background-color: #333; /* Dark background color */
|
| 5 |
+
color: #eee; /* Light text color */
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
.chat-container {
|
| 9 |
+
border: 1px solid #555; /* Darker border */
|
| 10 |
+
width: 600px;
|
| 11 |
+
margin: 20px auto;
|
| 12 |
+
padding: 10px;
|
| 13 |
+
background-color: #444; /* Slightly lighter background color */
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
.chat-messages {
|
| 17 |
+
height: 700px;
|
| 18 |
+
overflow-y: scroll;
|
| 19 |
+
border-bottom: 1px solid #555;
|
| 20 |
+
padding-bottom: 10px;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
.msgc {
|
| 24 |
+
margin-bottom: 10px;
|
| 25 |
+
padding: 5px;
|
| 26 |
+
border-radius: 5px;
|
| 27 |
+
background-color: #555; /* Darker message background */
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
.nme {
|
| 31 |
+
font-weight: bold;
|
| 32 |
+
margin-bottom: 5px;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
/* Your existing styles */
|
| 36 |
+
|
| 37 |
+
/* Adjusted styles for message content padding */
|
| 38 |
+
.msg {
|
| 39 |
+
margin-bottom: 10px;
|
| 40 |
+
padding: 10px; /* Add padding to the message */
|
| 41 |
+
border-radius: 5px;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
/* Rest of your existing styles */
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
/* Your existing styles */
|
| 48 |
+
|
| 49 |
+
/* Adjusted styles for input and button layout */
|
| 50 |
+
.chat-input {
|
| 51 |
+
display: flex;
|
| 52 |
+
flex-direction: row;
|
| 53 |
+
justify-content: space-between;
|
| 54 |
+
align-items: center;
|
| 55 |
+
margin-top: 10px;
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
input[type="text"] {
|
| 59 |
+
flex: 1;
|
| 60 |
+
padding: 10px;
|
| 61 |
+
border: 1px solid #ccc;
|
| 62 |
+
border-radius: 5px;
|
| 63 |
+
margin-right: 10px; /* Add spacing between inputs and button */
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
input[type="text"]:last-child {
|
| 67 |
+
margin-right: 0; /* Remove margin from the last input */
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
.chat-input button {
|
| 71 |
+
padding: 10px;
|
| 72 |
+
cursor: pointer;
|
| 73 |
+
background-color: #3498db;
|
| 74 |
+
color: #fff;
|
| 75 |
+
border: none;
|
| 76 |
+
border-radius: 5px;
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
/* Rest of your existing styles */
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
.chat-input button:hover {
|
| 83 |
+
background-color: #2980b9; /* Darker blue on hover */
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
/* Your existing styles */
|
| 87 |
+
|
| 88 |
+
/* Adjusted styles for alternating message backgrounds */
|
| 89 |
+
.msg:nth-child(even) {
|
| 90 |
+
background-color: #555; /* Darker background for even messages */
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
.msg:nth-child(odd) {
|
| 94 |
+
background-color: #444; /* Slightly lighter background for odd messages */
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
/* Rest of your existing styles */
|
| 98 |
+
|