copy.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @description 复制文字内容到剪切板,更多内容参见:https://www.zhangxinxu.com/wordpress/?p=10150
  3. * @author zhangxinxu(.com)
  4. * @created 2021-10-22
  5. */
  6. var copyText = function (button, content, success) {
  7. if (!button) {
  8. return;
  9. }
  10. if (typeof content == 'function') {
  11. success = content;
  12. content = null;
  13. }
  14. success = success || function () {};
  15. // 是否降级使用
  16. var isFallback = !navigator.clipboard;
  17. if (typeof button == 'string' && !content) {
  18. if (content === false) {
  19. isFallback = true;
  20. }
  21. content = button;
  22. button = null;
  23. }
  24. var eleTextarea = document.querySelector('#tempTextarea');
  25. if (!eleTextarea && isFallback) {
  26. eleTextarea = document.createElement('textarea');
  27. eleTextarea.style.width = 0;
  28. eleTextarea.style.position = 'fixed';
  29. eleTextarea.style.left = '-999px';
  30. eleTextarea.style.top = '10px';
  31. eleTextarea.setAttribute('readonly', 'readonly');
  32. document.body.appendChild(eleTextarea);
  33. }
  34. var funCopy = function (text, callback) {
  35. callback = callback || function () {};
  36. if (!isFallback) {
  37. navigator.clipboard.writeText(text).then(function () {
  38. callback();
  39. // 成功回调
  40. success(text);
  41. }, function() {
  42. // 禁止写入剪切板后使用兜底方法
  43. copyText(text, false);
  44. callback();
  45. // 成功回调
  46. success(text);
  47. });
  48. return;
  49. }
  50. eleTextarea.value = text;
  51. eleTextarea.select();
  52. document.execCommand('copy', true);
  53. callback();
  54. // 成功回调
  55. success(text);
  56. };
  57. if (!button) {
  58. funCopy(content);
  59. return;
  60. }
  61. // 事件绑定
  62. button.addEventListener('click', function (event) {
  63. var strCopy = content;
  64. if (content && content.tagName) {
  65. strCopy = content.textContent || content.value;
  66. }
  67. // 复制的文字内容
  68. if (!strCopy) {
  69. return;
  70. }
  71. funCopy(strCopy, function () {
  72. // 复制成功提示
  73. var eleTips = document.createElement('span');
  74. eleTips.className = 'text-popup';
  75. eleTips.innerHTML = '复制成功';
  76. document.body.appendChild(eleTips);
  77. // 事件
  78. eleTips.addEventListener('animationend', function() {
  79. eleTips.parentNode.removeChild(eleTips);
  80. });
  81. // For IE9
  82. if (!history.pushState) {
  83. setTimeout(function () {
  84. eleTips.parentNode.removeChild(eleTips);
  85. }, 1000);
  86. }
  87. eleTips.style.left = (event.pageX - eleTips.clientWidth / 2) + 'px';
  88. eleTips.style.top = (event.pageY - eleTips.clientHeight) + 'px';
  89. });
  90. });
  91. var strStyle = '.text-popup { animation: textPopup 1s both; -ms-transform: translateY(-20px); color: #01cf97; user-select: none; white-space: nowrap; position: absolute; z-index: 99; }@keyframes textPopup {0%, 100% { opacity: 0; } 5% { opacity: 1; } 100% { transform: translateY(-50px); }}'
  92. var eleStyle = document.querySelector('#popupStyle');
  93. if (!eleStyle) {
  94. eleStyle = document.createElement('style');
  95. eleStyle.id = 'popupStyle';
  96. eleStyle.innerHTML = strStyle;
  97. document.head.appendChild(eleStyle);
  98. }
  99. };
  100. /**
  101. * 输入文字到剪切板
  102. * @param {string} content
  103. * @returns
  104. */
  105. function copyText (content){
  106. return new Promise((resolve,reject)=>{
  107. // 判断是否支持 clipboard api 复制
  108. try {
  109. if (navigator.clipboard) {
  110. navigator.clipboard.writeText(content).then(resolve).catch(reject);
  111. } else {
  112. let textarea = document.createElement('textarea');
  113. // 隐藏此输入框
  114. textarea.style.position = 'fixed';
  115. textarea.style.opacity = '0';
  116. textarea.style.top = '20px';
  117. document.body.appendChild(textarea);
  118. // 赋值
  119. textarea.value = content;
  120. // 选中
  121. textarea.select();
  122. // 复制
  123. document.execCommand('copy', true);
  124. // 移除输入框
  125. document.body.removeChild(textarea);
  126. }
  127. resolve('ok');
  128. } catch (error) {
  129. reject(error);
  130. }
  131. })
  132. }