Update static/js/main.js
Browse files- static/js/main.js +67 -58
static/js/main.js
CHANGED
|
@@ -1,58 +1,67 @@
|
|
| 1 |
-
//
|
| 2 |
-
var messages = document.querySelector('.message-list')
|
| 3 |
-
var btn = document.querySelector('.btn')
|
| 4 |
-
var input = document.querySelector('input')
|
| 5 |
-
|
| 6 |
-
//
|
| 7 |
-
btn.addEventListener('click', sendMessage)
|
| 8 |
-
input.addEventListener('keyup', function(e)
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
})
|
| 25 |
-
.catch(error => console.error('Error:', error));
|
| 26 |
-
}
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Lấy các phần tử DOM
|
| 2 |
+
var messages = document.querySelector('.message-list');
|
| 3 |
+
var btn = document.querySelector('.btn');
|
| 4 |
+
var input = document.querySelector('input');
|
| 5 |
+
|
| 6 |
+
// Thêm sự kiện cho nút Send và phím Enter
|
| 7 |
+
btn.addEventListener('click', sendMessage);
|
| 8 |
+
input.addEventListener('keyup', function(e) {
|
| 9 |
+
if (e.key === 'Enter') sendMessage();
|
| 10 |
+
});
|
| 11 |
+
|
| 12 |
+
// Tải lịch sử chat khi trang load
|
| 13 |
+
function loadHistory() {
|
| 14 |
+
fetch('/history')
|
| 15 |
+
.then(response => response.json())
|
| 16 |
+
.then(data => {
|
| 17 |
+
for (let i = 0; i < data.length; i++) {
|
| 18 |
+
if (data[i].role === 'User') {
|
| 19 |
+
addMessage({ FROM: 'User', MESSAGE: data[i].content }, 'primary');
|
| 20 |
+
} else if (data[i].role === 'assistant') {
|
| 21 |
+
addMessage({ FROM: 'Oshi no Ko', MESSAGE: data[i].content }, 'secondary');
|
| 22 |
+
}
|
| 23 |
+
}
|
| 24 |
+
})
|
| 25 |
+
.catch(error => console.error('Error:', error));
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
loadHistory();
|
| 29 |
+
|
| 30 |
+
// Hàm gửi tin nhắn
|
| 31 |
+
function sendMessage() {
|
| 32 |
+
var msg = input.value.trim();
|
| 33 |
+
if (!msg) return; // Không gửi tin nhắn rỗng
|
| 34 |
+
|
| 35 |
+
// Hiển thị tin nhắn người dùng
|
| 36 |
+
addMessage({ FROM: 'User', MESSAGE: msg }, 'primary');
|
| 37 |
+
|
| 38 |
+
// Xóa input sau khi gửi
|
| 39 |
+
input.value = '';
|
| 40 |
+
|
| 41 |
+
// Gửi request đến server Flask
|
| 42 |
+
fetch('/chat', {
|
| 43 |
+
method: 'POST',
|
| 44 |
+
headers: { 'Content-Type': 'application/json' },
|
| 45 |
+
body: JSON.stringify({ message: msg })
|
| 46 |
+
})
|
| 47 |
+
.then(response => response.json())
|
| 48 |
+
.then(data => {
|
| 49 |
+
// Hiển thị phản hồi từ chatbot
|
| 50 |
+
addMessage({ FROM: 'Oshi no Ko', MESSAGE: data.MESSAGE }, 'secondary');
|
| 51 |
+
})
|
| 52 |
+
.catch(error => console.error('Error:', error));
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
// Hàm thêm tin nhắn vào giao diện
|
| 56 |
+
function addMessage(msg, typeMessage = 'primary') {
|
| 57 |
+
writeLine(`<span>${msg.FROM}</span><br><br> ${msg.MESSAGE}`, typeMessage);
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
// Hàm tạo dòng tin nhắn
|
| 61 |
+
function writeLine(text, typeMessage) {
|
| 62 |
+
var message = document.createElement('li');
|
| 63 |
+
message.classList.add('message-item', 'item-' + typeMessage);
|
| 64 |
+
message.innerHTML = text;
|
| 65 |
+
messages.appendChild(message);
|
| 66 |
+
messages.scrollTop = messages.scrollHeight; // Cuộn xuống cuối cùng
|
| 67 |
+
}
|