Add copy function

This commit is contained in:
James Musselman 2024-07-08 19:22:41 -05:00
parent a31d714e23
commit 95a3f2f83c
Signed by: Musselman
GPG key ID: 1DAEFF35ECB5D6DB

View file

@ -1,5 +1,29 @@
console.log("This site was generated by Hugo."); document.addEventListener("DOMContentLoaded", function () {
function copyText() { const codeBlocks = document.querySelectorAll("pre, code");
const text = document.getElementById("copyText").innerText;
navigator.clipboard.writeText(text); codeBlocks.forEach(function (codeBlock) {
} codeBlock.style.cursor = "pointer";
codeBlock.title = "Click to copy";
codeBlock.addEventListener("click", function () {
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(codeBlock);
selection.removeAllRanges();
selection.addRange(range);
try {
document.execCommand("copy");
const originalText = codeBlock.innerText;
codeBlock.innerText = "Copied!";
setTimeout(function () {
codeBlock.innerText = originalText;
}, 1000); // Reset the text after 1 second
} catch (err) {
console.error("Unable to copy:", err);
}
selection.removeAllRanges();
});
});
});