-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
66 lines (57 loc) · 2.13 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Replace 'YOUR_GEMINI_API_KEY_HERE' with your actual Gemini API key
const apiKey = 'AIzaSyBUFOndNmGDu3L-CJ9kcYOJLqU2wSY9AF4';
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('searchForm').addEventListener('submit', handleSubmit);
});
async function handleSubmit(e) {
e.preventDefault();
const query = document.getElementById('searchInput').value;
if (!query) return;
addMessageToChat('User', query);
document.getElementById('searchInput').value = '';
try {
const response = await sendToGemini(query);
addMessageToChat('AI', response);
} catch (error) {
addMessageToChat('System', 'Error: ' + error.message);
}
}
function addMessageToChat(sender, message) {
const chatArea = document.getElementById('chatArea');
const messageElement = document.createElement('p');
messageElement.innerHTML = `<strong>${sender}:</strong> ${message}`;
chatArea.appendChild(messageElement);
chatArea.scrollTop = chatArea.scrollHeight;
}
async function sendToGemini(query) {
const url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent';
const fullUrl = `${url}?key=${apiKey}`;
try {
const response = await fetch(fullUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
contents: [
{
parts: [
{
text: query
}
]
}
]
})
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(`Gemini API error: ${response.status} ${response.statusText} - ${JSON.stringify(errorData)}`);
}
const data = await response.json();
return data.candidates[0].content.parts[0].text;
} catch (error) {
console.error('Error details:', error);
throw new Error(`Failed to get response from Gemini: ${error.message}`);
}
}