diff options
author | Francesco Tomaselli <tomaselli.fr@gmail.com> | 2025-01-17 10:03:35 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-01-17 10:03:35 +0100 |
commit | 05e9fc84ff0ab6d9ac371c7b1d85432cc143c7fb (patch) | |
tree | 2f2b16f94cd37d93026fa5532b8f06a7ac9eda09 /static/js/copy-code.js | |
parent | 86bb1d6cda6a59a05d24558c83b6e3c780138f96 (diff) | |
parent | 156f1eb9623a54843a728936b08851cdb7aa21e3 (diff) |
Merge pull request #64 from arunmathaisk/main
Diffstat (limited to 'static/js/copy-code.js')
-rw-r--r-- | static/js/copy-code.js | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/static/js/copy-code.js b/static/js/copy-code.js new file mode 100644 index 0000000..67fd936 --- /dev/null +++ b/static/js/copy-code.js @@ -0,0 +1,33 @@ +document.addEventListener("DOMContentLoaded", function () { + const codeBlocks = document.querySelectorAll("pre"); + + codeBlocks.forEach((codeBlock) => { + const copyButton = document.createElement("button"); + copyButton.className = "copy-code-button"; + copyButton.textContent = "copy"; + + // Insert the button inside the <pre> block + codeBlock.appendChild(copyButton); + + copyButton.addEventListener("click", function () { + const code = codeBlock.querySelector("code"); + // Get the code content + const textToCopy = code.textContent || code.innerText; + + // Use the Clipboard API to copy the text + navigator.clipboard + .writeText(textToCopy) + .then(() => { + // Change button text to "Copied" + copyButton.textContent = "copied"; + + setTimeout(() => { + copyButton.textContent = "copy"; + }, 2000); // Reset the button text after 2 seconds + }) + .catch((err) => { + console.error("Unable to copy text:", err); + }); + }); + }); +}); |