mirror of
https://gitee.com/mateos/mateclaw.git
synced 2026-09-14 03:33:43 +08:00
20 lines
615 B
TypeScript
20 lines
615 B
TypeScript
/**
|
|
* Copy text to clipboard with fallback for non-HTTPS contexts
|
|
* where navigator.clipboard is undefined.
|
|
*/
|
|
export async function copyToClipboard(text: string): Promise<void> {
|
|
if (navigator.clipboard) {
|
|
return navigator.clipboard.writeText(text)
|
|
}
|
|
// Fallback: textarea + execCommand('copy')
|
|
const ta = document.createElement('textarea')
|
|
ta.value = text
|
|
ta.style.position = 'fixed'
|
|
ta.style.left = '-9999px'
|
|
document.body.appendChild(ta)
|
|
ta.select()
|
|
const ok = document.execCommand('copy')
|
|
document.body.removeChild(ta)
|
|
if (!ok) throw new Error('Clipboard copy command failed')
|
|
}
|