<script>
(function () {
    function codeText(code) {
        var ln = code.querySelectorAll(".hljs-ln .hljs-ln-code");
        if (ln.length) {
            var parts = [];
            for (var i = 0; i < ln.length; i++) parts.push(ln[i].textContent);
            return parts.join("\n");
        }
        return code.textContent;
    }

    document.querySelectorAll("#content pre").forEach(function (pre) {
        if (pre.classList.contains("code-processed")) return;
        var code = pre.querySelector("code");
        if (!code) return;
        pre.classList.add("code-processed");

        var btn = document.createElement("button");
        btn.className = "code-copy-btn";
        btn.type = "button";
        btn.textContent = "Copy";
        btn.addEventListener("click", function (e) {
            e.stopPropagation();
            var text = codeText(code);
            function copied() {
                btn.textContent = "Copied!";
                setTimeout(function () { btn.textContent = "Copy"; }, 1500);
            }
            if (navigator.clipboard && navigator.clipboard.writeText) {
                navigator.clipboard.writeText(text).then(copied);
            } else {
                var ta = document.createElement("textarea");
                ta.value = text;
                document.body.appendChild(ta);
                ta.select();
                document.execCommand("copy");
                document.body.removeChild(ta);
                copied();
            }
        });
        pre.appendChild(btn);
    });
})();
</script>