-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy2clipboard.js
31 lines (25 loc) · 940 Bytes
/
copy2clipboard.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
function copyToClipboard(element) {
let commandElement = null;
const preElement = element.closest("pre");
const languageNoneElement = element.closest(".language-none");
if (preElement) {
commandElement = preElement.querySelector("code");
} else if (languageNoneElement) {
commandElement = languageNoneElement.querySelector(".command");
}
if (!commandElement) {
console.error("No code or command element found");
return;
}
const code = commandElement.textContent ? commandElement.textContent.trim() : commandElement.value;
navigator.clipboard.writeText(code).then(() => {
const copyIcon = element.querySelector(".copy-icon");
const checkIcon = element.querySelector(".check-icon");
copyIcon.classList.add("hidden");
checkIcon.classList.remove("hidden");
setTimeout(() => {
copyIcon.classList.remove("hidden");
checkIcon.classList.add("hidden");
}, 2000);
});
}