29 lines
902 B
JavaScript
29 lines
902 B
JavaScript
document.addEventListener("DOMContentLoaded", function () {
|
|
const codeBlocks = document.querySelectorAll("pre, code");
|
|
|
|
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); // Revert text after 1 second
|
|
} catch (err) {
|
|
console.error("Unable to copy:", err);
|
|
}
|
|
|
|
selection.removeAllRanges();
|
|
});
|
|
});
|
|
});
|