Skip to content

Instantly share code, notes, and snippets.

@charisTheo
Created October 12, 2021 11:05
Show Gist options
  • Select an option

  • Save charisTheo/e011c4dae966e4e4cbeaa7b48c5fa9b8 to your computer and use it in GitHub Desktop.

Select an option

Save charisTheo/e011c4dae966e4e4cbeaa7b48c5fa9b8 to your computer and use it in GitHub Desktop.
export async function copyToClipboard (text) {
try {
// try to use Clipboard API
await navigator.clipboard.writeText(text);
return true
} catch (_) {
// Clipboard API is not supported
const el = document.createElement('textarea')
el.value = text
document.body.appendChild(el)
el.select()
const result = document.execCommand('copy')
document.body.removeChild(el)
return result === 'unsuccessful' ? false : true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment