-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
61 lines (53 loc) · 1.82 KB
/
background.js
File metadata and controls
61 lines (53 loc) · 1.82 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
// background.js - Debug Version
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'analyze_gemini') {
handleGeminiRequest(request, sendResponse);
return true; // Keep channel open
}
if (request.action === 'analyze_groq') {
handleGroqRequest(request, sendResponse);
return true; // Keep channel open
}
});
async function handleGeminiRequest(data, sendResponse) {
try {
const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:generateContent?key=${data.apiKey}`;
const response = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
contents: [{ parts: [{ text: data.prompt }] }]
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Gemini ${response.status}: ${errorText}`);
}
const result = await response.json();
sendResponse({ success: true, data: result });
} catch (error) {
console.error("Gemini Error:", error);
sendResponse({ success: false, error: error.message });
}
}
async function handleGroqRequest(data, sendResponse) {
try {
const response = await fetch('https://api.groq.com/openai/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${data.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(data.payload)
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Groq ${response.status}: ${errorText}`);
}
const result = await response.json();
sendResponse({ success: true, data: result });
} catch (error) {
console.error("Groq Error:", error);
sendResponse({ success: false, error: error.message });
}
}