2026-01-11 17:35:50 +08:00
|
|
|
|
// 页面加载时读取存储的配置
|
2026-01-11 12:33:46 +08:00
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
2026-01-17 20:24:08 +08:00
|
|
|
|
// 同时获取 aiConfig 和 voiceEnabled 状态
|
|
|
|
|
|
chrome.storage.sync.get(['aiConfig', 'voiceEnabled'], (result) => {
|
2026-01-11 17:35:50 +08:00
|
|
|
|
const config = result.aiConfig || {};
|
|
|
|
|
|
|
2026-01-17 20:24:08 +08:00
|
|
|
|
// 填充 API 配置
|
2026-01-11 17:35:50 +08:00
|
|
|
|
document.getElementById('apiUrl').value = config.apiUrl || DEFAULT_URL;
|
|
|
|
|
|
document.getElementById('apiKey').value = config.apiKey || "";
|
|
|
|
|
|
document.getElementById('modelName').value = config.modelName || DEFAULT_MODEL;
|
2026-01-17 20:24:08 +08:00
|
|
|
|
|
|
|
|
|
|
// 填充语音开关状态(默认为关闭 false)
|
|
|
|
|
|
document.getElementById('voiceEnabled').checked = result.voiceEnabled || false;
|
2026-01-11 12:33:46 +08:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 保存逻辑
|
|
|
|
|
|
document.getElementById('save').addEventListener('click', () => {
|
|
|
|
|
|
const config = {
|
2026-01-11 17:35:50 +08:00
|
|
|
|
apiUrl: document.getElementById('apiUrl').value.trim() || DEFAULT_URL,
|
2026-01-11 12:33:46 +08:00
|
|
|
|
apiKey: document.getElementById('apiKey').value.trim(),
|
2026-01-11 17:35:50 +08:00
|
|
|
|
modelName: document.getElementById('modelName').value.trim() || DEFAULT_MODEL
|
2026-01-11 12:33:46 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-17 20:24:08 +08:00
|
|
|
|
const isVoiceEnabled = document.getElementById('voiceEnabled').checked;
|
|
|
|
|
|
|
2026-01-11 17:35:50 +08:00
|
|
|
|
// 验证 API Key 是否填写
|
2026-01-11 12:33:46 +08:00
|
|
|
|
if (!config.apiKey) {
|
|
|
|
|
|
showStatus("❌ 请输入 API Key", "#f56c6c");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-17 20:24:08 +08:00
|
|
|
|
// 存储到 chrome.storage.sync
|
|
|
|
|
|
chrome.storage.sync.set({
|
|
|
|
|
|
aiConfig: config,
|
|
|
|
|
|
voiceEnabled: isVoiceEnabled
|
|
|
|
|
|
}, () => {
|
2026-01-11 17:35:50 +08:00
|
|
|
|
showStatus("✅ 配置已保存,刷新页面生效", "#67c23a");
|
2026-01-11 12:33:46 +08:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-11 17:35:50 +08:00
|
|
|
|
// 状态提示函数
|
2026-01-11 12:33:46 +08:00
|
|
|
|
function showStatus(text, color) {
|
|
|
|
|
|
const status = document.getElementById('status');
|
|
|
|
|
|
status.textContent = text;
|
|
|
|
|
|
status.style.color = color;
|
2026-01-17 20:24:08 +08:00
|
|
|
|
|
|
|
|
|
|
// 3秒后清除提示
|
2026-01-11 17:35:50 +08:00
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
status.textContent = '';
|
|
|
|
|
|
}, 3000);
|
2026-01-11 12:33:46 +08:00
|
|
|
|
}
|