| 123456789101112131415161718192021222324252627282930313233343536 |
- function legacyCopyText(text: string): boolean {
- const textarea = document.createElement('textarea')
- const activeElement = document.activeElement instanceof HTMLElement ? document.activeElement : null
- textarea.value = text
- textarea.setAttribute('readonly', '')
- textarea.setAttribute('aria-hidden', 'true')
- textarea.style.cssText = 'position:fixed;left:-9999px;top:0;width:1px;height:1px;opacity:0'
- document.body.appendChild(textarea)
- textarea.focus({ preventScroll: true })
- textarea.select()
- textarea.setSelectionRange(0, text.length)
- let copied = false
- try {
- copied = document.execCommand('copy')
- } catch {
- copied = false
- } finally {
- textarea.remove()
- activeElement?.focus({ preventScroll: true })
- }
- return copied
- }
- /** Copy text in secure browsers and in embedded/HTTP browsers with a legacy fallback. */
- export async function copyTextToClipboard(text: string): Promise<boolean> {
- try {
- if (window.isSecureContext && navigator.clipboard?.writeText) {
- await navigator.clipboard.writeText(text)
- return true
- }
- } catch {
- // Permission-restricted embedded browsers can expose the API but reject it.
- }
- return legacyCopyText(text)
- }
|