index.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*! clipboard-copy. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
  2. /* global DOMException */
  3. module.exports = clipboardCopy
  4. function makeError () {
  5. return new DOMException('The request is not allowed', 'NotAllowedError')
  6. }
  7. async function copyClipboardApi (text) {
  8. // Use the Async Clipboard API when available. Requires a secure browsing
  9. // context (i.e. HTTPS)
  10. if (!navigator.clipboard) {
  11. throw makeError()
  12. }
  13. return navigator.clipboard.writeText(text)
  14. }
  15. async function copyExecCommand (text) {
  16. // Put the text to copy into a <span>
  17. const span = document.createElement('span')
  18. span.textContent = text
  19. // Preserve consecutive spaces and newlines
  20. span.style.whiteSpace = 'pre'
  21. span.style.webkitUserSelect = 'auto'
  22. span.style.userSelect = 'all'
  23. // Add the <span> to the page
  24. document.body.appendChild(span)
  25. // Make a selection object representing the range of text selected by the user
  26. const selection = window.getSelection()
  27. const range = window.document.createRange()
  28. selection.removeAllRanges()
  29. range.selectNode(span)
  30. selection.addRange(range)
  31. // Copy text to the clipboard
  32. let success = false
  33. try {
  34. success = window.document.execCommand('copy')
  35. } finally {
  36. // Cleanup
  37. selection.removeAllRanges()
  38. window.document.body.removeChild(span)
  39. }
  40. if (!success) throw makeError()
  41. }
  42. async function clipboardCopy (text) {
  43. try {
  44. await copyClipboardApi(text)
  45. } catch (err) {
  46. // ...Otherwise, use document.execCommand() fallback
  47. try {
  48. await copyExecCommand(text)
  49. } catch (err2) {
  50. throw (err2 || err || makeError())
  51. }
  52. }
  53. }