Github Commit Whitespace

Adds button to hide whitespaces from commit

当前为 2014-04-17 提交的版本,查看 最新版本

  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.1
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. // use case: https://github.com/OpenUserJs/OpenUserJS.org/commit/64530e854874433c76a584b90b14196522ef54a8
  14.  
  15. (function() {
  16.  
  17. function addButton() {
  18. var e;
  19. if (!(/\/commit\//.test(location.href) || /\/compare\//.test(location.href)) ||
  20. !(e = document.querySelector(".explain"))) return;
  21.  
  22. var r = e.querySelector(".GithubCommitWhitespaceButton");
  23. if (r) r.parentElement.removeChild(r);
  24.  
  25. var on = /w=/.test(location.search);
  26.  
  27. var b = e.querySelector(".minibutton");
  28.  
  29. var s = document.createElement("span");
  30. s.textContent = " \u2423";
  31. s.style.color = "#333"; // set color because of css selector `p.explain .octicon`;
  32.  
  33. var a = document.createElement("a");
  34. a.classList.add("GithubCommitWhitespaceButton", "minibutton", "tooltipped", "tooltipped-s");
  35. if (on) a.classList.add("selected");
  36. a.setAttribute("href", on ? location.href.replace(location.search, "") : location.href + "?w=1");
  37. a.setAttribute("title", on ? "Show commit whitespace" : "Hide commit whitespaces");
  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.  
  43. b.parentNode.insertBefore(a, b);
  44. }
  45.  
  46. // init;
  47. addButton();
  48.  
  49. // on pjax;
  50. unsafeWindow.$(document).on("pjax:success", addButton);
  51.  
  52. })();