From acabe615326e48fda779f6a790e1f0163233c8c1 Mon Sep 17 00:00:00 2001 From: jakubartur <90887830+jakubartur@users.noreply.github.com> Date: Thu, 25 Dec 2025 15:30:22 +0100 Subject: [PATCH] Update main.js --- js/main.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/js/main.js b/js/main.js index 67f60279..2383ba2f 100644 --- a/js/main.js +++ b/js/main.js @@ -1105,3 +1105,59 @@ document.fonts.addEventListener("loadingdone", (event) => { // Initial call to set the margin based on current state updateMargin(); })(); + +//------------------------------------------------ +// Fix for Copy Button (with HTTP Fallback) +//------------------------------------------------ +document.addEventListener('click', function(e) { + const copyBtn = e.target.closest('.hljs-copy-button'); + if (!copyBtn) return; + + const pre = copyBtn.closest('pre'); + const code = pre ? pre.querySelector('code') : null; + + if (code) { + const text = code.innerText; + + function copyToClipboard(content) { + if (navigator.clipboard && window.isSecureContext) { + return navigator.clipboard.writeText(content); + } else { + return new Promise((resolve, reject) => { + const textArea = document.createElement("textarea"); + textArea.value = content; + textArea.style.position = "fixed"; + textArea.style.left = "-9999px"; + textArea.style.top = "-9999px"; + document.body.appendChild(textArea); + textArea.focus(); + textArea.select(); + try { + const successful = document.execCommand('copy'); + document.body.removeChild(textArea); + successful ? resolve() : reject(); + } catch (err) { + document.body.removeChild(textArea); + reject(err); + } + }); + } + } + + copyToClipboard(text).then(() => { + const originalText = copyBtn.innerText; + // Update button state + copyBtn.innerText = 'Copied!'; + copyBtn.setAttribute('data-copied', 'true'); + copyBtn.style.pointerEvents = 'none'; // Prevent double clicks + + setTimeout(() => { + copyBtn.innerText = 'Copy'; + copyBtn.setAttribute('data-copied', 'false'); + copyBtn.style.pointerEvents = 'auto'; + }, 2000); + }).catch(err => { + console.error('Copy failed:', err); + }); + } +});