block.
(function () {
"use strict";
function addCopyButton(block) {
var button = document.createElement("button");
button.className = "copybutton";
button.title = "Copy";
button.setAttribute("aria-label", "Copy code to clipboard");
button.innerHTML =
'
';
button.addEventListener("click", function () {
var code = block.querySelector("pre").innerText;
navigator.clipboard.writeText(code).then(
function () {
button.classList.add("copied");
setTimeout(function () {
button.classList.remove("copied");
}, 2000);
},
function () {
// Fallback for older browsers
var ta = document.createElement("textarea");
ta.value = code;
ta.style.position = "fixed";
ta.style.opacity = "0";
document.body.appendChild(ta);
ta.focus();
ta.select();
document.execCommand("copy");
document.body.removeChild(ta);
button.classList.add("copied");
setTimeout(function () {
button.classList.remove("copied");
}, 2000);
}
);
});
block.style.position = "relative";
block.appendChild(button);
}
document.addEventListener("DOMContentLoaded", function () {
document.querySelectorAll("div.highlight").forEach(addCopyButton);
});
})();