/ html / assets / js / copyCodeButton.js
copyCodeButton.js
 1  document.addEventListener("DOMContentLoaded", function () {
 2    const codeBoxes = document.querySelectorAll(".code-box");
 3  
 4    codeBoxes.forEach((box, index) => {
 5      const copyBtn = document.createElement("button");
 6      copyBtn.innerText = "📋";
 7  
 8      copyBtn.id = "copyCodeButton";
 9  
10      box.style.position = "relative";
11      box.appendChild(copyBtn);
12  
13      copyBtn.addEventListener("click", function () {
14        const lines = box.querySelectorAll(".hljs-ln-line");
15        if (lines.length > 0) {
16          const text = Array.from(lines)
17            .map(line => line.textContent.trimEnd())
18            .filter(line => line.length > 0)
19            .join("\n");
20          navigator.clipboard.writeText(text);
21          return;
22        }
23  
24        const code = box.querySelector("code");
25        if (code) {
26          navigator.clipboard.writeText(code.innerText.trimEnd());
27        }
28      });
29    });
30  });