Github Commit Diff

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

Mint 2014.09.04.. Lásd a legutóbbi verzió

  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
  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-s");
  41. a.setAttribute("href", getPatchOrDiffHref("diff"));
  42. a.setAttribute("title", "Show commit diff.\r\nHold Shift to open commit patch.");
  43. a.setAttribute("rel", "nofollow");
  44. a.setAttribute("aria-label", a.getAttribute("title"));
  45. a.appendChild(s);
  46. a.appendChild(document.createTextNode(" Diff"));
  47.  
  48. var g = document.createElement("div");
  49. g.classList.add("GithubCommitDiffButton", "button-group", "right");
  50. g.style.margin = "0 10px 0 0"; // give us some room;
  51. g.appendChild(a);
  52.  
  53. b.parentNode.insertBefore(g, b);
  54.  
  55. a.addEventListener("mousedown", function(e) {
  56. if (e.shiftKey) {
  57. var patch = getPatchOrDiffHref("patch");
  58. e.preventDefault();
  59. a.setAttribute("href", patch);
  60. if (e.which === 1) { // left click;
  61. location.href = patch;
  62. // To prevent Firefox default behavior (opening a new window)
  63. // when pressing shift-click on a link, delete the link.
  64. this.parentElement.removeChild(this);
  65. } else if (e.which === 2) { // middle click;
  66. window.open(patch, "GithubCommitDiff");
  67. }
  68. } else {
  69. a.setAttribute("href", getPatchOrDiffHref("diff"));
  70. }
  71. }, false);
  72. a.addEventListener("mouseout", function() {
  73. a.setAttribute("href", getPatchOrDiffHref("diff"));
  74. }, false);
  75. }
  76.  
  77. // init;
  78. addButton();
  79.  
  80. // on pjax;
  81. unsafeWindow.$(document).on("pjax:end", addButton); // `pjax:end` also runs on history back;
  82.  
  83. // on PR files tab;
  84. var f;
  85. if ((f = document.querySelector(".js-pull-request-tab[data-container-id='files_bucket']"))) {
  86. f.addEventListener("click", function() {
  87. window.setTimeout(addButton, 13);
  88. });
  89. }
  90.  
  91. })();