Github Commit Diff

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

Fra og med 14.05.2014. Se den nyeste version.

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