-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
78 lines (64 loc) · 2.78 KB
/
Copy pathscript.js
File metadata and controls
78 lines (64 loc) · 2.78 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
function openNav() {
document.getElementById("sidebar").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
}
function closeNav() {
document.getElementById("sidebar").style.width = "0";
document.getElementById("main").style.marginLeft = "0";
}
function createNewChat() {
const chatContainer = document.getElementById('chat-container');
chatContainer.innerHTML = ''; // Clear all previous messages
}
async function sendMessage() {
const userInput = document.getElementById('question-input').value;
if (userInput) {
// Display the user's message
const chatContainer = document.getElementById('chat-container');
const messageDiv = document.createElement('div');
messageDiv.className = 'message';
const profilePic = document.createElement('img');
profilePic.src = 'https://cdn-icons-png.flaticon.com/512/5787/5787016.png';
profilePic.alt = 'Profile Picture';
profilePic.className = 'profile-pic';
const text = document.createElement('span');
text.textContent = userInput;
text.className = 'message-text';
messageDiv.appendChild(profilePic);
messageDiv.appendChild(text);
chatContainer.appendChild(messageDiv);
// Send the user's message to the server
try {
const response = await fetch('/ask', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ question: userInput })
});
const data = await response.json();
// Display the response from the server
const responseDiv = document.createElement('div');
responseDiv.className = 'message';
const responseProfilePic = document.createElement('img');
responseProfilePic.src = 'https://cdn-icons-png.flaticon.com/512/5787/5787016.png';
responseProfilePic.alt = 'Profile Picture';
responseProfilePic.className = 'profile-pic';
const responseText = document.createElement('span');
responseText.textContent = data.answer;
responseText.className = 'message-text';
responseDiv.appendChild(responseProfilePic);
responseDiv.appendChild(responseText);
chatContainer.appendChild(responseDiv);
} catch (error) {
console.error('Error:', error);
}
// Clear the input
document.getElementById('question-input').value = '';
}
}
document.getElementById('question-input').addEventListener('keydown', function(event) {
if (event.key === 'Enter') {
sendMessage();
}
});