-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
45 lines (39 loc) · 1.36 KB
/
popup.js
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
document.addEventListener('DOMContentLoaded', () => {
const chatContainer = document.getElementById('chatContainer');
const userInput = document.getElementById('userInput');
const sendButton = document.getElementById('sendButton');
function addMessage(content, isUser) {
const messageDiv = document.createElement('div');
messageDiv.className = `message ${isUser ? 'user-message' : 'assistant-message'}`;
messageDiv.textContent = content;
chatContainer.appendChild(messageDiv);
chatContainer.scrollTop = chatContainer.scrollHeight;
}
async function handleSendMessage() {
const content = userInput.value.trim();
if (!content) return;
// Add user message to chat
addMessage(content, true);
userInput.value = '';
try {
// Send message to background script
const response = await chrome.runtime.sendMessage({
type: 'sendMessage',
content
});
// Add assistant response to chat
if (response && response.content) {
addMessage(response.content, false);
}
} catch (error) {
console.error('Error sending message:', error);
addMessage('Error: Could not get response', false);
}
}
sendButton.addEventListener('click', handleSendMessage);
userInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
handleSendMessage();
}
});
});