From bf6c30e3ff5e1524d16d5d98d61e656d2ecec0ad Mon Sep 17 00:00:00 2001 From: root Date: Thu, 29 Jan 2026 08:31:53 +0100 Subject: [PATCH] fix: Kopieren-Funktion im Dashboard repariert MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Änderungen: - Verbesserte copyEmbedCode() Funktion mit robusterem Clipboard-Handling - Fallback für ältere Browser hinzugefügt - Besseres Error-Handling und Logging - Erfolgs-Benachrichtigungen optimiert Die Kopieren-Funktion funktioniert jetzt zuverlässig in allen Browsern. --- js/dashboard.js | 85 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 66 insertions(+), 19 deletions(-) diff --git a/js/dashboard.js b/js/dashboard.js index 7662a11..dd85299 100644 --- a/js/dashboard.js +++ b/js/dashboard.js @@ -594,12 +594,58 @@ function getSessionId() { // ============================================ function copyEmbedCode() { const embedCode = document.getElementById('embedCode'); - if (!embedCode) return; + if (!embedCode) { + console.error('embedCode element not found'); + return; + } - const text = embedCode.textContent; - copyToClipboard('embedCode'); + // Get the text content and clean it up + let text = embedCode.textContent || embedCode.innerText; + text = text.trim(); - // Show success message + // Try modern clipboard API first + if (navigator.clipboard && navigator.clipboard.writeText) { + navigator.clipboard.writeText(text).then(() => { + showNotification('Code in Zwischenablage kopiert!', 'success'); + showCopySuccess(); + }).catch(err => { + console.error('Clipboard API failed:', err); + fallbackCopy(text); + }); + } else { + // Fallback for older browsers + fallbackCopy(text); + } +} + +function fallbackCopy(text) { + const textarea = document.createElement('textarea'); + textarea.value = text; + textarea.style.position = 'fixed'; + textarea.style.top = '0'; + textarea.style.left = '0'; + textarea.style.opacity = '0'; + document.body.appendChild(textarea); + textarea.focus(); + textarea.select(); + + try { + const successful = document.execCommand('copy'); + if (successful) { + showNotification('Code in Zwischenablage kopiert!', 'success'); + showCopySuccess(); + } else { + showNotification('Kopieren fehlgeschlagen. Bitte manuell kopieren.', 'error'); + } + } catch (err) { + console.error('Fallback copy failed:', err); + showNotification('Kopieren fehlgeschlagen. Bitte manuell kopieren.', 'error'); + } + + document.body.removeChild(textarea); +} + +function showCopySuccess() { const copySuccess = document.getElementById('copySuccess'); if (copySuccess) { copySuccess.style.display = 'flex'; @@ -611,23 +657,24 @@ function copyEmbedCode() { function copyToClipboard(elementId) { const element = document.getElementById(elementId); - if (!element) return; + if (!element) { + console.error('Element not found:', elementId); + return; + } - const text = element.textContent; + let text = element.textContent || element.innerText; + text = text.trim(); - navigator.clipboard.writeText(text).then(() => { - showNotification('In Zwischenablage kopiert', 'success'); - }).catch(err => { - console.error('Copy failed:', err); - // Fallback - const textarea = document.createElement('textarea'); - textarea.value = text; - document.body.appendChild(textarea); - textarea.select(); - document.execCommand('copy'); - document.body.removeChild(textarea); - showNotification('In Zwischenablage kopiert', 'success'); - }); + if (navigator.clipboard && navigator.clipboard.writeText) { + navigator.clipboard.writeText(text).then(() => { + showNotification('In Zwischenablage kopiert!', 'success'); + }).catch(err => { + console.error('Copy failed:', err); + fallbackCopy(text); + }); + } else { + fallbackCopy(text); + } } // Make functions available globally