diff --git a/_site/scripts/script.js b/_site/scripts/script.js index ea9dd0b..58f2775 100644 --- a/_site/scripts/script.js +++ b/_site/scripts/script.js @@ -60,6 +60,7 @@ function updateCode() { if (!e.dataset['codesrc']) { for (const code of e.querySelectorAll('code')) { addCommentStyle(code); + addCopyButton(e); } continue; } @@ -90,6 +91,7 @@ function updateCode() { code.textContent = text; addCommentStyle(code); e.appendChild(code); + addCopyButton(e); }); })(e); } @@ -116,6 +118,23 @@ function addCommentStyle(code) { } } +function addCopyButton(pre) { + pre.style.position = "relative"; + + const btn = document.createElement("button"); + btn.classList.add("copy-btn"); + btn.textContent = "Copy"; + + btn.addEventListener("click", () => { + navigator.clipboard.writeText(pre.querySelector("code").textContent.trim()).then(() => { + btn.textContent = "Copied"; + setTimeout(() => btn.textContent = "Copy", 700); + }); + }); + + pre.insertBefore(btn, pre.firstChild); +} + let tocLevel = 4; function disableTOC() { diff --git a/_site/styles/style.css b/_site/styles/style.css index cb7876b..dac6b63 100644 --- a/_site/styles/style.css +++ b/_site/styles/style.css @@ -748,6 +748,24 @@ ul.toplinks li img { opacity: 0.7; } +.copy-btn { + position: absolute; + top: 8px; + right: 8px; + border-radius: 8px; + padding: 2px 6px; + background: rgba(0,0,0,0.8); + color: #fff; + border: none; + cursor: pointer; + opacity: 0.7; + transition: opacity 0.2s ease, background 0.2s ease; +} + +.copy-btn:hover { + opacity: 1; +} + /* test image */ /*body > nav, main {