Github Commit Diff

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

2014-11-04 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

  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. // @include https://github.com/*
  11. // @version 1.6.1
  12. // @grant none
  13. // ==/UserScript==
  14. /* global unsafeWindow */
  15.  
  16. (function() {
  17.  
  18. function addButton() {
  19. var e;
  20. if (!(/\/commit\//.test(location.href) || /\/compare\//.test(location.href) || /\/pull\/\d*\/files/.test(location.href)) ||
  21. !(e = document.getElementById("toc"))) { return; }
  22.  
  23. var r = e.querySelector(".GithubCommitDiffButton");
  24. if (r) { r.parentElement.removeChild(r); }
  25.  
  26. function getPatchOrDiffHref(type) {
  27. return (document.querySelector("link[type='text/plain+" + type + "']")
  28. || document.querySelector("link[type='text/x-" + type + "']")
  29. || { href: location.href + "." + type }).href;
  30. }
  31.  
  32. var b = e.querySelector(".toc-diff-stats");
  33.  
  34. var s = document.createElement("span");
  35. s.textContent = " ";
  36. s.classList.add("octicon", "octicon-diff");
  37. s.style.color = "#333"; // set color because of css selector `p.explain .octicon`;
  38.  
  39. var a = document.createElement("a");
  40. a.classList.add("minibutton", "tooltipped", "tooltipped-n");
  41. a.setAttribute("href", getPatchOrDiffHref("diff"));
  42. a.setAttribute("rel", "nofollow");
  43. a.setAttribute("aria-label", "Show commit diff.\r\nHold Shift to open commit patch.");
  44. a.appendChild(s);
  45. a.appendChild(document.createTextNode(" Diff"));
  46.  
  47. var g = document.createElement("div");
  48. g.classList.add("GithubCommitDiffButton", "button-group", "right");
  49. g.style.margin = "0 10px 0 0"; // give us some room;
  50. g.appendChild(a);
  51.  
  52. b.parentNode.insertBefore(g, b);
  53.  
  54. a.addEventListener("mousedown", function(e) {
  55. if (e.shiftKey) {
  56. var patch = getPatchOrDiffHref("patch");
  57. e.preventDefault();
  58. a.setAttribute("href", patch);
  59. if (e.which === 1) { // left click;
  60. location.href = patch;
  61. // To prevent Firefox default behavior (opening a new window)
  62. // when pressing shift-click on a link, delete the link.
  63. this.parentElement.removeChild(this);
  64. } else if (e.which === 2) { // middle click;
  65. window.open(patch, "GithubCommitDiff");
  66. }
  67. } else {
  68. a.setAttribute("href", getPatchOrDiffHref("diff"));
  69. }
  70. }, false);
  71. a.addEventListener("mouseout", function() {
  72. a.setAttribute("href", getPatchOrDiffHref("diff"));
  73. }, false);
  74. }
  75.  
  76. // init;
  77. addButton();
  78.  
  79. // on pjax;
  80. unsafeWindow.$(document).on("pjax:end", addButton); // `pjax:end` also runs on history back;
  81.  
  82. // on PR files tab;
  83. var f;
  84. if ((f = document.querySelector(".js-pull-request-tab[data-container-id='files_bucket']"))) {
  85. f.addEventListener("click", function() {
  86. window.setTimeout(addButton, 13);
  87. });
  88. }
  89.  
  90. })();