Github Commit Diff

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

As of 2014-02-27. See the latest version.

  1. // ==UserScript==
  2. // @name Github Commit Diff
  3. // @namespace https://greatest.deepsurf.us/scripts/77
  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. // @downloadURL https://greatest.deepsurf.us/scripts/77/code.user.js
  9. // @include http*://github.com/*
  10. // @version 1.20140227141649
  11. // @grant none
  12. // @updateURL https://greatest.deepsurf.us/scripts/77/code.meta.js
  13. // ==/UserScript==
  14.  
  15.  
  16. (function(){
  17.  
  18. function addButton() {
  19. if(!/\/commit\//.test(location.href) || !document.querySelector(".explain")) return;
  20. var r;
  21. if((r = document.querySelector(".GithubCommitDiffButton"))) r.parentElement.removeChild(r);
  22. function getPatchOrDiffHref(type){
  23. return (document.querySelector("link[type='text/plain+" + type + "']")
  24. || { href: location.href + "." + type }).href;
  25. };
  26. var b = document.querySelector(".explain .minibutton");
  27. var s = document.createElement("span");
  28. s.textContent = " ";
  29. s.classList.add("octicon", "octicon-diff");
  30. s.style.color = "#333"; // set color because of css selector `p.explain .octicon`;
  31. var a = document.createElement("a");
  32. a.classList.add("GithubCommitDiffButton", "minibutton", "tooltipped", "tooltipped-s");
  33. a.setAttribute("href", getPatchOrDiffHref("diff"));
  34. a.setAttribute("title", "Show commit diff.\r\nHold Shift to open commit patch.");
  35. a.setAttribute("rel", "nofollow");
  36. a.setAttribute("aria-label", a.getAttribute("title"));
  37. a.style.marginLeft = "10px"; // give us some room;
  38. a.appendChild(s);
  39. a.appendChild(document.createTextNode("Diff"));
  40. b.parentNode.insertBefore(a, b);
  41. a.addEventListener("click", function(e){
  42. if(e.shiftKey) {
  43. e.preventDefault();
  44. location.href = getPatchOrDiffHref("patch");
  45. }
  46. }, false);
  47. }
  48. // init;
  49. addButton();
  50. // on pjax;
  51. $(document).on('pjax:success', addButton);
  52.  
  53. })();