-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
58 lines (50 loc) · 1.53 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
document.addEventListener("DOMContentLoaded", async () => {
const commands = await browser.commands.getAll();
const toggleCommand = commands.find((cmd) => cmd.name === "toggle-tabs");
document.getElementById("shortcutInput").value = toggleCommand
? toggleCommand.shortcut
: "Alt+W";
// Handle shortcut changes
const shortcutInput = document.getElementById("shortcutInput");
shortcutInput.addEventListener("keydown", handleShortcutInput);
});
async function handleShortcutInput(e) {
e.preventDefault();
if (
e.key === "Control" ||
e.key === "Alt" ||
e.key === "Shift" ||
e.key === "Meta"
) {
return;
}
const modifiers = [];
if (e.ctrlKey) modifiers.push("Ctrl");
if (e.altKey) modifiers.push("Alt");
if (e.shiftKey) modifiers.push("Shift");
if (e.metaKey) modifiers.push("Meta");
if (modifiers.length === 0) {
return;
}
const shortcut = [...modifiers, e.key.toUpperCase()].join("+");
try {
await browser.commands.update({
name: "toggle-tabs",
shortcut: shortcut,
});
e.target.value = shortcut;
const status = document.getElementById("shortcutStatus");
status.textContent = "Shortcut updated successfully!";
status.style.color = "green";
setTimeout(() => {
status.textContent = "";
}, 2000);
} catch (error) {
const status = document.getElementById("shortcutStatus");
status.textContent = "This shortcut is already in use.";
status.style.color = "red";
setTimeout(() => {
status.textContent = "";
}, 2000);
}
}