declare global { interface Window { clipboardData?: { setData?: (dataType: 'Text', data: string) => boolean } } } /** * Copy to clipboard. * * @see http://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript */ export function copy(text: string): boolean { /** @see https://caniuse.com/?search=navigator.clipboard.writeText */ // @ts-ignore if(window?.navigator?.clipboard?.writeText) { navigator.clipboard.writeText(text) return true } if(window?.clipboardData?.setData) { /** IE specific code path to prevent textarea being shown while dialog is visible. */ return window.clipboardData.setData('Text', text) } else if(document.queryCommandSupported && document.queryCommandSupported('copy')) { const textarea = document.createElement('textarea') textarea.textContent = text textarea.style.position = 'fixed' // Prevent scrolling to bottom of page in MS Edge. document.body.appendChild(textarea) textarea.select() try { return document.execCommand('copy') // Security exception may be thrown by some browsers. } catch(ex) { return false } finally { document.body.removeChild(textarea) } } return false }