-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
88 lines (77 loc) · 2.86 KB
/
script.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
const insert = document.getElementById('insert');
const checkbox = document.getElementById('checkbox');
const html = document.querySelector('html');
// Check if dark mode is enabled from previous session
const darkMode = localStorage.getItem('darkMode');
// If dark mode is enabled, add the class to the body
if (darkMode) {
html.classList.add('dark');
checkbox.checked = true;
}
// Listen for a click on the checkbox
checkbox.addEventListener('change', (e) => {
// If the checkbox is checked, add the dark class and save the dark mode preference to local storage
if (e.target.checked) {
html.classList.add('dark');
localStorage.setItem('darkMode', 'true');
} else {
// If the checkbox is not checked, remove the dark class and remove the dark mode preference from local storage
html.classList.remove('dark');
localStorage.removeItem('darkMode');
}
});
// Listen for a keydown event on the window object
window.addEventListener('keydown', (e) => {
// Insert the key code into the div
insert.innerHTML = `
<div class="key">
${e.key === ' ' ? 'Space' : e.key}
<small>event.key</small>
<span class="tooltip">Copy</span>
</div>
<div class="key">
${e.code}
<small>event.code</small>
<span class="tooltip">Copy</span>
</div>
<div class="key deprecated">
${e.keyCode}
<small>event.keyCode</small>
<span class="tooltip">Copy</span>
</div>
`
// Get all the keys
const keys = document.querySelectorAll('.key');
// Loop through all the keys
keys.forEach((key) => {
// Listen for a click on the key
key.addEventListener("click", (e) => {
// Get the text of the key and copy it to the clipboard
const text = key.innerText.split('\n')[0];
copyToClipboard(text);
// Change the tooltip text to "Copied!"
const tooltip = key.querySelector(".tooltip");
tooltip.innerText = "Copied!";
// Change the tooltip styles to hide it after 1 second
setTimeout(() => {
tooltip.style = "top: 50px; opacity: 0; pointer-events: none;";
}, 1000);
// Change the tooltip text back to "Copy" after 3 seconds and remove the tooltip styles
setTimeout(() => {
tooltip.innerText = "Copy";
tooltip.removeAttribute("style");
}, 3000);
});
});
});
// Function to copy text to clipboard
function copyToClipboard(text) {
// copy text to clipboard using the new Clipboard API
navigator.clipboard.writeText(text)
.then(() => {
console.log('Copied to clipboard');
})
.catch(err => {
console.error('Failed to copy!', err);
});
}