GitHub Toggle Wiki Sidebar

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

当前为 2017-04-14 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Toggle Wiki Sidebar
  3. // @version 1.0.5
  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. // @author Rob Garrison
  7. // @namespace https://github.com/Mottie
  8. // @include https://github.com/*
  9. // @run-at document-idle
  10. // @grant GM_addStyle
  11. // @grant GM_getValue
  12. // @grant GM_setValue
  13. // @require https://gf.qytechs.cn/scripts/28721-mutations/code/mutations.js?version=188072
  14. // @icon https://github.com/fluidicon.png
  15. // ==/UserScript==
  16. (() => {
  17. "use strict";
  18.  
  19. // disable click targeting of button SVG internals
  20. GM_addStyle(".ghtws-button > * { pointer-events: none; }");
  21.  
  22. // sidebar state
  23. let isHidden = false,
  24.  
  25. 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>";
  26.  
  27. function addToggle() {
  28. if (document.querySelector("#wiki-wrapper") && !document.querySelector(".ghtws-button")) {
  29. let el = document.querySelector(".gh-header-actions") || document.querySelector(".gh-header-title");
  30. const button = document.createElement("div");
  31. button.className = "btn btn-sm tooltipped tooltipped-s ghtws-button";
  32. button.innerHTML = toggleIcon;
  33. button.setAttribute("aria-label", "Toggle Sidebar");
  34. if (el.nodeName === "H1") {
  35. // non-editable wiki pages
  36. button.style.float = "right";
  37. el = el.parentNode;
  38. }
  39. // editable wikis have a "header-actions" area
  40. // prepend button
  41. el.insertBefore(button, el.childNodes[0]);
  42. if (isHidden) {
  43. toggleSidebar();
  44. }
  45. }
  46. }
  47.  
  48. function toggleSidebar() {
  49. const sidebar = document.querySelector("#wiki-rightbar"),
  50. wrapper = sidebar && sidebar.parentNode;
  51. if (sidebar) {
  52. if (isHidden) {
  53. sidebar.style.display = "none";
  54. wrapper.classList.remove("has-rightbar");
  55. } else {
  56. sidebar.style.display = "";
  57. wrapper.classList.add("has-rightbar");
  58. }
  59. GM_setValue("sidebar-state", isHidden);
  60. }
  61. }
  62.  
  63. function toggleEvent(event) {
  64. const target = event.target;
  65. if (target && target.classList.contains("ghtws-button")) {
  66. isHidden = !isHidden;
  67. toggleSidebar();
  68. }
  69. }
  70.  
  71. function init() {
  72. isHidden = GM_getValue("sidebar-state", false);
  73. document.querySelector("body").addEventListener("click", toggleEvent);
  74. addToggle();
  75. }
  76.  
  77. document.addEventListener("ghmo:container", addToggle);
  78. init();
  79. })();

QingJ © 2025

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