GitHub Toggle Wiki Sidebar

A userscript that adds a button to toggle the GitHub Wiki sidebar

当前为 2016-04-02 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Toggle Wiki Sidebar
  3. // @version 1.0.0
  4. // @description A userscript that adds a button to toggle the GitHub Wiki sidebar
  5. // @license https://creativecommons.org/licenses/by-sa/4.0/
  6. // @namespace http://github.com/Mottie
  7. // @include https://github.com/*
  8. // @run-at document-idle
  9. // @grant GM_getValue
  10. // @grant GM_setValue
  11. // @author Rob Garrison
  12. // ==/UserScript==
  13. /* global GM_getValue, GM_setValue */
  14. /*jshint unused:true */
  15. (function() {
  16. "use strict";
  17.  
  18. var busy = false,
  19.  
  20. // sidebar state
  21. isHidden = false,
  22.  
  23. toggleIcon = "<svg class='octicon' xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16'><path fill='none' stroke='currentColor' stroke-miterlimit='10' d='M.5 3.5h10v9H.5z'/><path fill='currentColor' stroke='currentColor' stroke-miterlimit='10' d='M7 7.8l1.5-1.2V9zM10.5 3.5h5v9h-5v-9zm4.3 4.3l-4.3-3V11l4.3-3.2z'/></svg>",
  24.  
  25. addToggle = function() {
  26. if (document.querySelector("#wiki-wrapper") && !document.querySelector(".ghtws-button")) {
  27. busy = true;
  28. var el = document.querySelector(".gh-header-actions") || document.querySelector(".gh-header-title"),
  29. button = document.createElement("div");
  30. button.className = "btn btn-sm tooltipped tooltipped-s ghtws-button";
  31. button.innerHTML = toggleIcon;
  32. button.setAttribute("aria-label", "Toggle Sidebar");
  33. if (el.nodeName === "H1") {
  34. // non-editable wiki pages
  35. button.style.float = "right";
  36. el = el.parentNode;
  37. }
  38. // editable wikis have a "header-actions" area
  39. // prepend button
  40. el.insertBefore(button, el.childNodes[0]);
  41. if (isHidden) {
  42. toggleSidebar();
  43. }
  44. busy = false;
  45. }
  46. },
  47.  
  48. toggleSidebar = function() {
  49. busy = true;
  50. var sidebar = document.querySelector("#wiki-rightbar"),
  51. wrapper = sidebar && sidebar.parentNode;
  52. if (sidebar) {
  53. if (isHidden) {
  54. sidebar.style.display = "none";
  55. wrapper.classList.remove("has-rightbar");
  56. } else {
  57. sidebar.style.display = "";
  58. wrapper.classList.add("has-rightbar");
  59. }
  60. GM_setValue("sidebar-state", isHidden);
  61. }
  62. busy = false;
  63. },
  64.  
  65. regexPath = /path/i,
  66. regexSVG = /svg/i,
  67.  
  68. toggleEvent = function(event) {
  69. var target = event.target;
  70. // delegated event binding may fire on stuff inside the button...
  71. // also, the nodename may not be uppercase
  72. if (target.nodeName && regexPath.test(target.nodeName || "")) {
  73. target = target.parentNode;
  74. }
  75. if (target && regexSVG.test(target.nodeName || "")) {
  76. target = target.parentNode;
  77. }
  78. if (target && target.classList.contains("ghtws-button")) {
  79. isHidden = !isHidden;
  80. toggleSidebar();
  81. }
  82. },
  83.  
  84. init = function() {
  85. busy = true;
  86. isHidden = GM_getValue("sidebar-state", false);
  87. document.querySelector("body").addEventListener("click", toggleEvent);
  88. addToggle();
  89. // busy = false from addToggle();
  90. },
  91.  
  92. // DOM targets - to detect GitHub dynamic ajax page loading
  93. targets = document.querySelectorAll([
  94. "#js-repo-pjax-container",
  95. "#js-pjax-container"
  96. ].join(","));
  97.  
  98. // update TOC when content changes
  99. Array.prototype.forEach.call(targets, function(target) {
  100. new MutationObserver(function(mutations) {
  101. mutations.forEach(function(mutation) {
  102. // preform checks before adding code wrap to minimize function calls
  103. if (!busy && mutation.target === target) {
  104. addToggle();
  105. }
  106. });
  107. }).observe(target, {
  108. childList: true,
  109. subtree: true
  110. });
  111. });
  112.  
  113. init();
  114.  
  115. })();

QingJ © 2025

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