Github Commit Diff

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

Version au 20/03/2014. Voir la dernière 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.2
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14.  
  15. function addButton() {
  16. var e;
  17. if (!/\/commit\//.test(location.href) || !(e = document.querySelector(".explain"))) return;
  18.  
  19. var r = e.querySelector(".GithubCommitWhitespaceButton");
  20. if (r) r.parentElement.removeChild(r);
  21.  
  22. function getPatchOrDiffHref(type) {
  23. return (document.querySelector("link[type='text/plain+" + type + "']")
  24. || { href: location.href + "." + type }).href;
  25. };
  26.  
  27. var b = e.querySelector(".minibutton");
  28.  
  29. var s = document.createElement("span");
  30. s.textContent = " ";
  31. s.classList.add("octicon", "octicon-diff");
  32. s.style.color = "#333"; // set color because of css selector `p.explain .octicon`;
  33.  
  34. var a = document.createElement("a");
  35. a.classList.add("GithubCommitDiffButton", "minibutton", "tooltipped", "tooltipped-s");
  36. a.setAttribute("href", getPatchOrDiffHref("diff"));
  37. a.setAttribute("title", "Show commit diff.\r\nHold Shift to open commit patch.");
  38. a.setAttribute("rel", "nofollow");
  39. a.setAttribute("aria-label", a.getAttribute("title"));
  40. a.style.marginLeft = "10px"; // give us some room;
  41. a.appendChild(s);
  42. a.appendChild(document.createTextNode("Diff"));
  43.  
  44. b.parentNode.insertBefore(a, b);
  45.  
  46. a.addEventListener("click", function(e) {
  47. if (e.shiftKey) {
  48. e.preventDefault();
  49. location.href = getPatchOrDiffHref("patch");
  50. }
  51. }, false);
  52. }
  53.  
  54. // init;
  55. addButton();
  56.  
  57. // on pjax;
  58. unsafeWindow.$(document).on('pjax:success', addButton);
  59.  
  60. })();