copy2clipboard

Copy Button for Code Box in stackoverflow.com etc.

  1. // ==UserScript==
  2. // @name copy2clipboard
  3. // @namespace guebosch
  4. // @version 1.2
  5. // @description Copy Button for Code Box in stackoverflow.com etc.
  6. // @author guebosch, https://github.com/pinple
  7. // @match https://stackoverflow.com/*
  8. // @match https://*.stackexchange.com/*
  9. // @match https://*.zhihu.com/*
  10. // @match https://www.jianshu.com/*
  11. // @match https://dev.to/*
  12. // @match https://superuser.com/*
  13. // @match *.github.io/*
  14. // @grant none
  15. // @license GPLv3
  16. // ==/UserScript==
  17.  
  18. // To remove IDE warnings
  19. var $ = window.jQuery;
  20.  
  21. (() => {
  22. "use strict";
  23.  
  24. function selectElementText(el) {
  25. var range = document.createRange();
  26. range.selectNodeContents(el);
  27. var selection = window.getSelection();
  28. selection.removeAllRanges();
  29. selection.addRange(range);
  30. }
  31.  
  32. function getSelectedText() {
  33. var t = '';
  34. if (window.getSelection) {
  35. t = window.getSelection();
  36. } else if (document.getSelection) {
  37. t = document.getSelection();
  38. } else if (document.selection) {
  39. t = document.selection.createRange().text;
  40. }
  41. return t;
  42. }
  43.  
  44. function copyToClipboard(text) {
  45. if (window.clipboardData && window.clipboardData.setData) {
  46. // IE specific code path to prevent textarea being shown while dialog is visible.
  47. return window.clipboardData.setData("Text", text);
  48.  
  49. } else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
  50. var textarea = document.createElement("textarea");
  51. textarea.textContent = text;
  52. textarea.style.position = "fixed"; // Prevent scrolling to bottom of page in MS Edge.
  53. document.body.appendChild(textarea);
  54. textarea.select();
  55. try {
  56. return document.execCommand("copy"); // Security exception may be thrown by some browsers.
  57. } catch (ex) {
  58. console.warn("Copy to clipboard failed.", ex);
  59. return false;
  60. } finally {
  61. document.body.removeChild(textarea);
  62. }
  63. }
  64. }
  65.  
  66. $("pre").each(function () {
  67. var pre = this;
  68. $(pre).wrapAll('<div style= "position: relative;"></div>');
  69.  
  70. var $copyCodeButton = $("<button class='copy-code-button'>Copy</button>");
  71. $copyCodeButton.css({
  72. "position": "absolute",
  73. "top": "1px",
  74. "right": "1px",
  75. "padding": "3px",
  76. "display": "none",
  77. "background-color": "white",
  78. "color": "#313E4E",
  79. "border-radius": "5px",
  80. "-moz-border-radius": "5px",
  81. "-webkit-border-radius": "5px",
  82. "border": "2px solid #CCCCCC"
  83. });
  84.  
  85. setTimeout(function () {
  86. if ($codeContainer.length == 0) {
  87. $(pre).contents().filter(function () {
  88. return this.className !== "copy-code-button";
  89. }).wrapAll('<code style= "overflow-x: auto; padding: 0px;"></code>');
  90. $codeContainer = $copyCodeButton.siblings("code").get(0);
  91. } else {
  92. $codeContainer = $codeContainer.get(0);
  93. }
  94. }, 0);
  95.  
  96. $copyCodeButton.click(function () {
  97. selectElementText($codeContainer);
  98. var selectedText = getSelectedText();
  99.  
  100. var buttonNewText = "";
  101. if (copyToClipboard(selectedText) == true) {
  102. buttonNewText = "Copied";
  103. selectElementText($codeContainer);
  104. } else {
  105. buttonNewText = "Unable to copy";
  106. selectElementText($codeContainer);
  107. }
  108.  
  109. $(this).text(buttonNewText);
  110. var that = this;
  111.  
  112. setTimeout(function () {
  113. $(that).text("Copy");
  114. var selection = window.getSelection(); // clear text range
  115. selection.removeAllRanges();
  116. }, 400);
  117. });
  118.  
  119. $(this).append($copyCodeButton);
  120. var $codeContainer = $copyCodeButton.siblings("code");
  121. $("pre").hover(function () {
  122. $(this).children(".copy-code-button").css("display", "block");
  123. }, function () {
  124. $(this).children(".copy-code-button").css("display", "none");
  125. });
  126. });
  127. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址