1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| function clipboardCopy (text) { if (navigator.clipboard) { return navigator.clipboard.writeText(text).catch(function (err) { throw (err !== undefined ? err : new DOMException('The request is not allowed', 'NotAllowedError')) }) }
const span = document.createElement('span') span.textContent = text
span.style.whiteSpace = 'pre'
document.body.appendChild(span)
const selection = window.getSelection() const range = window.document.createRange() selection.removeAllRanges() range.selectNode(span) selection.addRange(range)
let success = false try { success = window.document.execCommand('copy') } catch (err) { console.log('error', err) }
selection.removeAllRanges() window.document.body.removeChild(span)
return success ? Promise.resolve() : Promise.reject(new DOMException('The request is not allowed', 'NotAllowedError')) }
|