-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
173 lines (150 loc) · 4.93 KB
/
options.js
File metadata and controls
173 lines (150 loc) · 4.93 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// WebPets Options Page
document.addEventListener('DOMContentLoaded', function() {
// Load saved settings
loadSavedSettings();
// Event listeners for save buttons
document.getElementById('save-api-key').addEventListener('click', saveApiKey);
document.getElementById('save-pet-name').addEventListener('click', savePetName);
});
// Load any previously saved settings
function loadSavedSettings() {
// Load Gemini API key
chrome.storage.local.get(['geminiApiKey'], function(result) {
if (result.geminiApiKey) {
// Mask the API key for security
const apiKey = result.geminiApiKey;
const maskedKey = maskApiKey(apiKey);
document.getElementById('gemini-api-key').value = maskedKey;
document.getElementById('gemini-api-key').dataset.masked = 'true';
}
});
// Load pet name
chrome.storage.local.get(['petState'], function(result) {
if (result.petState && result.petState.name) {
document.getElementById('pet-name').value = result.petState.name;
}
});
// Clear the input when clicked if it's masked
document.getElementById('gemini-api-key').addEventListener('focus', function() {
if (this.dataset.masked === 'true') {
this.value = '';
this.dataset.masked = 'false';
}
});
}
// Save Gemini API key
function saveApiKey() {
const apiKey = document.getElementById('gemini-api-key').value.trim();
const statusMsg = document.getElementById('status-message');
if (!apiKey) {
showStatus(statusMsg, 'Please enter an API key.', 'error');
return;
}
// Test the API key before saving
testApiKey(apiKey)
.then(() => {
// Save the API key
chrome.storage.local.set({ geminiApiKey: apiKey }, function() {
showStatus(statusMsg, 'API key saved successfully!', 'success');
// Update the config in memory
updateConfigInMemory(apiKey);
// Mask the API key for security
const maskedKey = maskApiKey(apiKey);
document.getElementById('gemini-api-key').value = maskedKey;
document.getElementById('gemini-api-key').dataset.masked = 'true';
// Notify all open extension pages of the change
chrome.runtime.sendMessage({ action: 'settingsUpdated', key: 'apiKey' });
});
})
.catch(error => {
showStatus(statusMsg, `Error: ${error.message}`, 'error');
});
}
// Test the API key before saving
function testApiKey(apiKey) {
return new Promise((resolve, reject) => {
// Use direct fetch API instead of Google GenAI library
const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${apiKey}`;
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
contents: [
{
parts: [
{
text: "Hello"
}
]
}
],
generationConfig: {
temperature: 0.7,
maxOutputTokens: 30
}
})
})
.then(response => {
if (!response.ok) {
throw new Error(`API test failed with status ${response.status}`);
}
return response.json();
})
.then(data => {
console.log("API test successful");
resolve();
})
.catch(error => {
console.error("API test error:", error);
reject(error);
});
});
}
// Save pet name
function savePetName() {
const petName = document.getElementById('pet-name').value.trim();
const statusMsg = document.getElementById('status-message');
if (!petName) {
showStatus(statusMsg, 'Please enter a pet name.', 'error');
return;
}
// Get current pet state
chrome.storage.local.get(['petState'], function(result) {
const petState = result.petState || {};
petState.name = petName;
// Save updated state
chrome.storage.local.set({ petState: petState }, function() {
showStatus(statusMsg, `Pet name saved! Your pet is now named ${petName}!`, 'success');
// Notify all open extension pages of the change
chrome.runtime.sendMessage({ action: 'settingsUpdated', key: 'petName', name: petName });
});
});
}
// Utility: Show status message
function showStatus(element, message, type) {
element.textContent = message;
element.className = `status ${type}`;
// Clear after 3 seconds
setTimeout(() => {
element.textContent = '';
element.className = '';
}, 3000);
}
// Utility: Mask API key for display
function maskApiKey(apiKey) {
if (apiKey.length <= 8) {
return '••••••••';
}
const firstFour = apiKey.substring(0, 4);
const lastFour = apiKey.substring(apiKey.length - 4);
const masked = '•'.repeat(apiKey.length - 8);
return `${firstFour}${masked}${lastFour}`;
}
// Update config in memory to use in current session
function updateConfigInMemory(apiKey) {
if (typeof CONFIG !== 'undefined') {
CONFIG.GEMINI_API_KEY = apiKey;
}
}