mirror of
https://github.com/Lynnesbian/FediBooks/
synced 2024-11-25 08:38:59 +00:00
only keep 50 most recent messages, disable input box while waiting for a reply
This commit is contained in:
parent
e9cdbf7de2
commit
d8dc54f802
1 changed files with 25 additions and 5 deletions
|
@ -1,10 +1,12 @@
|
||||||
|
var chatlog = [];
|
||||||
|
|
||||||
function sendMessage() {
|
function sendMessage() {
|
||||||
let id = window.location.href.split("/").slice(-1)[0]
|
let id = window.location.href.split("/").slice(-1)[0]
|
||||||
message = document.getElementById("chatbox-input-box").value
|
message = document.getElementById("chatbox-input-box").value
|
||||||
document.getElementById("chatbox-input-box").value = ''
|
document.getElementById("chatbox-input-box").value = ''
|
||||||
let chatbox = document.getElementById("chatbox");
|
document.getElementById("chatbox-input-box").disabled = true;
|
||||||
chatbox.innerHTML += `<div class="message-container user"><div class="message user">${message}</div></div>`;
|
chatlog.push(["user", message])
|
||||||
chatbox.scrollTop = chatbox.scrollHeight;
|
renderChatlog();
|
||||||
var xhttp = new XMLHttpRequest();
|
var xhttp = new XMLHttpRequest();
|
||||||
xhttp.onreadystatechange = function() {
|
xhttp.onreadystatechange = function() {
|
||||||
if (this.readyState == 4) {
|
if (this.readyState == 4) {
|
||||||
|
@ -13,8 +15,10 @@ function sendMessage() {
|
||||||
} else {
|
} else {
|
||||||
message = "Encountered an error while trying to get a response.";
|
message = "Encountered an error while trying to get a response.";
|
||||||
}
|
}
|
||||||
chatbox.innerHTML += `<div class="message-container bot"><div class="bot-icon"></div><div class="message bot">${message}</div></div>`;
|
chatlog.push(["bot", message]);
|
||||||
chatbox.scrollTop = chatbox.scrollHeight;
|
renderChatlog();
|
||||||
|
document.getElementById("chatbox-input-box").disabled = false;
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
xhttp.open("GET", `/bot/chat/${id}/message`, true);
|
xhttp.open("GET", `/bot/chat/${id}/message`, true);
|
||||||
|
@ -22,3 +26,19 @@ function sendMessage() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function renderChatlog() {
|
||||||
|
let chatbox = document.getElementById("chatbox");
|
||||||
|
let out = "";
|
||||||
|
if (chatlog.length > 50) {
|
||||||
|
chatlog.shift(); //only keep the 50 most recent messages to avoid slowdown
|
||||||
|
}
|
||||||
|
chatlog.forEach(function(item, i) {
|
||||||
|
if (item[0] == "user") {
|
||||||
|
out += `<div class="message-container user"><div class="message user">${item[1]}</div></div>`;
|
||||||
|
} else {
|
||||||
|
out += `<div class="message-container bot"><div class="bot-icon"></div><div class="message bot">${item[1]}</div></div>`;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
chatbox.innerHTML = out;
|
||||||
|
chatbox.scrollTop = chatbox.scrollHeight;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue