Skip to content

Instantly share code, notes, and snippets.

@artemsites
Last active July 16, 2024 16:25
Show Gist options
  • Save artemsites/8d2878cc86048e58a445b458ca04efac to your computer and use it in GitHub Desktop.
Save artemsites/8d2878cc86048e58a445b458ca04efac to your computer and use it in GitHub Desktop.
/**
* import { copyToClipboard } from "/src/utils/copyToClipboard.js"
*
* copyToClipboard("Этот текст будет скопирован в буфер обмена")
*/
export function copyToClipboard(text, callback) {
const textarea = document.createElement('textarea')
textarea.value = text
document.body.appendChild(textarea)
textarea.select()
try {
document.execCommand('copy')
console.log('Текст успешно скопирован')
callback()
} catch (err) {
console.error('Не удалось скопировать текст: ', err)
}
document.body.removeChild(textarea)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment