GitHub CopyCode

Copy content of a code-block everywhere on GitHub

目前为 2018-09-07 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub CopyCode
  3. // @namespace https://github.com/KeyWeeUsr/Userscripts
  4. // @version 0.4
  5. // @description Copy content of a code-block everywhere on GitHub
  6. // @author Peter Badida
  7. // @copyright 2016+, Peter Badida
  8. // @license GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
  9. // @homepage https://github.com/KeyWeeUsr/Userscripts/tree/master/CopyCode
  10. // @supportURL https://github.com/KeyWeeUsr/Userscripts/issues
  11. // @icon https://assets-cdn.github.com/favicon.ico
  12. // @include *github.com*
  13. // @exclude *gist.github.com*
  14. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
  15. // @contributionURL https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=ACVM74AYCXVWQ
  16. // ==/UserScript==
  17. /* jshint -W097 */
  18. 'use strict';
  19.  
  20. (function () {
  21. function selectText(element) {
  22. var range;
  23. if (document.body.createTextRange) {
  24. range = document.body.createTextRange();
  25. range.moveToElementText(element);
  26. range.select();
  27. } else if (window.getSelection) {
  28. var selection = window.getSelection();
  29. range = document.createRange();
  30. range.selectNodeContents(element);
  31. selection.removeAllRanges();
  32. selection.addRange(range);
  33. }
  34. }
  35.  
  36. function addButtons() {
  37. $("pre").each(function (i, codeBlock) {
  38. var id = "codeButton-" + i;
  39. var parentId = "codeButton-parent-" + i;
  40. var codeContainer = $('<div id="' + parentId + '"></div>');
  41.  
  42. // ignore PRE if it's a GH online editor
  43. if (~String(codeBlock.getAttribute("class")).indexOf("CodeMirror")) {
  44. return;
  45. }
  46.  
  47. // ignore PRE if it already has a button
  48. var oldId = codeBlock.parentNode.getAttribute("id");
  49. if (~String(oldId).indexOf("-parent-")) {
  50. return;
  51. }
  52.  
  53. // put PRE block into DIV for easier adding of COPY button
  54. $(codeBlock).replaceWith(codeContainer);
  55. codeContainer.append(codeBlock);
  56.  
  57. // mark the container, so that it's ignored later
  58. codeContainer.attr("id", parentId);
  59.  
  60. // create COPY button on mouse enter
  61. codeContainer.mouseenter(function () {
  62. var codeButton = $('<div style="background-color: #000; ' +
  63. 'color: #fff; cursor: pointer; display:' +
  64. 'inline; font-size: 12pt; opacity: 0.5;' +
  65. 'padding: 3px; position: absolute;">Copy' +
  66. '</div>');
  67.  
  68. // give button an ID for easier removing later
  69. codeButton.attr("id", id);
  70.  
  71. codeContainer.append(codeButton);
  72. codeButton.css('top', codeContainer.position().top);
  73. codeButton.css('left',
  74. codeContainer.position().left +
  75. codeContainer.width() - 50);
  76.  
  77. // select the whole text in PRE and copy to the clipboard
  78. codeButton.click(function () {
  79. selectText(codeBlock);
  80. document.execCommand("copy");
  81. });
  82.  
  83. // hovering styles
  84. codeButton.mouseover(function () {
  85. codeButton.css('opacity', 1.0);
  86. });
  87. codeButton.mouseleave(function () {
  88. codeButton.css('opacity', 0.5);
  89. });
  90. });
  91.  
  92. // remove COPY button on mouse leave
  93. codeContainer.mouseleave(function () {
  94. $("#" + id).remove();
  95. });
  96. });
  97. }
  98.  
  99. function appendMe() {
  100. var av_style = "opacity: 0.3; border-radius: 10px;"
  101. var av_cont_style = "margin: 2px 10px -2px 0px;"
  102. var av = $('<li style="' + av_cont_style + '">' +
  103. '<div style="float: right;"><a href="' +
  104. 'https://github.com/KeyWeeUsr/Userscripts">' +
  105. '<img id="kwu_av" style="' + av_style + '" ' +
  106. 'src="https://github.com/identicons/KeyWeeUsr.png" ' +
  107. 'width="24"></img></a></div></li>');
  108.  
  109. av.mouseenter(function () {
  110. $('#kwu_av').css('opacity', 1.0);
  111. });
  112.  
  113. av.mouseleave(function () {
  114. $('#kwu_av').css('opacity', 0.3);
  115. });
  116. $('.pagehead-actions').prepend(av);
  117. }
  118.  
  119. function run() {
  120. addButtons();
  121. if (!document.getElementById('kwu_av')){
  122. appendMe();
  123. }
  124. }
  125. // repeat in case of switching e.g. GH tabs
  126. var runCheck = setInterval(run, 250)
  127. })();