forked from intern2grow/technical-documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
37 lines (30 loc) · 969 Bytes
/
script.js
File metadata and controls
37 lines (30 loc) · 969 Bytes
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
"use strict";
const COPY_TIMEOUT = 1500; // Timeout duration in milliseconds
const copyCodeButtons = document.querySelectorAll(".btn.copy-code");
let timeoutId;
copyCodeButtons.forEach((btn) => {
btn.addEventListener("click", () => {
if (btn.classList.contains("copied")) {
// If already copied, clear the timeout and remove the copied class immediately
clearTimeout(timeoutId);
btn.classList.remove("copied");
return;
}
const text = btn.previousElementSibling.textContent;
btn.classList.add("copied");
copyTextToClipboard(text, btn);
});
});
function copyTextToClipboard(text, btn) {
navigator.clipboard
.writeText(text)
.then(() => {
// Set a timeout to remove the copied class after COPY_TIMEOUT milliseconds
timeoutId = setTimeout(() => {
btn.classList.remove("copied");
}, COPY_TIMEOUT);
})
.catch((err) => {
console.error("Failed to copy: ", err);
});
}