-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
168 lines (149 loc) · 5.78 KB
/
options.js
File metadata and controls
168 lines (149 loc) · 5.78 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
// YouTube Transcript Summarizer - Options Page Script
// This handles saving and loading the API key and theme settings
// Wait for DOM to load
document.addEventListener('DOMContentLoaded', () => {
// Get references to elements
const apiKeyInput = document.getElementById('apiKey');
const toggleApiKeyBtn = document.getElementById('toggleApiKey');
const saveButton = document.getElementById('saveButton');
const statusDiv = document.getElementById('status');
const themeOptions = document.querySelectorAll('input[name="theme"]');
const modelOptions = document.querySelectorAll('input[name="model"]');
// Load saved settings when page opens
loadSettings();
// Add event listener for save button
saveButton.addEventListener('click', saveSettings);
// Add event listeners for option cards
setupOptionCards();
// Add event listener for API key toggle
if (toggleApiKeyBtn) {
toggleApiKeyBtn.addEventListener('click', toggleApiKeyVisibility);
}
// Function to toggle API key visibility
function toggleApiKeyVisibility() {
if (apiKeyInput.type === 'password') {
apiKeyInput.type = 'text';
toggleApiKeyBtn.classList.add('showing');
} else {
apiKeyInput.type = 'password';
toggleApiKeyBtn.classList.remove('showing');
}
// Focus on the input after toggling
apiKeyInput.focus();
}
// Function to setup option cards
function setupOptionCards() {
// Add event listeners for theme options
themeOptions.forEach(radio => {
radio.addEventListener('change', () => {
// Remove selected class from all theme cards
document.querySelectorAll('.theme-options .option-card').forEach(card => {
card.classList.remove('selected');
});
// Add selected class to the checked card
if (radio.checked) {
radio.closest('.option-card').classList.add('selected');
}
});
});
// Add event listeners for model options
modelOptions.forEach(radio => {
radio.addEventListener('change', () => {
// Remove selected class from all model cards
document.querySelectorAll('.model-options .option-card').forEach(card => {
card.classList.remove('selected');
});
// Add selected class to the checked card
if (radio.checked) {
radio.closest('.option-card').classList.add('selected');
}
});
});
}
// Function to load settings from storage
function loadSettings() {
chrome.storage.sync.get(['veniceApiKey', 'theme', 'model'], (data) => {
// If we have a saved API key, show it in the input field
if (data.veniceApiKey) {
apiKeyInput.value = data.veniceApiKey;
// Show a placeholder instead of the actual key
if (apiKeyInput.type === 'password') {
// Keep the actual key accessible for saving
apiKeyInput.setAttribute('data-original-key', data.veniceApiKey);
}
}
// Set the theme radio button
if (data.theme) {
const themeRadio = document.querySelector(`input[name="theme"][value="${data.theme}"]`);
if (themeRadio) {
themeRadio.checked = true;
// Add selected class to the parent card
themeRadio.closest('.option-card').classList.add('selected');
}
} else {
// If no theme is set, mark Auto as selected
const autoTheme = document.querySelector('input[name="theme"][value="auto"]');
if (autoTheme) {
autoTheme.checked = true;
autoTheme.closest('.option-card').classList.add('selected');
}
}
// Set the model radio button
if (data.model) {
const modelRadio = document.querySelector(`input[name="model"][value="${data.model}"]`);
if (modelRadio) {
modelRadio.checked = true;
// Add selected class to the parent card
modelRadio.closest('.option-card').classList.add('selected');
}
} else {
// If no model is set, mark mistral-31-24b as selected
const defaultModel = document.querySelector('input[name="model"][value="mistral-31-24b"]');
if (defaultModel) {
defaultModel.checked = true;
defaultModel.closest('.option-card').classList.add('selected');
}
}
});
}
// Function to save settings to storage
function saveSettings() {
let apiKey = apiKeyInput.value.trim();
// If the input is empty but we have an original key stored, use that
if (!apiKey && apiKeyInput.getAttribute('data-original-key')) {
apiKey = apiKeyInput.getAttribute('data-original-key');
}
// Validate API key (basic check - not empty)
if (!apiKey) {
showStatus('Please enter a valid API key.', 'error');
return;
}
// Get selected theme and model
const selectedTheme = document.querySelector('input[name="theme"]:checked').value;
const selectedModel = document.querySelector('input[name="model"]:checked').value;
// Save to Chrome's sync storage
chrome.storage.sync.set({
veniceApiKey: apiKey,
theme: selectedTheme,
model: selectedModel
}, () => {
if (chrome.runtime.lastError) {
showStatus('Error saving settings: ' + chrome.runtime.lastError.message, 'error');
} else {
// Store the original key in data attribute
apiKeyInput.setAttribute('data-original-key', apiKey);
showStatus('Settings saved successfully!', 'success');
}
});
}
// Function to show status messages
function showStatus(message, type) {
statusDiv.textContent = message;
statusDiv.className = 'status ' + type;
statusDiv.style.display = 'block';
// Hide status message after 3 seconds
setTimeout(() => {
statusDiv.style.display = 'none';
}, 3000);
}
});