Clipboard fix: fallback fuer HTTP (non-secure) context, SSH copy-to-clipboard
This commit is contained in:
@@ -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()
|
||||
}
|
||||
Reference in New Issue
Block a user