-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
282 lines (235 loc) · 7.89 KB
/
Copy pathoptions.js
File metadata and controls
282 lines (235 loc) · 7.89 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
class SettingsManager {
constructor() {
this.isRecording = false;
this.currentKeys = new Set();
this.init();
}
async init() {
await this.loadSettings();
this.setupEventListeners();
this.updateUI();
}
async loadSettings() {
try {
const result = await chrome.storage.sync.get({
// 默认设置
shortcut: 'Ctrl+Shift+T',
defaultView: 'all',
popupWidth: '400',
autoRefresh: true,
showFavicon: true,
enableSearch: true
});
this.settings = result;
console.log('加载设置:', this.settings);
} catch (error) {
console.error('加载设置失败:', error);
this.settings = {
shortcut: 'Ctrl+Shift+T',
defaultView: 'all',
popupWidth: '400',
autoRefresh: true,
showFavicon: true,
enableSearch: true
};
}
}
setupEventListeners() {
// 快捷键输入框
const shortcutInput = document.getElementById('shortcutInput');
shortcutInput.addEventListener('click', () => this.startRecording());
shortcutInput.addEventListener('keydown', (e) => this.handleKeyDown(e));
shortcutInput.addEventListener('keyup', (e) => this.handleKeyUp(e));
shortcutInput.addEventListener('blur', () => this.stopRecording());
// 重置快捷键按钮
document.getElementById('resetShortcut').addEventListener('click', () => {
this.settings.shortcut = 'Ctrl+Shift+T';
this.updateUI();
this.showStatus('快捷键已重置为默认值', 'success');
});
// 保存设置按钮
document.getElementById('saveSettings').addEventListener('click', () => {
this.saveSettings();
});
// 重置所有设置按钮
document.getElementById('resetSettings').addEventListener('click', () => {
if (confirm('确定要重置所有设置吗?此操作不可撤销。')) {
this.resetAllSettings();
}
});
// 其他设置项
document.getElementById('defaultView').addEventListener('change', (e) => {
this.settings.defaultView = e.target.value;
});
document.getElementById('popupWidth').addEventListener('change', (e) => {
this.settings.popupWidth = e.target.value;
});
document.getElementById('autoRefresh').addEventListener('change', (e) => {
this.settings.autoRefresh = e.target.checked;
});
document.getElementById('showFavicon').addEventListener('change', (e) => {
this.settings.showFavicon = e.target.checked;
});
document.getElementById('enableSearch').addEventListener('change', (e) => {
this.settings.enableSearch = e.target.checked;
});
}
startRecording() {
if (this.isRecording) return;
this.isRecording = true;
this.currentKeys.clear();
const shortcutInput = document.getElementById('shortcutInput');
shortcutInput.classList.add('recording');
shortcutInput.placeholder = '请按下快捷键组合...';
shortcutInput.focus();
this.showStatus('正在录制快捷键,请按下您想要的组合...', 'info');
}
stopRecording() {
if (!this.isRecording) return;
this.isRecording = false;
const shortcutInput = document.getElementById('shortcutInput');
shortcutInput.classList.remove('recording');
shortcutInput.placeholder = '点击这里设置快捷键';
if (this.currentKeys.size > 0) {
const shortcut = this.formatShortcut();
this.settings.shortcut = shortcut;
this.updateUI();
this.showStatus(`快捷键已设置为: ${shortcut}`, 'success');
}
}
handleKeyDown(e) {
if (!this.isRecording) return;
e.preventDefault();
e.stopPropagation();
const key = this.getKeyName(e);
if (key) {
this.currentKeys.add(key);
this.updateShortcutDisplay();
}
}
handleKeyUp(e) {
if (!this.isRecording) return;
e.preventDefault();
e.stopPropagation();
const key = this.getKeyName(e);
if (key) {
this.currentKeys.delete(key);
}
// 如果没有按键了,停止录制
if (this.currentKeys.size === 0) {
this.stopRecording();
}
}
getKeyName(e) {
const key = e.key.toLowerCase();
const modifiers = [];
if (e.ctrlKey) modifiers.push('Ctrl');
if (e.metaKey) modifiers.push('Cmd'); // Mac的Command键
if (e.altKey) modifiers.push('Alt');
if (e.shiftKey) modifiers.push('Shift');
// 特殊键处理
let keyName = key;
if (key === 'control' || key === 'meta' || key === 'alt' || key === 'shift') {
return null; // 忽略单独的修饰键
}
// 功能键
if (key.startsWith('f') && key.length > 1) {
keyName = key.toUpperCase();
}
// 其他特殊键
const specialKeys = {
'tab': 'Tab',
'enter': 'Enter',
'escape': 'Esc',
'backspace': 'Backspace',
'delete': 'Delete',
'arrowup': '↑',
'arrowdown': '↓',
'arrowleft': '←',
'arrowright': '→',
'pageup': 'PageUp',
'pagedown': 'PageDown',
'home': 'Home',
'end': 'End',
'insert': 'Insert',
'space': 'Space'
};
if (specialKeys[key]) {
keyName = specialKeys[key];
}
return [...modifiers, keyName].join('+');
}
updateShortcutDisplay() {
const shortcutInput = document.getElementById('shortcutInput');
const shortcut = this.formatShortcut();
shortcutInput.value = shortcut;
}
formatShortcut() {
if (this.currentKeys.size === 0) return '';
const keys = Array.from(this.currentKeys);
return keys.join(' + ');
}
updateUI() {
// 更新快捷键显示
document.getElementById('shortcutInput').value = this.settings.shortcut;
// 更新其他设置项
document.getElementById('defaultView').value = this.settings.defaultView;
document.getElementById('popupWidth').value = this.settings.popupWidth;
document.getElementById('autoRefresh').checked = this.settings.autoRefresh;
document.getElementById('showFavicon').checked = this.settings.showFavicon;
document.getElementById('enableSearch').checked = this.settings.enableSearch;
}
async saveSettings() {
try {
await chrome.storage.sync.set(this.settings);
// 更新扩展的快捷键
await this.updateExtensionShortcut();
this.showStatus('设置已保存!', 'success');
console.log('设置已保存:', this.settings);
} catch (error) {
console.error('保存设置失败:', error);
this.showStatus('保存设置失败,请重试', 'error');
}
}
async updateExtensionShortcut() {
try {
// 这里需要与background script通信来更新快捷键
// 由于Chrome扩展的限制,快捷键更新需要用户手动在chrome://extensions/shortcuts中设置
console.log('请手动在chrome://extensions/shortcuts中更新快捷键为:', this.settings.shortcut);
} catch (error) {
console.error('更新扩展快捷键失败:', error);
}
}
async resetAllSettings() {
try {
await chrome.storage.sync.clear();
this.settings = {
shortcut: 'Ctrl+Shift+T',
defaultView: 'all',
popupWidth: '400',
autoRefresh: true,
showFavicon: true,
enableSearch: true
};
this.updateUI();
this.showStatus('所有设置已重置为默认值', 'success');
} catch (error) {
console.error('重置设置失败:', error);
this.showStatus('重置设置失败,请重试', 'error');
}
}
showStatus(message, type = 'info') {
const statusElement = document.getElementById('status');
statusElement.textContent = message;
statusElement.className = `status ${type}`;
// 3秒后自动隐藏
setTimeout(() => {
statusElement.textContent = '';
statusElement.className = 'status';
}, 3000);
}
}
// 初始化设置管理器
document.addEventListener('DOMContentLoaded', () => {
new SettingsManager();
});