Spaces:
Sleeping
Sleeping
Commit ·
e1116fd
1
Parent(s): 4f737fe
Rename chatbox/script.js to static/script.js
Browse files- {chatbox → static}/script.js +2 -32
{chatbox → static}/script.js
RENAMED
|
@@ -1,11 +1,6 @@
|
|
| 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;
|
|
@@ -52,12 +47,8 @@ function getMessages(url="https://jacobinathanialpeterson-chatbox.hf.space/messa
|
|
| 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;
|
|
@@ -80,44 +71,23 @@ function updateUI() {
|
|
| 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();
|
| 103 |
-
postMessage();
|
| 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);
|
|
|
|
|
|
|
|
|
|
| 1 |
const DELAYTIME = 500
|
|
|
|
| 2 |
let DATA;
|
| 3 |
|
|
|
|
|
|
|
| 4 |
function postMessage(url="https://jacobinathanialpeterson-chatbox.hf.space/postMessage") {
|
| 5 |
const controller = new AbortController();
|
| 6 |
const abortSignal = controller.signal;
|
|
|
|
| 47 |
.catch(error => { if (error.name === 'AbortError') { console.log('Fetch aborted:', error.message) } else { console.error('Error fetching data:', error.message) }});
|
| 48 |
}
|
| 49 |
|
|
|
|
|
|
|
|
|
|
| 50 |
let atBottom = true;
|
| 51 |
|
|
|
|
| 52 |
function updateUI() {
|
| 53 |
const messagesContainer = document.getElementById("messages");
|
| 54 |
const isAtBottom = messagesContainer.scrollHeight - messagesContainer.scrollTop === messagesContainer.clientHeight;
|
|
|
|
| 71 |
atBottom = isAtBottom;
|
| 72 |
}
|
| 73 |
|
|
|
|
| 74 |
setInterval(getMessages, 100);
|
| 75 |
setTimeout(() => {
|
| 76 |
setInterval(updateUI, 100);
|
| 77 |
}, DELAYTIME);
|
| 78 |
|
|
|
|
| 79 |
document.getElementById("messages").addEventListener("scroll", () => {
|
| 80 |
const messagesContainer = document.getElementById("messages");
|
| 81 |
const isAtBottom = messagesContainer.scrollHeight - messagesContainer.scrollTop === messagesContainer.clientHeight;
|
| 82 |
atBottom = isAtBottom;
|
| 83 |
});
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
function handleKeyPress(event) {
|
| 86 |
if (event.keyCode === 13) {
|
| 87 |
+
event.preventDefault();
|
| 88 |
+
postMessage();
|
| 89 |
}
|
| 90 |
}
|
| 91 |
|
|
|
|
| 92 |
document.getElementById("messageInputBox").addEventListener("keypress", handleKeyPress);
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|