h5-copy.js 928 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. export function h5Copy(content) {
  2. if (!document.queryCommandSupported('copy')) {
  3. // 不支持
  4. return false
  5. }
  6. let textarea = document.createElement("textarea")
  7. textarea.value = content
  8. textarea.readOnly = "readOnly"
  9. document.body.appendChild(textarea)
  10. textarea.select() // 选择对象
  11. textarea.setSelectionRange(0, content.length) //核心
  12. let result = document.execCommand("copy") // 执行浏览器复制命令
  13. textarea.remove()
  14. return result
  15. }
  16. /**
  17. * 获取系统剪贴板内容
  18. */
  19. export function getClipboardData() {
  20. return new Promise((success, fail) => {
  21. // #ifndef H5
  22. uni.getClipboardData({
  23. success: ({ data }) => success(data),
  24. fail
  25. })
  26. // #endif
  27. // #ifdef H5
  28. try {
  29. navigator.clipboard.readText().then(success).catch(fail)
  30. } catch (error) {
  31. fail(error)
  32. }
  33. // #endif
  34. })
  35. }