-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
108 lines (94 loc) · 3.28 KB
/
popup.js
File metadata and controls
108 lines (94 loc) · 3.28 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
// Popup script for Grammar Corrector Extension
import { API_TYPES } from './api-manager.js';
document.addEventListener('DOMContentLoaded', () => {
const apiTypeSelect = document.getElementById('apiType');
const openaiSection = document.getElementById('openaiSection');
const geminiSection = document.getElementById('geminiSection');
const deepseekSection = document.getElementById('deepseekSection');
const openaiKeyInput = document.getElementById('openaiKey');
const geminiKeyInput = document.getElementById('geminiKey');
const deepseekKeyInput = document.getElementById('deepseekKey');
const saveButton = document.getElementById('saveButton');
const statusElement = document.getElementById('status');
// Handle API type selection change
apiTypeSelect.addEventListener('change', () => {
const selectedApiType = apiTypeSelect.value;
// Hide all sections first
openaiSection.classList.add('hidden');
geminiSection.classList.add('hidden');
deepseekSection.classList.add('hidden');
// Show only the selected section
switch (selectedApiType) {
case API_TYPES.OPENAI:
openaiSection.classList.remove('hidden');
break;
case API_TYPES.GEMINI:
geminiSection.classList.remove('hidden');
break;
case API_TYPES.DEEPSEEK:
deepseekSection.classList.remove('hidden');
break;
}
});
// Load saved API settings on popup open
chrome.storage.sync.get(['apiType', 'openaiKey', 'geminiKey', 'deepseekKey'], (result) => {
if (result.apiType) {
apiTypeSelect.value = result.apiType;
// Trigger change event to show correct sections
apiTypeSelect.dispatchEvent(new Event('change'));
}
if (result.openaiKey) {
openaiKeyInput.value = result.openaiKey;
}
if (result.geminiKey) {
geminiKeyInput.value = result.geminiKey;
}
if (result.deepseekKey) {
deepseekKeyInput.value = result.deepseekKey;
}
});
// Save API settings when button is clicked
saveButton.addEventListener('click', () => {
const apiType = apiTypeSelect.value;
const openaiKey = openaiKeyInput.value.trim();
const geminiKey = geminiKeyInput.value.trim();
const deepseekKey = deepseekKeyInput.value.trim();
// Get the current API key based on selected type
let currentKey = '';
let apiName = '';
switch(apiType) {
case API_TYPES.OPENAI:
currentKey = openaiKey;
apiName = 'OpenAI';
break;
case API_TYPES.GEMINI:
currentKey = geminiKey;
apiName = 'Gemini';
break;
case API_TYPES.DEEPSEEK:
currentKey = deepseekKey;
apiName = 'Deepseek';
break;
}
// Validate key
if (!currentKey) {
alert('Please enter a valid API key');
return;
}
// Save all API settings to storage
chrome.storage.sync.set({
apiType: apiType,
openaiKey: openaiKey,
geminiKey: geminiKey,
deepseekKey: deepseekKey
}, () => {
// Show success message
statusElement.style.display = 'block';
statusElement.textContent = 'API key saved successfully!';
// Hide message after 3 seconds
setTimeout(() => {
statusElement.style.display = 'none';
}, 3000);
});
});
});