Github Commit Diff

Adds button to show diff (or patch) file for commit

Fra 27.04.2016. Se den seneste versjonen.

  1. // ==UserScript==
  2. // @name Github Commit Diff
  3. // @namespace https://github.com/jerone/UserScripts
  4. // @description Adds button to show diff (or patch) file for commit
  5. // @author jerone
  6. // @copyright 2014+, jerone (http://jeroenvanwarmerdam.nl)
  7. // @license GNU GPLv3
  8. // @homepage https://github.com/jerone/UserScripts/tree/master/Github_Commit_Diff
  9. // @homepageURL https://github.com/jerone/UserScripts/tree/master/Github_Commit_Diff
  10. // @supportURL https://github.com/jerone/UserScripts/issues
  11. // @contributionURL https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=VCYMHWQ7ZMBKW
  12. // @icon https://github.com/fluidicon.png
  13. // @include https://github.com/*
  14. // @version 1.6.3
  15. // @grant none
  16. // ==/UserScript==
  17. /* global unsafeWindow */
  18.  
  19. (function() {
  20.  
  21. function addButton() {
  22. var e;
  23. if ((/\/commit\//.test(location.href) || /\/compare\//.test(location.href)) && (e = document.getElementById("toc"))) {
  24.  
  25. var r = e.querySelector(".GithubCommitDiffButton");
  26. if (r) {
  27. r.parentElement.removeChild(r);
  28. }
  29.  
  30. var b = e.querySelector(".toc-diff-stats");
  31.  
  32. const s = document.createElementNS("http://www.w3.org/2000/svg", "svg");
  33. s.classList.add("octicon", "octicon-diff");
  34. s.setAttributeNS(null, "height", 16);
  35. s.setAttributeNS(null, "width", 14);
  36. s.setAttributeNS(null, "viewBox", "0 0 14 16");
  37.  
  38. const p = document.createElementNS("http://www.w3.org/2000/svg", "path");
  39. p.setAttributeNS(null, "d", "M6 7h2v1H6v2h-1V8H3v-1h2V5h1v2zM3 13h5v-1H3v1z m4.5-11l3.5 3.5v9.5c0 0.55-0.45 1-1 1H1c-0.55 0-1-0.45-1-1V3c0-0.55 0.45-1 1-1h6.5z m2.5 4L7 3H1v12h9V6zM8.5 0S3 0 3 0v1h5l4 4v8h1V4.5L8.5 0z");
  40. s.appendChild(p);
  41.  
  42. var a = document.createElement("a");
  43. a.classList.add("btn", "btn-sm", "tooltipped", "tooltipped-n");
  44. a.setAttribute("href", getPatchOrDiffHref("diff"));
  45. a.setAttribute("rel", "nofollow");
  46. a.setAttribute("aria-label", "Show commit diff.\r\nHold Shift to open commit patch.");
  47. a.appendChild(s);
  48. a.appendChild(document.createTextNode(" Diff"));
  49.  
  50. var g = document.createElement("div");
  51. g.classList.add("GithubCommitDiffButton", "right");
  52. g.style.margin = "0 10px 0 0"; // give us some room;
  53. g.appendChild(a);
  54.  
  55. b.parentNode.insertBefore(g, b);
  56.  
  57. a.addEventListener("mousedown", mousedownEvent, false);
  58. a.addEventListener("mouseout", function() {
  59. a.setAttribute("href", getPatchOrDiffHref("diff"));
  60. }, false);
  61. } else if (/\/pull\/\d*\/files/.test(location.href) && (e = document.querySelector("#files_bucket .pr-toolbar .diffbar > .right"))) {
  62.  
  63. var r = e.querySelector(".GithubCommitDiffButton");
  64. if (r) {
  65. r.parentElement.removeChild(r);
  66. }
  67.  
  68. const s = document.createElementNS("http://www.w3.org/2000/svg", "svg");
  69. s.classList.add("octicon", "octicon-diff");
  70. s.setAttributeNS(null, "height", 16);
  71. s.setAttributeNS(null, "width", 14);
  72. s.setAttributeNS(null, "viewBox", "0 0 14 16");
  73.  
  74. const p = document.createElementNS("http://www.w3.org/2000/svg", "path");
  75. p.setAttributeNS(null, "d", "M6 7h2v1H6v2h-1V8H3v-1h2V5h1v2zM3 13h5v-1H3v1z m4.5-11l3.5 3.5v9.5c0 0.55-0.45 1-1 1H1c-0.55 0-1-0.45-1-1V3c0-0.55 0.45-1 1-1h6.5z m2.5 4L7 3H1v12h9V6zM8.5 0S3 0 3 0v1h5l4 4v8h1V4.5L8.5 0z");
  76. s.appendChild(p);
  77.  
  78. var a = document.createElement("a");
  79. a.classList.add("btn-link", "muted-link");
  80. a.setAttribute("href", getPatchOrDiffHref("diff"));
  81. a.setAttribute("rel", "nofollow");
  82. a.setAttribute("aria-label", "Show commit diff.\r\nHold Shift to open commit patch.");
  83. a.appendChild(s);
  84. a.appendChild(document.createTextNode(" Diff"));
  85.  
  86. var g = document.createElement("div");
  87. g.classList.add("GithubCommitDiffButton", "diffbar-item");
  88. g.appendChild(a);
  89.  
  90. e.insertBefore(g, e.firstChild);
  91.  
  92. a.addEventListener("mousedown", mousedownEvent, false);
  93. a.addEventListener("mouseout", function() {
  94. a.setAttribute("href", getPatchOrDiffHref("diff"));
  95. }, false);
  96. }
  97. }
  98.  
  99. function mousedownEvent(e) {
  100. if (e.shiftKey) {
  101. var patch = getPatchOrDiffHref("patch");
  102. e.preventDefault();
  103. a.setAttribute("href", patch);
  104. if (e.which === 1) { // left click;
  105. location.href = patch;
  106. // To prevent Firefox default behavior (opening a new window)
  107. // when pressing shift-click on a link, delete the link.
  108. this.parentElement.removeChild(this);
  109. } else if (e.which === 2) { // middle click;
  110. window.open(patch, "GithubCommitDiff");
  111. }
  112. } else {
  113. a.setAttribute("href", getPatchOrDiffHref("diff"));
  114. }
  115. }
  116.  
  117. function getPatchOrDiffHref(type) {
  118. return (document.querySelector("link[type='text/plain+" + type + "']") || document.querySelector("link[type='text/x-" + type + "']") || {
  119. href: location.href + "." + type
  120. }).href;
  121. }
  122.  
  123. // init;
  124. addButton();
  125.  
  126. // on pjax;
  127. document.addEventListener('pjax:end', addButton);
  128.  
  129. })();