Github Commit Whitespace

Adds button to hide whitespaces from commit

2014-05-14 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

  1. // ==UserScript==
  2. // @name Github Commit Whitespace
  3. // @namespace https://github.com/jerone/UserScripts
  4. // @description Adds button to hide whitespaces from commit
  5. // @author jerone
  6. // @homepage https://github.com/jerone/UserScripts/tree/master/Github_Commit_Whitespace
  7. // @homepageURL https://github.com/jerone/UserScripts/tree/master/Github_Commit_Whitespace
  8. // @include https://github.com/*
  9. // @version 1.2.1
  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(".GithubCommitWhitespaceButton");
  22. if (r) { r.parentElement.removeChild(r); }
  23.  
  24. var on = /w=/.test(location.search);
  25.  
  26. var b = e.querySelector(".minibutton");
  27.  
  28. var s = document.createElement("span");
  29. s.textContent = " \u2423";
  30. s.style.color = "#333"; // set color because of css selector `p.explain .octicon`;
  31.  
  32. var a = document.createElement("a");
  33. a.classList.add("GithubCommitWhitespaceButton", "minibutton", "tooltipped", "tooltipped-s");
  34. if (on) { a.classList.add("selected"); }
  35. a.setAttribute("href", on ? location.href.replace(location.search, "") : location.href + "?w=1");
  36. a.setAttribute("title", on ? "Show commit whitespace" : "Hide commit whitespaces");
  37. a.setAttribute("rel", "nofollow");
  38. a.setAttribute("aria-label", a.getAttribute("title"));
  39. a.style.marginLeft = "10px"; // give us some room;
  40. a.appendChild(s);
  41.  
  42. b.parentNode.insertBefore(a, b);
  43. }
  44.  
  45. // init;
  46. addButton();
  47.  
  48. // on pjax;
  49. unsafeWindow.$(document).on("pjax:success", addButton);
  50.  
  51. // on PR files tab;
  52. var f;
  53. if ((f = document.querySelector(".js-pull-request-tab[data-container-id='files_bucket']"))) {
  54. f.addEventListener("click", function() {
  55. window.setTimeout(addButton, 13);
  56. });
  57. }
  58.  
  59. })();