33 lines
929 B
JavaScript
33 lines
929 B
JavaScript
async function copyText(text, button) {
|
|
try {
|
|
await navigator.clipboard.writeText(text);
|
|
} catch {
|
|
const input = document.createElement("textarea");
|
|
input.value = text;
|
|
input.setAttribute("readonly", "");
|
|
input.style.position = "fixed";
|
|
input.style.left = "-9999px";
|
|
document.body.appendChild(input);
|
|
input.select();
|
|
document.execCommand("copy");
|
|
document.body.removeChild(input);
|
|
}
|
|
|
|
if (!button) return;
|
|
const prev = button.textContent;
|
|
button.textContent = "Скопировано";
|
|
button.classList.add("is-copied");
|
|
setTimeout(() => {
|
|
button.textContent = prev;
|
|
button.classList.remove("is-copied");
|
|
}, 1500);
|
|
}
|
|
|
|
document.addEventListener("click", (event) => {
|
|
const button = event.target.closest("[data-copy]");
|
|
if (!button) return;
|
|
event.preventDefault();
|
|
const text = button.getAttribute("data-copy") || "";
|
|
if (text) copyText(text, button);
|
|
});
|