Greasy Fork is available in English.

Github Commit Diff

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

Per 27-07-2014. Zie de nieuwste versie.

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