-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
88 lines (78 loc) · 3.23 KB
/
Copy pathbackground.js
File metadata and controls
88 lines (78 loc) · 3.23 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
79
80
81
82
83
84
85
86
87
88
import { sendImageToGemini } from './src/services/geminiService.js';
import { loadApiKey, saveApiKey } from './src/services/settingsService.js';
// ─── Dev hot-reload ──────────────────────────────────────────────────────────
// Connects to the local dev-server.js WebSocket. In production the connection
// attempt silently fails (the server isn't running) so this is safe to ship.
try {
const _devWs = new WebSocket('ws://localhost:8181');
_devWs.onmessage = () => {
// Reload the active tab so updated content scripts take effect, then
// reload the extension itself so the new background/sidepanel are loaded.
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]?.id) chrome.tabs.reload(tabs[0].id);
});
setTimeout(() => chrome.runtime.reload(), 200);
};
} catch (_) {}
// ─────────────────────────────────────────────────────────────────────────────
console.log('Background script running.');
let sidePanelOpen = false;
let apiKey = '';
// Initialize: Load API key from storage when the extension starts
loadApiKey().then((key) => {
apiKey = key;
console.log(
key ? 'API key loaded from storage' : 'No API key found in storage'
);
});
// Open sidepanel when the extension icon is clicked
chrome.action.onClicked.addListener((tab) => {
chrome.sidePanel.getOptions({ tabId: tab.id }, (options) => {
if (options?.enabled && sidePanelOpen) {
sidePanelOpen = false;
chrome.runtime.sendMessage({ action: 'closeSidePanel' });
} else {
sidePanelOpen = true;
chrome.sidePanel.open({ tabId: tab.id });
}
});
});
// Listen for sidepanel disconnect event to update the state
chrome.runtime.onConnect.addListener((port) => {
if (port.name === 'sidePanel') {
port.onDisconnect.addListener(() => {
sidePanelOpen = false;
});
}
});
// Listen for messages from the sidepanel
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'sendToGemini') {
// Get the image data and custom instruction from the request
const imageData = request.imageData;
const customInstruction =
request.instruction ||
"What's in this image? Please describe it in detail.";
// Call the Gemini API using the imported service
sendImageToGemini(imageData, customInstruction, apiKey)
.then((result) => {
sendResponse({ success: true, result });
})
.catch((error) => {
console.error('Error sending to Gemini:', error);
sendResponse({ success: false, error: error.message });
});
// Return true to indicate we'll send an async response
return true;
}
// Handle API key updates from the settings
if (request.action === 'updateApiKey') {
// Save the new API key using our settings service
saveApiKey(request.apiKey).then(() => {
apiKey = request.apiKey;
console.log('API key updated and saved to storage');
sendResponse({ success: true });
});
return true;
}
});