-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
55 lines (51 loc) · 1.86 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
document.addEventListener('DOMContentLoaded', async () => {
// 创建 JSONEditor 实例
const container = document.getElementById('jsoneditor');
const options = {
mode: 'tree', // 使用树形模式
modes: ['tree', 'view', 'form', 'code', 'text'], // 允许切换的模式
onError: function(err) {
alert(err.toString());
},
onModeChange: function(newMode, oldMode) {
console.log('Mode switched from', oldMode, 'to', newMode);
}
};
const editor = new JSONEditor(container, options);
const saveButton = document.getElementById('save');
const resetButton = document.getElementById('reset');
// 加载当前配置
try {
const result = await chrome.storage.sync.get('config');
if (result.config) {
editor.set(result.config);
} else {
const response = await fetch(chrome.runtime.getURL('config.json'));
const defaultConfig = await response.json();
editor.set(defaultConfig);
}
} catch (error) {
console.error('加载配置失败:', error);
}
// 保存配置
saveButton.addEventListener('click', async () => {
try {
const newConfig = editor.get();
await chrome.storage.sync.set({ config: newConfig });
alert('配置已保存');
} catch (error) {
alert('配置格式错误: ' + error);
}
});
// 重置配置
resetButton.addEventListener('click', async () => {
try {
const response = await fetch(chrome.runtime.getURL('config.json'));
const defaultConfig = await response.json();
editor.set(defaultConfig);
await chrome.storage.sync.set({ config: defaultConfig });
} catch (error) {
alert('重置失败: ' + error);
}
});
});