-
Notifications
You must be signed in to change notification settings - Fork 9
/
options.js
53 lines (43 loc) · 1.71 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
const shortcutInput = document.getElementById('shortcut');
const saveButton = document.getElementById('saveShortcut');
const message = document.getElementById('message');
// load saved shortcut from storage
chrome.storage.sync.get(['eyeProtectionShortcut'], function(result) {
if (result.eyeProtectionShortcut) {
shortcutInput.value = result.eyeProtectionShortcut;
}
});
// save shortcut and redirect
saveButton.addEventListener('click', function() {
const shortcut = shortcutInput.value;
// save the shortcut to chrome storage
chrome.storage.sync.set({eyeProtectionShortcut: shortcut}, function() {
// notification
message.textContent = 'Shortcut saved. Redirecting to Chrome Shortcuts page...';
message.style.display = 'block';
// redirection
chrome.tabs.create({ url: 'chrome://extensions/shortcuts' });
setTimeout(() => {
message.style.display = 'none';
}, 3000);
});
});
shortcutInput.addEventListener('keydown', function(e) {
e.preventDefault();
const keys = [];
if (e.ctrlKey) keys.push('Ctrl');
if (e.altKey) keys.push('Alt');
if (e.shiftKey) keys.push('Shift');
if (e.metaKey) keys.push('Command');
if (e.key.length === 1) {
keys.push(e.key.toUpperCase());
}
console.log(`Captured keys: ${keys.join('+')}`);
shortcutInput.value = keys.join('+');
});
document.getElementById('saveMessage').addEventListener('click', function() {
const message = document.getElementById('userinput').value;
chrome.storage.sync.set({ customMessage: message }, function() {
document.getElementById('messagesaved').textContent = 'Message saved successfully!';
});
});