Github Commit Diff

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

Från och med 2014-11-06. Se den senaste versionen.

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