Update main.js

This commit is contained in:
jakubartur 2025-12-25 15:30:22 +01:00 committed by GitHub
parent 652d13c003
commit acabe61532
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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);
});
}
});