copy_text.js

复制文字内容到剪切板

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/492927/1362733/copy_textjs.js

  1. // ==UserScript==
  2. // @name copy_text.js
  3. // @namespace https://github.com/iibeibei
  4. // @version 0.0.1
  5. // @description 复制文字内容到剪切板
  6. // @author Beibei
  7. // @license GPLv3
  8. // @match *://*/*
  9. // ==/UserScript==
  10.  
  11. self.copyText = function (button, content, success) {
  12. if (!button) {
  13. return;
  14. }
  15.  
  16. if (typeof content == 'function') {
  17. success = content;
  18. content = null;
  19. }
  20.  
  21. success = success || function () {};
  22.  
  23. // 是否降级使用
  24. var isFallback = !navigator.clipboard;
  25.  
  26. if (typeof button == 'string' && !content) {
  27. if (content === false) {
  28. isFallback = true;
  29. }
  30. content = button;
  31. button = null;
  32. }
  33.  
  34. var eleTextarea = document.querySelector('#tempTextarea');
  35. if (!eleTextarea && isFallback) {
  36. eleTextarea = document.createElement('textarea');
  37. eleTextarea.style.width = 0;
  38. eleTextarea.style.position = 'fixed';
  39. eleTextarea.style.left = '-999px';
  40. eleTextarea.style.top = '10px';
  41. eleTextarea.setAttribute('readonly', 'readonly');
  42. document.body.appendChild(eleTextarea);
  43. }
  44.  
  45. var funCopy = function (text, callback) {
  46. callback = callback || function () {};
  47.  
  48. if (!isFallback) {
  49. navigator.clipboard.writeText(text).then(
  50. function () {
  51. callback();
  52. // 成功回调
  53. success(text);
  54. },
  55. function () {
  56. // 禁止写入剪切板后使用兜底方法
  57. copyText(text, false);
  58. callback();
  59. // 成功回调
  60. success(text);
  61. }
  62. );
  63.  
  64. return;
  65. }
  66.  
  67. eleTextarea.value = text;
  68. eleTextarea.select();
  69. document.execCommand('copy', true);
  70.  
  71. callback();
  72. // 成功回调
  73. success(text);
  74. };
  75.  
  76. // 提示复制成功的方法
  77. // 对外可访问
  78. copyText.tips = function (event) {
  79. if (!event) {
  80. return;
  81. }
  82. // 复制成功提示
  83. var eleTips = document.createElement('span');
  84. eleTips.className = 'text-popup';
  85. eleTips.innerHTML = '复制成功';
  86. document.body.appendChild(eleTips);
  87. // 事件
  88. eleTips.addEventListener('animationend', function () {
  89. eleTips.parentNode.removeChild(eleTips);
  90. });
  91. // For IE9
  92. if (!history.pushState) {
  93. setTimeout(function () {
  94. eleTips.parentNode.removeChild(eleTips);
  95. }, 1000);
  96. }
  97.  
  98. eleTips.style.left = event.pageX - eleTips.clientWidth / 2 + 'px';
  99. eleTips.style.top = event.pageY - eleTips.clientHeight + 'px';
  100. };
  101.  
  102. var strStyle =
  103. '.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); }}';
  104.  
  105. var eleStyle = document.querySelector('#popupStyle');
  106. if (!eleStyle) {
  107. eleStyle = document.createElement('style');
  108. eleStyle.id = 'popupStyle';
  109. eleStyle.innerHTML = strStyle;
  110. document.head.appendChild(eleStyle);
  111. }
  112.  
  113. if (!button) {
  114. funCopy(content);
  115. return;
  116. }
  117.  
  118. // 事件绑定
  119. button.addEventListener('click', function (event) {
  120. var strCopy = content;
  121. if (content && content.tagName) {
  122. strCopy = content.textContent || content.value;
  123. }
  124. // 复制的文字内容
  125. if (!strCopy) {
  126. return;
  127. }
  128.  
  129. funCopy(strCopy, function () {
  130. copyText.tips(event);
  131. });
  132. });
  133. };