-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
21 lines (19 loc) · 759 Bytes
/
script.js
File metadata and controls
21 lines (19 loc) · 759 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
document.addEventListener("DOMContentLoaded", function () {
document.querySelectorAll("pre code").forEach((codeBlock) => {
// Create the copy button
const button = document.createElement("button");
button.className = "copy-button";
button.textContent = "Copy";
// Position the button inside the <pre> block
const pre = codeBlock.parentElement;
pre.style.position = "relative"; // Ensure proper positioning
pre.appendChild(button);
// Copy code to clipboard on button click
button.addEventListener("click", () => {
navigator.clipboard.writeText(codeBlock.innerText).then(() => {
button.textContent = "Copied!";
setTimeout(() => (button.textContent = "Copy"), 2000);
});
});
});
});