Create script.js
Browse files- static/script.js +42 -0
static/script.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const sidebar = document.getElementById('sidebar');
|
| 2 |
+
const toggleBtn = document.getElementById('toggle-sidebar');
|
| 3 |
+
const userInput = document.getElementById('user-input');
|
| 4 |
+
const sendBtn = document.getElementById('send-btn');
|
| 5 |
+
const chatWindow = document.getElementById('chat-window');
|
| 6 |
+
|
| 7 |
+
// إخفاء وإظهار الشريط الجانبي
|
| 8 |
+
toggleBtn.onclick = () => sidebar.classList.toggle('hidden');
|
| 9 |
+
|
| 10 |
+
// تحميل المحادثات من المتصفح
|
| 11 |
+
let currentChat = JSON.parse(localStorage.getItem('genisi_chats')) || [];
|
| 12 |
+
|
| 13 |
+
async function sendMessage() {
|
| 14 |
+
const text = userInput.value.trim();
|
| 15 |
+
if (!text) return;
|
| 16 |
+
|
| 17 |
+
appendMessage('user', text);
|
| 18 |
+
userInput.value = '';
|
| 19 |
+
|
| 20 |
+
const response = await fetch('/chat', {
|
| 21 |
+
method: 'POST',
|
| 22 |
+
headers: {'Content-Type': 'application/json'},
|
| 23 |
+
body: JSON.stringify({ message: text, history: currentChat })
|
| 24 |
+
});
|
| 25 |
+
|
| 26 |
+
const data = await response.json();
|
| 27 |
+
appendMessage('bot', data.response);
|
| 28 |
+
|
| 29 |
+
// حفظ في ذاكرة المتصفح
|
| 30 |
+
currentChat.push({user: text, bot: data.response});
|
| 31 |
+
localStorage.setItem('genisi_chats', JSON.stringify(currentChat));
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
function appendMessage(role, text) {
|
| 35 |
+
const div = document.createElement('div');
|
| 36 |
+
div.className = role === 'user' ? 'msg-user' : 'msg-bot';
|
| 37 |
+
div.innerText = text;
|
| 38 |
+
chatWindow.appendChild(div);
|
| 39 |
+
chatWindow.scrollTop = chatWindow.scrollHeight;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
sendBtn.onclick = sendMessage;
|