-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
176 lines (147 loc) · 5.58 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
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
document.addEventListener('DOMContentLoaded', restoreOptions);
document.getElementById('options-form').addEventListener('submit', saveOptions);
const shortcutButtons = document.querySelectorAll('.shortcut-btn');
shortcutButtons.forEach(button => button.addEventListener('click', handleShortcutButtonClick));
let currentButton = null;
let currentCombination = [];
function handleShortcutButtonClick(event) {
event.preventDefault();
if (currentButton && currentButton === event.target) {
stopRecording();
} else {
startRecording(event.target);
}
}
function startRecording(button) {
if (currentButton) {
stopRecording();
}
currentButton = button;
button.textContent = 'Stop Set';
currentCombination = [];
const inputId = button.id.replace('-btn', '');
const inputElement = document.getElementById(inputId);
function handleKeydown(event) {
if (currentButton !== button) return;
const keyCombination = getKeyCombination(event);
console.log(`Key combination captured: ${keyCombination}`);
if (!currentCombination.includes(keyCombination) && isValidKey(keyCombination)) {
currentCombination.push(keyCombination);
}
inputElement.value = currentCombination.join('+');
console.log(`Current combination: ${inputElement.value}`);
if (currentCombination.length >= 3 || isValidCombination(currentCombination)) {
stopRecording();
}
}
window.addEventListener('keydown', handleKeydown);
button.handleKeydown = handleKeydown;
}
function stopRecording() {
if (currentButton) {
window.removeEventListener('keydown', currentButton.handleKeydown);
currentButton.textContent = 'Set Shortcut';
currentButton = null;
currentCombination = [];
}
}
function getKeyCombination(event) {
const keys = [];
if (event.altKey && !keys.includes('Alt')) keys.push('Alt');
if (event.ctrlKey && !keys.includes('Ctrl')) keys.push('Ctrl');
if (event.shiftKey && !keys.includes('Shift')) keys.push('Shift');
const key = event.key.toUpperCase();
if (!['ALT', 'CONTROL', 'SHIFT', 'META'].includes(key)) {
keys.push(key);
}
return keys.join('+');
}
function isValidKey(key) {
const valid = key.length > 0 && !['ALT', 'CONTROL', 'SHIFT', 'META'].includes(key);
console.log(`Is valid key "${key}": ${valid}`);
return valid;
}
function isValidCombination(combination) {
const validModifiers = ['Alt', 'Ctrl', 'Shift', 'Meta'];
const keys = combination.filter(key => !validModifiers.includes(key));
const modifiers = combination.filter(key => validModifiers.includes(key));
const valid = (modifiers.length >= 1 && modifiers.length <= 2) && keys.length === 1;
console.log(`Is valid combination "${combination.join('+')}": ${valid}`);
return valid;
}
function saveOptions(event) {
event.preventDefault();
const duplicateTab = document.getElementById('duplicate-tab').value;
const openNewTab = document.getElementById('open-new-tab').value;
const openRecentClosedTab = document.getElementById('open-recent-closed-tab').value;
const muteTab = document.getElementById('mute-tab').value;
const commands = {
"duplicate-tab": {
"suggested_key": { "default": duplicateTab },
"description": "Duplicate the current tab"
},
"open-new-tab": {
"suggested_key": { "default": openNewTab },
"description": "Open a new tab"
},
"open-recent-closed-tab": {
"suggested_key": { "default": openRecentClosedTab },
"description": "Open a new tab"
},
"mute-tab": {
"suggested_key": { "default": muteTab },
"description": "Muting focused tab"
}
};
// Validate commands
if (!validateCommands(commands)) {
alert('Invalid key combinations. Please make sure to use a valid combination.');
return;
}
browser.storage.sync.set({ commands }).then(() => {
console.log('Options saved');
});
updateCommands(commands);
}
function restoreOptions() {
browser.storage.sync.get('commands').then((result) => {
const commands = result.commands || {
"duplicate-tab": { "suggested_key": { "default": "Alt+Shift+D" } },
"open-new-tab": { "suggested_key": { "default": "Ctrl+T" } },
"open-recent-closed-tab": { "suggested_key": { "default": "Alt+Shift+T" } },
"mute-tab": { "suggested_key": { "default": "Alt+Shift+M" } },
};
document.getElementById('duplicate-tab').value = commands["duplicate-tab"].suggested_key.default;
document.getElementById('open-new-tab').value = commands["open-new-tab"].suggested_key.default;
document.getElementById('open-recent-closed-tab').value = commands["open-recent-closed-tab"].suggested_key.default;
document.getElementById('mute-tab').value = commands["mute-tab"].suggested_key.default;
});
}
function updateCommands(commands) {
for (const command in commands) {
const shortcut = commands[command].suggested_key.default;
if (shortcut) {
browser.commands.update({
name: command,
shortcut: shortcut
}).then(() => {
console.log(`${command} updated`);
}).catch((error) => {
console.error(`Error updating ${command}: `, error);
});
}
}
}
function validateCommands(commands) {
for (const command in commands) {
const combination = commands[command].suggested_key.default;
if (combination) { // Only validate non-empty combinations
console.log(`Validating command "${command}" with combination: ${combination}`);
if (!isValidCombination(combination.split('+'))) {
console.log(`Invalid combination for command "${command}": ${combination}`);
return false;
}
}
}
return true;
}