-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
309 lines (282 loc) · 10.8 KB
/
Copy pathpopup.js
File metadata and controls
309 lines (282 loc) · 10.8 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Popup script: manage provider settings and model selection
const providerSelect = document.getElementById('providerSelect');
const apiKeyInput = document.getElementById('apiKey');
const apiKeyLabel = document.getElementById('apiKeyLabel');
const modelSelect = document.getElementById('modelSelect');
const saveBtn = document.getElementById('saveBtn');
const statusDiv = document.getElementById('status');
const fetchModelsBtn = document.getElementById('fetchModelsBtn');
const fetchStatus = document.getElementById('fetchStatus');
const downloadModelBtn = document.getElementById('downloadModelBtn');
const downloadStatus = document.getElementById('downloadStatus');
const costInfo = document.getElementById('costInfo');
const helpText = document.getElementById('helpText');
const dangerNote = document.getElementById('dangerNote');
const DEFAULT_SETTINGS = {
provider: 'groq',
apiKeyGroq: '',
apiKeyOpenAI: '',
modelGroq: 'whisper-large-v3',
modelOpenAI: 'gpt-4o-mini-transcribe',
modelLocal: 'Xenova/whisper-small',
localMode: 'browser'
};
const PROVIDER_META = {
groq: {
label: 'Groq API Key',
keyPlaceholder: 'gsk_...',
helpHtml:
'How to use:<br />' +
'1. Get a Groq API key at <a href="https://console.groq.com/keys" target="_blank">Groq Console</a>.<br />' +
'2. Paste it above and Save.<br />' +
'3. Go to or refresh WhatsApp Web. Audio messages will show a Transcribe button.<br />' +
'Your key is stored only locally (Chrome sync storage).',
costByModel: {
'whisper-large-v3-turbo': '$0.00067 / min (Groq)',
'whisper-large-v3': '$0.00185 / min (Groq)',
'distil-whisper-large-v3-en': '$0.00033 / min (Groq)'
},
minBillingNote: 'Groq bills a minimum of ~10 seconds per request.'
},
openai: {
label: 'OpenAI API Key',
keyPlaceholder: 'sk-...',
helpHtml:
'How to use:<br />' +
'1. Create an API key at <a href="https://platform.openai.com/api-keys" target="_blank">OpenAI Platform</a>.<br />' +
'2. Paste it above and Save.<br />' +
'3. Go to or refresh WhatsApp Web. Audio messages will show a Transcribe button.<br />' +
'Your key is stored only locally (Chrome sync storage).',
costByModel: {
'gpt-4o-mini-transcribe': '$0.003 / min (OpenAI)',
'gpt-4o-transcribe': '$0.006 / min (OpenAI)',
'whisper-1': '$0.006 / min (OpenAI)'
}
},
local: {
label: 'Local (browser) setup',
keyPlaceholder: 'No API key required',
helpHtml:
'Local mode downloads the model in your browser (no server). ' +
'First run can take a while depending on model size. ' +
'If a large model fails, try Xenova/whisper-small.',
costByModel: {
'Xenova/whisper-tiny': '$0 / min (local)',
'Xenova/whisper-base': '$0 / min (local)',
'Xenova/whisper-small': '$0 / min (local)',
'Xenova/whisper-medium': '$0 / min (local)',
'Xenova/whisper-large-v2': '$0 / min (local)',
'Xenova/whisper-large-v3': '$0 / min (local)'
}
}
};
function loadSettings() {
chrome.storage.sync.get(DEFAULT_SETTINGS, items => {
providerSelect.value = items.provider;
updateProviderUI(items.provider, items);
if (items.provider === 'groq' && (items.apiKeyGroq || '').startsWith('gsk_')) {
fetchModels();
}
});
}
function getProviderKey(provider, items) {
return provider === 'groq' ? items.apiKeyGroq : items.apiKeyOpenAI;
}
function setProviderKey(provider, value) {
if (provider === 'groq') return { apiKeyGroq: value };
if (provider === 'openai') return { apiKeyOpenAI: value };
return {};
}
function getProviderModel(provider, items) {
if (provider === 'groq') return items.modelGroq;
if (provider === 'openai') return items.modelOpenAI;
return items.modelLocal || 'Xenova/whisper-small';
}
function setProviderModel(provider, value) {
if (provider === 'groq') return { modelGroq: value };
if (provider === 'openai') return { modelOpenAI: value };
if (provider === 'local') return { modelLocal: value };
return {};
}
function updateCostInfo(provider) {
const meta = PROVIDER_META[provider];
const model = modelSelect.value;
const cost = (meta.costByModel && meta.costByModel[model]) || 'Cost varies by model';
const extra = meta.minBillingNote ? `<br /><span style="opacity:.8">${meta.minBillingNote}</span>` : '';
costInfo.innerHTML = `<strong>Approx cost:</strong> ${cost}${extra}`;
}
function setModelOptions(provider) {
modelSelect.innerHTML = '';
if (provider === 'local') {
const localModels = [
'Xenova/whisper-tiny',
'Xenova/whisper-base',
'Xenova/whisper-small',
'Xenova/whisper-medium',
'Xenova/whisper-large-v2',
'Xenova/whisper-large-v3'
];
localModels.forEach(m => {
const opt = document.createElement('option');
opt.value = m;
opt.textContent = m;
modelSelect.appendChild(opt);
});
modelSelect.disabled = false;
fetchModelsBtn.disabled = true;
fetchStatus.textContent = '';
updateCostInfo(provider);
return;
}
modelSelect.disabled = false;
const defaultModels =
provider === 'groq'
? ['whisper-large-v3-turbo', 'whisper-large-v3', 'distil-whisper-large-v3-en']
: ['gpt-4o-mini-transcribe', 'gpt-4o-transcribe', 'whisper-1'];
defaultModels.forEach(m => {
const opt = document.createElement('option');
opt.value = m;
opt.textContent = m;
modelSelect.appendChild(opt);
});
updateCostInfo(provider);
}
function updateProviderUI(provider, items) {
const meta = PROVIDER_META[provider];
apiKeyLabel.textContent = meta.label;
apiKeyInput.placeholder = meta.keyPlaceholder;
apiKeyInput.value = provider === 'local' ? '' : (getProviderKey(provider, items) || '');
apiKeyInput.disabled = provider === 'local';
fetchModelsBtn.style.display = provider === 'groq' ? 'inline' : 'none';
fetchModelsBtn.disabled = provider !== 'groq' || !apiKeyInput.value.trim().startsWith('gsk_');
fetchStatus.textContent = '';
helpText.innerHTML = meta.helpHtml;
dangerNote.style.display = provider === 'local' ? 'none' : 'block';
const showLocal = provider === 'local';
downloadModelBtn.style.display = showLocal ? 'inline' : 'none';
downloadStatus.style.display = showLocal ? 'block' : 'none';
downloadStatus.textContent = '';
setModelOptions(provider);
const model = getProviderModel(provider, items);
if (model && [...modelSelect.options].some(o => o.value === model)) {
modelSelect.value = model;
}
updateCostInfo(provider);
}
saveBtn.addEventListener('click', () => {
const provider = providerSelect.value;
const apiKey = apiKeyInput.value.trim();
const model = modelSelect.value;
saveBtn.disabled = true;
statusDiv.textContent = 'Saving…';
statusDiv.style.color = 'inherit';
const payload = {
provider,
...setProviderKey(provider, apiKey),
...setProviderModel(provider, model)
};
chrome.storage.sync.set(payload, () => {
statusDiv.textContent = 'Saved';
statusDiv.style.color = 'green';
saveBtn.disabled = false;
setTimeout(() => (statusDiv.textContent = ''), 2500);
});
});
providerSelect.addEventListener('change', () => {
chrome.storage.sync.get(DEFAULT_SETTINGS, items => {
items.provider = providerSelect.value;
updateProviderUI(providerSelect.value, items);
});
});
modelSelect.addEventListener('change', () => updateCostInfo(providerSelect.value));
fetchModelsBtn.addEventListener('click', () => fetchModels());
downloadModelBtn.addEventListener('click', () => preloadLocalModel());
apiKeyInput.addEventListener('input', () => {
const apiKey = apiKeyInput.value.trim();
if (providerSelect.value === 'groq') {
fetchModelsBtn.disabled = !apiKey.startsWith('gsk_');
if (!apiKey.startsWith('gsk_')) fetchStatus.textContent = '';
}
});
apiKeyInput.addEventListener('change', () => {
if (providerSelect.value === 'groq') fetchModels();
});
chrome.runtime.onMessage.addListener((message) => {
if (message && message.type === 'LOCAL_PROGRESS' && providerSelect.value === 'local') {
if (message.model && message.model !== modelSelect.value) return;
const payload = message.payload || {};
const status = payload.status || 'downloading';
if (typeof payload.progress === 'number') {
const pct = payload.progress <= 1 ? Math.round(payload.progress * 100) : Math.round(payload.progress);
downloadStatus.textContent = `${status} ${Math.min(pct, 100)}%`;
} else if (typeof payload.loaded === 'number' && typeof payload.total === 'number' && payload.total > 0) {
const pct = Math.round((payload.loaded / payload.total) * 100);
downloadStatus.textContent = `${status} ${Math.min(pct, 100)}%`;
} else if (payload.file) {
downloadStatus.textContent = `${status}: ${payload.file}`;
} else {
downloadStatus.textContent = status;
}
}
});
async function preloadLocalModel() {
if (providerSelect.value !== 'local') return;
const model = modelSelect.value;
downloadModelBtn.disabled = true;
downloadStatus.textContent = 'Starting download…';
try {
const response = await chrome.runtime.sendMessage({
type: 'LOCAL_PRELOAD',
model
});
if (response && response.success) {
downloadStatus.textContent = 'Model ready';
} else {
downloadStatus.textContent = 'Error: ' + ((response && response.error) || 'Download failed');
}
} catch (error) {
downloadStatus.textContent = 'Error: ' + (error.message || 'Download failed');
} finally {
downloadModelBtn.disabled = false;
}
}
async function fetchModels() {
const provider = providerSelect.value;
if (provider !== 'groq') return;
const apiKey = apiKeyInput.value.trim();
if (!apiKey || !apiKey.startsWith('gsk_')) return;
fetchModelsBtn.disabled = true;
fetchStatus.textContent = 'Loading models…';
fetchStatus.style.color = 'inherit';
try {
const response = await fetch('https://api.groq.com/openai/v1/models', {
headers: { 'Authorization': `Bearer ${apiKey}` }
});
if (!response.ok) throw new Error('Request failed');
const data = await response.json();
const audioModels = (data.data || []).filter(m => /whisper|distil-whisper/i.test(m.id));
modelSelect.innerHTML = '';
audioModels.forEach(m => {
const opt = document.createElement('option');
opt.value = m.id;
opt.textContent = m.id;
modelSelect.appendChild(opt);
});
const preferred = ['whisper-large-v3-turbo', 'whisper-large-v3'];
for (const p of preferred) {
if ([...modelSelect.options].some(o => o.value === p)) {
modelSelect.value = p;
break;
}
}
fetchStatus.textContent = 'Models updated';
fetchStatus.style.color = 'green';
updateCostInfo(provider);
} catch (e) {
fetchStatus.textContent = 'Error: ' + e.message;
fetchStatus.style.color = 'red';
} finally {
fetchModelsBtn.disabled = false;
}
}
loadSettings();