-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
101 lines (86 loc) · 3.29 KB
/
Copy pathpopup.js
File metadata and controls
101 lines (86 loc) · 3.29 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
document.addEventListener('DOMContentLoaded', function() {
const startButton = document.getElementById('start-practice');
const saveButton = document.getElementById('save-settings');
const tabs = document.querySelectorAll('.tab');
// Load saved settings
loadSettings();
// Tab switching
tabs.forEach(tab => {
tab.addEventListener('click', function() {
// Remove active class from all tabs and content
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
// Add active class to current tab and content
const tabName = this.getAttribute('data-tab');
this.classList.add('active');
document.getElementById(`${tabName}-tab`).classList.add('active');
});
});
// Save API settings
saveButton.addEventListener('click', function() {
const apiKey = document.getElementById('api-key').value.trim();
// Validate inputs
if (!apiKey) {
alert('Please enter your OpenRouter API key');
return;
}
// Save settings to chrome.storage
chrome.storage.local.set({
apiKey: apiKey
}, function() {
alert('Settings saved successfully!');
});
});
startButton.addEventListener('click', async function() {
try {
// 使用配置中的默认值而不是从DOM元素中读取
const questionCount = config.defaultQuestionCount;
// Check if API key is set
chrome.storage.local.get(['apiKey'], function(result) {
if (!result.apiKey) {
alert('Please set your OpenRouter API key in the Settings tab before starting practice.');
// Switch to settings tab
document.querySelector('.tab[data-tab="settings"]').click();
return;
}
// Save settings to local storage
chrome.storage.local.set({ questionCount: questionCount });
// Get current active tab
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
if (!tabs[0]) {
alert('Unable to get current tab. Please try again.');
return;
}
// Send message to content script
chrome.tabs.sendMessage(
tabs[0].id,
{ action: 'startToeflPractice', questionCount: questionCount },
function(response) {
// Check if received response
if (chrome.runtime.lastError) {
alert('Unable to communicate with page. Error: ' + chrome.runtime.lastError.message);
return;
}
}
);
// Close popup
window.close();
});
});
} catch (error) {
alert('An error occurred: ' + error.message);
console.error('Error:', error);
}
});
// Function to load saved settings
function loadSettings() {
chrome.storage.local.get(['apiKey'], function(result) {
if (result.apiKey) {
document.getElementById('api-key').value = result.apiKey;
}
// Always set model and system prompt from config
document.getElementById('model').value = config.model;
document.getElementById('system-prompt').value = config.systemPrompt;
});
}
});