File size: 1,442 Bytes
be513db | 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 | const sidebar = document.getElementById('sidebar');
const toggleBtn = document.getElementById('toggle-sidebar');
const userInput = document.getElementById('user-input');
const sendBtn = document.getElementById('send-btn');
const chatWindow = document.getElementById('chat-window');
// إخفاء وإظهار الشريط الجانبي
toggleBtn.onclick = () => sidebar.classList.toggle('hidden');
// تحميل المحادثات من المتصفح
let currentChat = JSON.parse(localStorage.getItem('genisi_chats')) || [];
async function sendMessage() {
const text = userInput.value.trim();
if (!text) return;
appendMessage('user', text);
userInput.value = '';
const response = await fetch('/chat', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({ message: text, history: currentChat })
});
const data = await response.json();
appendMessage('bot', data.response);
// حفظ في ذاكرة المتصفح
currentChat.push({user: text, bot: data.response});
localStorage.setItem('genisi_chats', JSON.stringify(currentChat));
}
function appendMessage(role, text) {
const div = document.createElement('div');
div.className = role === 'user' ? 'msg-user' : 'msg-bot';
div.innerText = text;
chatWindow.appendChild(div);
chatWindow.scrollTop = chatWindow.scrollHeight;
}
sendBtn.onclick = sendMessage; |