Github Commit Whitespace

Adds button to hide whitespaces from commit

目前為 2014-09-04 提交的版本,檢視 最新版本

  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
  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);
  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-s");
  36. if (on) { a.classList.add("selected"); }
  37. a.setAttribute("href", url(on));
  38. a.setAttribute("title", on ? "Show commit whitespace" : "Hide commit whitespaces");
  39. a.setAttribute("rel", "nofollow");
  40. a.setAttribute("aria-label", a.getAttribute("title"));
  41. a.appendChild(s);
  42.  
  43. var g = document.createElement("div");
  44. g.classList.add("GithubCommitWhitespaceButton", "button-group", "right");
  45. g.style.margin = "0 10px 0 0"; // give us some room;
  46. g.appendChild(a);
  47.  
  48. b.parentNode.insertBefore(g, b);
  49. }
  50.  
  51. function url(on) {
  52. var searches = location.search.replace(/^\?/, "").split("&").filter(function(item) {
  53. return item && !/w=.*/.test(item);
  54. });
  55. if (!on) {
  56. searches.push("w=1");
  57. }
  58. return location.href.replace(location.search, "")
  59. + (searches.length > 0 ? "?" + searches.join("&") : "");
  60. }
  61.  
  62. // init;
  63. addButton();
  64.  
  65. // on pjax;
  66. unsafeWindow.$(document).on("pjax:end", addButton); // `pjax:end` also runs on history back;
  67.  
  68. // on PR files tab;
  69. var f;
  70. if ((f = document.querySelector(".js-pull-request-tab[data-container-id='files_bucket']"))) {
  71. f.addEventListener("click", function() {
  72. window.setTimeout(addButton, 13);
  73. });
  74. }
  75.  
  76. })();