Github Commit Whitespace

Adds button to hide whitespaces from commit

  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 (https://github.com/jerone)
  7. // @license CC-BY-NC-SA-4.0; https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
  8. // @license GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
  9. // @homepage https://github.com/jerone/UserScripts/tree/master/Github_Commit_Whitespace
  10. // @homepageURL https://github.com/jerone/UserScripts/tree/master/Github_Commit_Whitespace
  11. // @supportURL https://github.com/jerone/UserScripts/issues
  12. // @contributionURL https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=VCYMHWQ7ZMBKW
  13. // @icon https://github.githubassets.com/pinned-octocat.svg
  14. // @include https://github.com/*
  15. // @version 1.5.4
  16. // @grant none
  17. // ==/UserScript==
  18.  
  19. // cSpell:ignore tooltipped, diffbar
  20.  
  21. (function () {
  22. function addButton() {
  23. var e;
  24. if (
  25. (/\/commit\//.test(location.href) ||
  26. /\/compare\//.test(location.href)) &&
  27. (e = document.getElementById("toc"))
  28. ) {
  29. let r = e.querySelector(".GithubCommitWhitespaceButton");
  30. if (r) {
  31. r.parentElement.removeChild(r);
  32. }
  33.  
  34. let on = /w=/.test(location.search); // Any occurrence results in enabling
  35.  
  36. let b = e.querySelector(".toc-diff-stats");
  37.  
  38. let a = document.createElement("a");
  39. a.classList.add("btn", "btn-sm", "tooltipped", "tooltipped-n");
  40. if (on) {
  41. a.classList.add("selected");
  42. }
  43. a.setAttribute("href", url(on));
  44. a.setAttribute("rel", "nofollow");
  45. a.setAttribute(
  46. "aria-label",
  47. on ? "Show commit whitespace" : "Hide commit whitespace",
  48. );
  49. a.appendChild(document.createTextNode("\u2423"));
  50.  
  51. let g = document.createElement("div");
  52. g.classList.add("GithubCommitWhitespaceButton", "float-right");
  53. g.style.margin = "0 10px 0 0"; // Give us some room
  54. g.appendChild(a);
  55.  
  56. b.parentNode.insertBefore(g, b);
  57. } else if (
  58. /\/pull\/\d*\/(files|commits)/.test(location.href) &&
  59. (e = document.querySelector(
  60. "#files_bucket .pr-toolbar .diffbar > .pr-review-tools",
  61. ))
  62. ) {
  63. let r = e.querySelector(".GithubCommitWhitespaceButton");
  64. if (r) {
  65. r.parentElement.removeChild(r);
  66. }
  67.  
  68. let on = /w=/.test(location.search); // Any occurrence result in enabling
  69.  
  70. let a = document.createElement("a");
  71. a.classList.add(
  72. "btn",
  73. "btn-sm",
  74. "btn-outline",
  75. "tooltipped",
  76. "tooltipped-s",
  77. );
  78. a.setAttribute("href", url(on));
  79. a.setAttribute("rel", "nofollow");
  80. a.setAttribute(
  81. "aria-label",
  82. on ? "Show commit whitespace" : "Hide commit whitespace",
  83. );
  84. a.appendChild(document.createTextNode("\u2423"));
  85.  
  86. let g = document.createElement("div");
  87. g.classList.add("GithubCommitWhitespaceButton", "diffbar-item");
  88. g.appendChild(a);
  89.  
  90. e.insertBefore(g, e.firstChild);
  91. }
  92. }
  93.  
  94. function url(on) {
  95. var searches = location.search
  96. .replace(/^\?/, "")
  97. .split("&")
  98. .filter(function (item) {
  99. return item && !/w=.*/.test(item);
  100. });
  101. if (!on) {
  102. searches.push("w=1");
  103. }
  104. return (
  105. location.href
  106. .replace(location.search, "")
  107. .replace(location.hash, "") +
  108. (searches.length > 0 ? "?" + searches.join("&") : "") +
  109. location.hash
  110. );
  111. }
  112.  
  113. // Init
  114. addButton();
  115.  
  116. // Pjax
  117. document.addEventListener("pjax:end", addButton);
  118. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址