-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
48 lines (42 loc) · 1.24 KB
/
config.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
// 默认配置
const defaultConfig = {
API_KEY: window.env?.API_KEY || '',
API_URL: 'https://translation.googleapis.com/language/translate/v2'
};
// 配置管理类
class ConfigManager {
constructor() {
this.config = { ...defaultConfig };
}
async init() {
try {
const stored = await chrome.storage.local.get('translationConfig');
if (stored.translationConfig) {
this.config = { ...defaultConfig, ...stored.translationConfig };
} else {
// 如果没有存储的配置,使用环境变量中的默认值
this.config = { ...defaultConfig };
await this.saveConfig();
}
} catch (error) {
console.error('加载配置失败:', error);
}
}
async setApiKey(key) {
this.config.API_KEY = key;
await this.saveConfig();
}
getApiKey() {
return this.config.API_KEY;
}
async saveConfig() {
try {
await chrome.storage.local.set({
translationConfig: this.config
});
} catch (error) {
console.error('保存配置失败:', error);
}
}
}
window.configManager = new ConfigManager();