Clipboard fix: fallback fuer HTTP (non-secure) context, SSH copy-to-clipboard

This commit is contained in:
cynfo3000
2026-03-03 23:54:56 +01:00
parent 025840e9ba
commit 983ecf390c
6 changed files with 99 additions and 43 deletions
+25
View File
@@ -0,0 +1,25 @@
/**
* Copy text to clipboard with fallback for HTTP (non-secure) contexts.
* navigator.clipboard.writeText requires HTTPS or localhost.
*/
export function copyToClipboard(text) {
if (navigator.clipboard && window.isSecureContext) {
return navigator.clipboard.writeText(text)
}
// Fallback: hidden textarea + execCommand
const textarea = document.createElement('textarea')
textarea.value = text
textarea.style.position = 'fixed'
textarea.style.left = '-9999px'
textarea.style.top = '-9999px'
document.body.appendChild(textarea)
textarea.focus()
textarea.select()
try {
document.execCommand('copy')
} catch (e) {
console.error('Copy failed:', e)
}
document.body.removeChild(textarea)
return Promise.resolve()
}