GitHub Copy Code Snippet

A userscript adds a copy to clipboard button on hover of markdown code snippets

Από την 09/04/2018. Δείτε την τελευταία έκδοση.

  1. // ==UserScript==
  2. // @name GitHub Copy Code Snippet
  3. // @version 0.2.3
  4. // @description A userscript adds a copy to clipboard button on hover of markdown code snippets
  5. // @license MIT
  6. // @author Rob Garrison
  7. // @namespace https://github.com/Mottie
  8. // @include https://github.com/*
  9. // @include https://gist.github.com/*
  10. // @run-at document-idle
  11. // @grant GM_addStyle
  12. // @require https://greatest.deepsurf.us/scripts/28721-mutations/code/mutations.js?version=264157
  13. // @icon https://assets-cdn.github.com/pinned-octocat.svg
  14. // ==/UserScript==
  15. (() => {
  16. "use strict";
  17.  
  18. const markdownSelector = ".markdown-body, .markdown-format",
  19. codeSelector = "pre:not(.gh-csc-pre)",
  20.  
  21. copyButton = document.createElement("button");
  22.  
  23. addAttr(copyButton, {
  24. "aria-label": "Copy to clipboard",
  25. className: "js-zeroclipboard btn btn-sm zeroclipboard-button tooltipped tooltipped-w gh-csc-button",
  26. "data-copied-hint": "Copied!",
  27. type: "button",
  28. innerHTML: `
  29. <svg aria-hidden="true" class="octicon octicon-clippy" height="16" viewBox="0 0 14 16" width="14">
  30. <path fill-rule="evenodd" d="M2 13h4v1H2v-1zm5-6H2v1h5V7zm2 3V8l-3 3 3 3v-2h5v-2H9zM4.5 9H2v1h2.5V9zM2 12h2.5v-1H2v1zm9 1h1v2c-.02.28-.11.52-.3.7-.19.18-.42.28-.7.3H1c-.55 0-1-.45-1-1V4c0-.55.45-1 1-1h3c0-1.11.89-2 2-2 1.11 0 2 .89 2 2h3c.55 0 1 .45 1 1v5h-1V6H1v9h10v-2zM2 5h8c0-.55-.45-1-1-1H8c-.55 0-1-.45-1-1s-.45-1-1-1-1 .45-1 1-.45 1-1 1H3c-.55 0-1 .45-1 1z"></path>
  31. </svg>`
  32. });
  33.  
  34. GM_addStyle(`
  35. .gh-csc-wrap {
  36. position: relative;
  37. }
  38. .gh-csc-wrap:hover .gh-csc-button {
  39. display: block;
  40. }
  41. .gh-csc-button {
  42. display: none;
  43. position: absolute;
  44. top: 3px;
  45. right: 3px;
  46. z-index: 20;
  47. }
  48. .gh-csc-wrap.ghd-code-wrapper .gh-csc-button {
  49. right: 31px;
  50. }
  51. .gh-csc-button svg {
  52. vertical-align: text-bottom;
  53. }
  54. `);
  55.  
  56. function addAttr(el, attrs) {
  57. Object.keys(attrs).forEach(attr => {
  58. const value = attrs[attr];
  59. switch(attr) {
  60. case "className":
  61. el.className = value;
  62. break;
  63. case "innerHTML":
  64. el.innerHTML = value;
  65. break;
  66. default:
  67. el.setAttribute(attr, value);
  68. }
  69. });
  70. }
  71.  
  72. function addButton(wrap, code) {
  73. wrap.classList.add("gh-csc-wrap", "js-zeroclipboard-container");
  74. code.classList.add("gh-csc-code", "js-zeroclipboard-target");
  75. wrap.insertBefore(copyButton.cloneNode(true), wrap.childNodes[0]);
  76. }
  77.  
  78. function init() {
  79. const markdown = document.querySelector(markdownSelector);
  80. if (markdown) {
  81. [...markdown.querySelectorAll(codeSelector)].forEach(pre => {
  82. let code = pre.querySelector("code");
  83. let wrap = pre.parentNode;
  84. if (code) {
  85. // pre > code
  86. addButton(pre, code);
  87. } else if (wrap.classList.contains("highlight")) {
  88. // div.highlight > pre
  89. addButton(wrap, pre);
  90. }
  91. });
  92. }
  93. }
  94.  
  95. document.addEventListener("ghmo:container", init);
  96. document.addEventListener("ghmo:comments", init);
  97. init();
  98. })();