Greasy Fork is available in English.

Copy github file to clipboard

Add copy github file text to clipboard

  1. // ==UserScript==
  2. // @name Copy github file to clipboard
  3. // @description Add copy github file text to clipboard
  4. // @namespace org.adong
  5. // @version 0.1
  6. // @author adong
  7. // @homepageURL https://github.com/ldong/github-copy-text
  8. // @supportURL https://github.com/ldong/github-copy-text/README.md
  9. // @match *://github.com/*
  10. // @match *://www.github.com/*
  11. // @include /^https?://git\.corp.*/.*$/
  12. // @grant GM_setClipboard
  13. // @grant GM_xmlhttpRequest
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. // copied from http://stackoverflow.com/a/36640126/2305243
  18. function CopyToClipboard(node) {
  19. var range;
  20. if (document.selection) {
  21. range = document.body.createTextRange();
  22. range.moveToElementText(node);
  23. range.select().createTextRange();
  24. document.execCommand("Copy");
  25.  
  26. } else if (window.getSelection) {
  27. range = document.createRange();
  28. range.selectNode(node);
  29. window.getSelection().addRange(range);
  30. document.execCommand("Copy");
  31. alert("text copied");
  32. }
  33. }
  34.  
  35.  
  36. var copyButton = document.createElement('button');
  37. copyButton.classList = 'btn btn-sm BtnGroup-item copy-file-another';
  38. copyButton.textContent = 'Copy Text';
  39.  
  40. copyButton.addEventListener('click', function(e) {
  41. e.preventDefault();
  42. var copyContentNode = document.querySelector(".file > .file-header").nextElementSibling;
  43. CopyToClipboard(copyContentNode);
  44. });
  45.  
  46.  
  47. var targetButtonGroupClass = '.BtnGroup.float-right';
  48. var targetButtonGroup = document.querySelector(targetButtonGroupClass);
  49. if (targetButtonGroup) {
  50. targetButtonGroup.appendChild(copyButton);
  51. } else {
  52. console.error('Please file a bug, or make a PR. ');
  53. }
  54.  
  55. })();