/*! clipboard-copy. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */ /* global DOMException */
module.exports = clipboardCopy
functionmakeError () { returnnewDOMException('The request is not allowed', 'NotAllowedError') }
asyncfunctioncopyClipboardApi (text) { // Use the Async Clipboard API when available. Requires a secure browsing // context (i.e. HTTPS) if (!navigator.clipboard) { throwmakeError() } return navigator.clipboard.writeText(text) }
asyncfunctioncopyExecCommand (text) { // Put the text to copy into a <span> const span = document.createElement('span') span.textContent = text
// Add the <span> to the page document.body.appendChild(span)
// Make a selection object representing the range of text selected by the user const selection = window.getSelection() const range = window.document.createRange() selection.removeAllRanges() range.selectNode(span) selection.addRange(range)
// Copy text to the clipboard let success = false try { success = window.document.execCommand('copy') } finally { // Cleanup selection.removeAllRanges() window.document.body.removeChild(span) }