Github Commit Whitespace

Adds button to hide whitespaces from commit

目前为 2014-09-08 提交的版本。查看 最新版本

  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. // @copyright 2014+, jerone (http://jeroenvanwarmerdam.nl)
  7. // @license GNU GPLv3
  8. // @homepage https://github.com/jerone/UserScripts/tree/master/Github_Commit_Whitespace
  9. // @homepageURL https://github.com/jerone/UserScripts/tree/master/Github_Commit_Whitespace
  10. // @include https://github.com/*
  11. // @version 1.4.1
  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(".GithubCommitWhitespaceButton");
  24. if (r) { r.parentElement.removeChild(r); }
  25.  
  26. var on = /w=/.test(location.search); // any occurense results in enabling;
  27.  
  28. var b = e.querySelector(".toc-diff-stats");
  29.  
  30. var s = document.createElement("span");
  31. s.textContent = " \u2423";
  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("minibutton", "tooltipped", "tooltipped-n");
  36. if (on) { a.classList.add("selected"); }
  37. a.setAttribute("href", url(on));
  38. a.setAttribute("rel", "nofollow");
  39. a.setAttribute("aria-label", on ? "Show commit whitespace" : "Hide commit whitespaces");
  40. a.appendChild(s);
  41.  
  42. var g = document.createElement("div");
  43. g.classList.add("GithubCommitWhitespaceButton", "button-group", "right");
  44. g.style.margin = "0 10px 0 0"; // give us some room;
  45. g.appendChild(a);
  46.  
  47. b.parentNode.insertBefore(g, b);
  48. }
  49.  
  50. function url(on) {
  51. var searches = location.search.replace(/^\?/, "").split("&").filter(function(item) {
  52. return item && !/w=.*/.test(item);
  53. });
  54. if (!on) {
  55. searches.push("w=1");
  56. }
  57. return location.href.replace(location.search, "")
  58. + (searches.length > 0 ? "?" + searches.join("&") : "");
  59. }
  60.  
  61. // init;
  62. addButton();
  63.  
  64. // on pjax;
  65. unsafeWindow.$(document).on("pjax:end", addButton); // `pjax:end` also runs on history back;
  66.  
  67. // on PR files tab;
  68. var f;
  69. if ((f = document.querySelector(".js-pull-request-tab[data-container-id='files_bucket']"))) {
  70. f.addEventListener("click", function() {
  71. window.setTimeout(addButton, 13);
  72. });
  73. }
  74.  
  75. })();