-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
72 lines (64 loc) · 2.72 KB
/
options.js
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
document.addEventListener('DOMContentLoaded', function() {
// Load saved settings
chrome.storage.sync.get({
'apiProvider': 'openai',
'openaiApiKey': '',
'azureApiKey': '',
'azureEndpoint': ''
}, function(items) {
// Set the radio button
document.querySelector(`input[name="apiProvider"][value="${items.apiProvider}"]`).checked = true;
// Set the saved values
document.getElementById('openaiApiKey').value = items.openaiApiKey;
document.getElementById('azureApiKey').value = items.azureApiKey;
document.getElementById('azureEndpoint').value = items.azureEndpoint;
// Show the correct settings section
toggleSettings(items.apiProvider);
});
// Add change event listener for radio buttons
document.querySelectorAll('input[name="apiProvider"]').forEach(radio => {
radio.addEventListener('change', (e) => {
toggleSettings(e.target.value);
});
});
// Save settings
document.getElementById('save').addEventListener('click', function() {
const apiProvider = document.querySelector('input[name="apiProvider"]:checked').value;
const openaiApiKey = document.getElementById('openaiApiKey').value;
const azureApiKey = document.getElementById('azureApiKey').value;
const azureEndpoint = document.getElementById('azureEndpoint').value;
chrome.storage.sync.set({
apiProvider: apiProvider,
openaiApiKey: openaiApiKey,
azureApiKey: azureApiKey,
azureEndpoint: azureEndpoint
}, function() {
const status = document.getElementById('status');
status.textContent = chrome.i18n.getMessage('settingsSaved');
status.className = 'success';
setTimeout(function() {
status.textContent = '';
status.className = '';
}, 3000);
});
});
// Add keyboard shortcut for save
document.addEventListener('keydown', function(e) {
// Check for Ctrl+S or Cmd+S (on Mac)
if ((e.ctrlKey || e.metaKey) && e.key === 's') {
e.preventDefault(); // Prevent the browser's default save dialog
document.getElementById('save').click(); // Trigger the save button click
}
});
});
function toggleSettings(provider) {
const openaiSettings = document.getElementById('openaiSettings');
const azureSettings = document.getElementById('azureSettings');
if (provider === 'azure') {
openaiSettings.style.display = 'none';
azureSettings.style.display = 'block';
} else {
openaiSettings.style.display = 'block';
azureSettings.style.display = 'none';
}
}