app.js
1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function send() {
/*client side */
var chat = document.createElement("li");
var chat_input = document.getElementById("chat_input");
var chat_text = chat_input.value;
chat.className = "chat-bubble mine";
chat.innerText = chat_text
document.getElementById("chat_list").appendChild(chat);
chat_input.value = "";
/* ajax request */
var request = new XMLHttpRequest();
request.open("POST", `${window.location.host}/api/soft`, true);
request.onreadystatechange = function() {
if (request.readyState !== 4 || Math.floor(request.status /100) !==2) return;
var bot_chat = document.createElement("li");
bot_chat.className = "chat-bubble bots";
bot_chat.innerText = JSON.parse(request.responseText).data;
document.getElementById("chat_list").appendChild(bot_chat);
};
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
request.send(JSON.stringify({"data":chat_text}));
}
function setDefault() {
document.getElementById("chat_input").addEventListener("keyup", function(event) {
let input = document.getElementById("chat_input").value;
let button = document.getElementById("send_button");
if(input.length>0)
{
button.removeAttribute("disabled");
}
else
{
button.setAttribute("disabled", "true");
}
// Number 13 is the "Enter" key on the keyboard
if (event.keyCode === 13) {
// Cancel the default action, if needed
event.preventDefault();
// Trigger the button element with a click
button.click();
}
});
}