-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
75 lines (66 loc) · 3.23 KB
/
popup.js
File metadata and controls
75 lines (66 loc) · 3.23 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
// Language Translator Extension - Popup Script
const defaultConfig = {
apiKey: '',
model: 'google/gemini-2.5-flash',
targetLanguage: 'English',
wordPrompt: "Translate the word '{{text}}' into {{language}}. Provide a brief explanation if helpful.",
selectionPrompt: "Translate this text into {{language}}: {{text}}",
maxConversationTurns: 3,
verificationModel: '',
verificationPrompt: "Please verify this translation and provide the correct version if needed:\n\nOriginal: {{text}}\nTranslation: {{translation}}\nTarget Language: {{language}}"
};
// Load saved configuration
async function loadConfig() {
try {
const result = await chrome.storage.sync.get('translatorConfig');
const config = result.translatorConfig || defaultConfig;
document.getElementById('apiKey').value = config.apiKey || '';
document.getElementById('model').value = config.model || defaultConfig.model;
document.getElementById('targetLanguage').value = config.targetLanguage || defaultConfig.targetLanguage;
document.getElementById('wordPrompt').value = config.wordPrompt || defaultConfig.wordPrompt;
document.getElementById('selectionPrompt').value = config.selectionPrompt || defaultConfig.selectionPrompt;
document.getElementById('maxTurns').value = config.maxConversationTurns || defaultConfig.maxConversationTurns;
document.getElementById('verificationModel').value = config.verificationModel || '';
document.getElementById('verificationPrompt').value = config.verificationPrompt || defaultConfig.verificationPrompt;
} catch (error) {
showStatus('Failed to load settings', 'error');
}
}
// Save configuration
async function saveConfig() {
const config = {
apiKey: document.getElementById('apiKey').value,
model: document.getElementById('model').value,
targetLanguage: document.getElementById('targetLanguage').value,
wordPrompt: document.getElementById('wordPrompt').value,
selectionPrompt: document.getElementById('selectionPrompt').value,
maxConversationTurns: parseInt(document.getElementById('maxTurns').value) || 3,
verificationModel: document.getElementById('verificationModel').value,
verificationPrompt: document.getElementById('verificationPrompt').value
};
try {
await chrome.storage.sync.set({ translatorConfig: config });
showStatus('Settings saved successfully!', 'success');
// Notify content script of config update
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
if (tabs[0]) {
chrome.tabs.sendMessage(tabs[0].id, { action: 'configUpdated' });
}
} catch (error) {
showStatus('Failed to save settings', 'error');
}
}
// Show status message
function showStatus(message, type) {
const statusEl = document.getElementById('status');
statusEl.textContent = message;
statusEl.className = `status ${type}`;
statusEl.style.display = 'block';
setTimeout(() => {
statusEl.style.display = 'none';
}, 3000);
}
// Event listeners
document.getElementById('saveButton').addEventListener('click', saveConfig);
// Load config on popup open
loadConfig();