You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
636 B
JavaScript

3 years ago
copies = document.querySelectorAll("[data-copy]");
Array.from(copies).forEach((copy) => {
clipboard = document.createElement("button");
clipboard.classList.add("clipboard");
clipboard.innerHTML = "Copy to clipboard";
insertAfter(clipboard, copy);
clipboard.addEventListener("click", (e) => {
navigator.clipboard.writeText(copy.dataset.copy).then(
function () {
clipboard.innerHTML = "Copied!";
},
function (err) {
console.error("Async: Could not copy text: ", err);
}
);
});
});
function insertAfter(newNode, referenceNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}