-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.ts
More file actions
206 lines (183 loc) · 7.75 KB
/
options.ts
File metadata and controls
206 lines (183 loc) · 7.75 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Import settings manager using path aliases
import {
Settings,
getSettings,
updateSettings,
resetUsageCounter
} from '@utils/settingsManager';
// Test the Exa API key
async function testExaKey(key: string): Promise<boolean> {
try {
const response = await fetch('https://api.exa.ai/search', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${key}`
},
body: JSON.stringify({
query: 'test query',
numResults: 1
})
});
return response.ok;
} catch (error) {
return false;
}
}
// Test the OpenAI API key
async function testOpenAIKey(key: string): Promise<boolean> {
try {
const response = await fetch('https://api.openai.com/v1/models', {
headers: {
'Authorization': `Bearer ${key}`
}
});
return response.ok;
} catch (error) {
return false;
}
}
// Save settings to storage
async function saveSettings(): Promise<void> {
const maxVerifications = (document.getElementById('maxVerifications') as HTMLInputElement).value;
const cacheDuration = (document.getElementById('cacheDuration') as HTMLInputElement).value;
// Get current settings to preserve usage stats
const currentSettings = await getSettings();
const settings: Settings = {
exaKey: (document.getElementById('exaKey') as HTMLInputElement).value,
openaiKey: (document.getElementById('openaiKey') as HTMLInputElement).value,
highlightsEnabled: (document.getElementById('highlightsEnabled') as HTMLInputElement).checked,
sidebarEnabled: (document.getElementById('sidebarEnabled') as HTMLInputElement).checked,
darkMode: (document.getElementById('darkMode') as HTMLInputElement).checked,
excludedDomains: (document.getElementById('excludedDomains') as HTMLTextAreaElement)
.value
.split('\n')
.map(d => d.trim())
.filter(d => d),
maxVerificationsPerDay: parseInt(maxVerifications || '10', 10),
enableCaching: (document.getElementById('enableCaching') as HTMLInputElement).checked,
cacheDuration: parseInt(cacheDuration || '7', 10),
useLLMExtraction: (document.getElementById('useLLMExtraction') as HTMLInputElement).checked,
// Preserve usage stats
usageCount: currentSettings.usageCount,
lastUsageReset: currentSettings.lastUsageReset
};
// Update settings using the settings manager
await updateSettings(settings);
// Notify content scripts of settings change
const tabs = await chrome.tabs.query({});
for (const tab of tabs) {
if (tab.id) {
chrome.tabs.sendMessage(tab.id, { type: 'SETTINGS_UPDATED', settings });
}
}
}
// Load settings from storage
async function loadSettings(): Promise<void> {
// Get settings from the settings manager
const settings = await getSettings();
(document.getElementById('exaKey') as HTMLInputElement).value = settings.exaKey;
(document.getElementById('openaiKey') as HTMLInputElement).value = settings.openaiKey;
(document.getElementById('highlightsEnabled') as HTMLInputElement).checked = settings.highlightsEnabled;
(document.getElementById('sidebarEnabled') as HTMLInputElement).checked = settings.sidebarEnabled;
(document.getElementById('darkMode') as HTMLInputElement).checked = settings.darkMode;
(document.getElementById('excludedDomains') as HTMLTextAreaElement).value =
settings.excludedDomains.join('\n');
(document.getElementById('maxVerifications') as HTMLInputElement).value =
settings.maxVerificationsPerDay.toString();
(document.getElementById('enableCaching') as HTMLInputElement).checked =
settings.enableCaching;
(document.getElementById('cacheDuration') as HTMLInputElement).value =
settings.cacheDuration.toString();
(document.getElementById('useLLMExtraction') as HTMLInputElement).checked =
settings.useLLMExtraction;
// Update current usage display
const currentUsageElement = document.getElementById('currentUsage');
if (currentUsageElement) {
currentUsageElement.textContent = settings.usageCount.toString();
}
// Conditionally disable LLM extraction toggle if OpenAI key is missing
const openaiKeyField = document.getElementById('openaiKey') as HTMLInputElement;
const useLLMExtractionField = document.getElementById('useLLMExtraction') as HTMLInputElement;
if (!openaiKeyField.value) {
useLLMExtractionField.checked = false;
useLLMExtractionField.disabled = true;
useLLMExtractionField.parentElement?.parentElement?.setAttribute('title', 'OpenAI API key required');
} else {
useLLMExtractionField.disabled = false;
useLLMExtractionField.parentElement?.parentElement?.removeAttribute('title');
}
}
// Update status message
function updateStatus(elementId: string, success: boolean, message: string): void {
const element = document.getElementById(elementId);
if (element) {
element.textContent = message;
element.className = `status ${success ? 'success' : 'error'}`;
}
}
// Initialize
document.addEventListener('DOMContentLoaded', () => {
loadSettings();
// Test Exa API key
document.getElementById('testExaKey')?.addEventListener('click', async () => {
const key = (document.getElementById('exaKey') as HTMLInputElement).value;
const success = await testExaKey(key);
updateStatus('exaKeyStatus', success,
success ? '✓ API key is valid' : '✗ Invalid API key');
});
// Test OpenAI API key
document.getElementById('testOpenAIKey')?.addEventListener('click', async () => {
const key = (document.getElementById('openaiKey') as HTMLInputElement).value;
const success = await testOpenAIKey(key);
updateStatus('openaiKeyStatus', success,
success ? '✓ API key is valid' : '✗ Invalid API key');
// Update LLM extraction toggle based on key validity
const useLLMExtractionField = document.getElementById('useLLMExtraction') as HTMLInputElement;
if (success) {
useLLMExtractionField.disabled = false;
useLLMExtractionField.parentElement?.parentElement?.removeAttribute('title');
} else {
useLLMExtractionField.checked = false;
useLLMExtractionField.disabled = true;
useLLMExtractionField.parentElement?.parentElement?.setAttribute('title', 'OpenAI API key required');
}
});
// Listen for changes to the OpenAI key field
document.getElementById('openaiKey')?.addEventListener('input', () => {
const key = (document.getElementById('openaiKey') as HTMLInputElement).value;
const useLLMExtractionField = document.getElementById('useLLMExtraction') as HTMLInputElement;
if (key.trim() === '') {
useLLMExtractionField.checked = false;
useLLMExtractionField.disabled = true;
useLLMExtractionField.parentElement?.parentElement?.setAttribute('title', 'OpenAI API key required');
} else {
useLLMExtractionField.disabled = false;
useLLMExtractionField.parentElement?.parentElement?.removeAttribute('title');
}
});
// Reset usage counter
document.getElementById('resetUsage')?.addEventListener('click', async () => {
// Use the settings manager to reset usage counter
await resetUsageCounter();
// Update UI
const currentUsageElement = document.getElementById('currentUsage');
if (currentUsageElement) {
currentUsageElement.textContent = '0';
}
updateStatus('saveStatus', true, 'Usage counter reset!');
setTimeout(() => {
const element = document.getElementById('saveStatus');
if (element) element.textContent = '';
}, 2000);
});
// Save settings
document.getElementById('saveSettings')?.addEventListener('click', async () => {
await saveSettings();
updateStatus('saveStatus', true, 'Settings saved!');
setTimeout(() => {
const element = document.getElementById('saveStatus');
if (element) element.textContent = '';
}, 2000);
});
});