Github Commit Diff

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

  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 (https://github.com/jerone)
  7. // @license CC-BY-NC-SA-4.0; https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
  8. // @license GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
  9. // @homepage https://github.com/jerone/UserScripts/tree/master/Github_Commit_Diff
  10. // @homepageURL https://github.com/jerone/UserScripts/tree/master/Github_Commit_Diff
  11. // @supportURL https://github.com/jerone/UserScripts/issues
  12. // @contributionURL https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=VCYMHWQ7ZMBKW
  13. // @icon https://github.githubassets.com/pinned-octocat.svg
  14. // @include https://github.com/*
  15. // @exclude https://github.com/*/*.diff
  16. // @exclude https://github.com/*/*.patch
  17. // @version 1.6.7
  18. // @grant none
  19. // ==/UserScript==
  20.  
  21. // cSpell:ignore tooltipped, diffbar
  22.  
  23. (function () {
  24. function addButton() {
  25. var e;
  26. if (
  27. (/\/commit\//.test(location.href) ||
  28. /\/compare\//.test(location.href)) &&
  29. (e = document.getElementById("toc"))
  30. ) {
  31. let r = e.querySelector(".GithubCommitDiffButton");
  32. if (r) {
  33. r.parentElement.removeChild(r);
  34. }
  35.  
  36. let b = e.querySelector(".toc-diff-stats");
  37.  
  38. const s = document.createElementNS(
  39. "http://www.w3.org/2000/svg",
  40. "svg",
  41. );
  42. s.classList.add("octicon", "octicon-diff");
  43. s.setAttributeNS(null, "height", 16);
  44. s.setAttributeNS(null, "width", 14);
  45. s.setAttributeNS(null, "viewBox", "0 0 14 16");
  46.  
  47. const p = document.createElementNS(
  48. "http://www.w3.org/2000/svg",
  49. "path",
  50. );
  51. p.setAttributeNS(
  52. null,
  53. "d",
  54. "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",
  55. );
  56. s.appendChild(p);
  57.  
  58. let a = document.createElement("a");
  59. a.classList.add("btn", "btn-sm", "tooltipped", "tooltipped-n");
  60. a.setAttribute("href", getPatchOrDiffHref("diff"));
  61. a.setAttribute("rel", "nofollow");
  62. a.setAttribute(
  63. "aria-label",
  64. "Show commit diff.\r\nHold Shift to open commit patch.",
  65. );
  66. a.appendChild(s);
  67. a.appendChild(document.createTextNode(" Diff"));
  68.  
  69. let g = document.createElement("div");
  70. g.classList.add("GithubCommitDiffButton", "float-right");
  71. g.style.margin = "0 10px 0 0"; // Give us some room
  72. g.appendChild(a);
  73.  
  74. b.parentNode.insertBefore(g, b);
  75.  
  76. a.addEventListener("mousedown", mousedownEvent, false);
  77. a.addEventListener(
  78. "mouseout",
  79. function () {
  80. a.setAttribute("href", getPatchOrDiffHref("diff"));
  81. },
  82. false,
  83. );
  84. } else if (
  85. /\/pull\/\d*\/(files|commits)/.test(location.href) &&
  86. (e = document.querySelector(
  87. "#files_bucket .pr-toolbar .diffbar > .float-right",
  88. ))
  89. ) {
  90. let r = e.querySelector(".GithubCommitDiffButton");
  91. if (r) {
  92. r.parentElement.removeChild(r);
  93. }
  94.  
  95. const s = document.createElementNS(
  96. "http://www.w3.org/2000/svg",
  97. "svg",
  98. );
  99. s.classList.add("octicon", "octicon-diff");
  100. s.setAttributeNS(null, "height", 16);
  101. s.setAttributeNS(null, "width", 14);
  102. s.setAttributeNS(null, "viewBox", "0 0 14 16");
  103.  
  104. const p = document.createElementNS(
  105. "http://www.w3.org/2000/svg",
  106. "path",
  107. );
  108. p.setAttributeNS(
  109. null,
  110. "d",
  111. "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",
  112. );
  113. s.appendChild(p);
  114.  
  115. let a = document.createElement("a");
  116. a.classList.add(
  117. "btn",
  118. "btn-sm",
  119. "btn-outline",
  120. "tooltipped",
  121. "tooltipped-s",
  122. );
  123. a.setAttribute("href", getPatchOrDiffHref("diff"));
  124. a.setAttribute("rel", "nofollow");
  125. a.setAttribute(
  126. "aria-label",
  127. "Show commit diff.\r\nHold Shift to open commit patch.",
  128. );
  129. a.appendChild(s);
  130. a.appendChild(document.createTextNode(" Diff"));
  131.  
  132. let g = document.createElement("div");
  133. g.classList.add("GithubCommitDiffButton", "diffbar-item");
  134. g.appendChild(a);
  135.  
  136. e.insertBefore(g, e.firstChild);
  137.  
  138. a.addEventListener("mousedown", mousedownEvent, false);
  139. a.addEventListener(
  140. "mouseout",
  141. function () {
  142. a.setAttribute("href", getPatchOrDiffHref("diff"));
  143. },
  144. false,
  145. );
  146. }
  147. }
  148.  
  149. function mousedownEvent(e) {
  150. if (e.shiftKey) {
  151. var patch = getPatchOrDiffHref("patch");
  152. e.preventDefault();
  153. this.setAttribute("href", patch);
  154. if (e.which === 1) {
  155. // left click
  156. location.href = patch;
  157. // To prevent Firefox default behavior (opening a new window)
  158. // when pressing shift-click on a link, delete the link.
  159. this.parentElement.removeChild(this);
  160. } else if (e.which === 2) {
  161. // Middle click
  162. window.open(patch, "GithubCommitDiff");
  163. }
  164. } else {
  165. this.setAttribute("href", getPatchOrDiffHref("diff"));
  166. }
  167. }
  168.  
  169. function getPatchOrDiffHref(type) {
  170. return (
  171. document.querySelector('link[type="text/plain+' + type + '"]') ||
  172. document.querySelector('link[type="text/x-' + type + '"]') || {
  173. href: location.href + "." + type,
  174. }
  175. ).href;
  176. }
  177.  
  178. // Init
  179. addButton();
  180.  
  181. // Pjax
  182. document.addEventListener("pjax:end", addButton);
  183. })();