GitHub Toggle Wiki Sidebar

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

  1. // ==UserScript==
  2. // @name GitHub Toggle Wiki Sidebar
  3. // @version 1.1.3
  4. // @description A userscript that adds a button to toggle the GitHub Wiki sidebar
  5. // @license MIT
  6. // @author Rob Garrison
  7. // @namespace https://github.com/Mottie
  8. // @match 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=1108163
  14. // @require https://gf.qytechs.cn/scripts/398877-utils-js/code/utilsjs.js?version=1079637
  15. // @icon https://github.githubassets.com/pinned-octocat.svg
  16. // @supportURL https://github.com/Mottie/GitHub-userscripts/issues
  17. // ==/UserScript==
  18.  
  19. /* global $ make on */
  20. (() => {
  21. "use strict";
  22.  
  23. // disable click targeting of button SVG internals
  24. // classes "mr-1" = 4px & "mr-2" = 8px; silly GitHub
  25. GM_addStyle(`
  26. .ghtws-button > * { pointer-events: none; }
  27. .ghtws-button { margin-right: 6px; }`
  28. );
  29.  
  30. // sidebar state
  31. let isHidden = false;
  32.  
  33. const toggleIcon = `
  34. <svg class="octicon" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
  35. <path fill="none" stroke="currentColor" stroke-miterlimit="10" d="M.5 3.5h10v9H.5z"/>
  36. <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"/>
  37. </svg>`;
  38.  
  39. function addToggle() {
  40. if ($("#wiki-wrapper") && !$(".ghtws-button")) {
  41. let el = $(".gh-header-actions") || $(".gh-header-title");
  42. const button = make({
  43. el: "button",
  44. className: `btn btn-sm tooltipped tooltipped-s ghtws-button${isHidden ? " selected" : ""}`,
  45. html: toggleIcon,
  46. attrs: {
  47. type: "button",
  48. "aria-label": "Toggle Sidebar"
  49. }
  50. });
  51. if (el.nodeName === "H1") {
  52. // non-editable wiki pages
  53. button.style.float = "right";
  54. el = el.parentNode;
  55. }
  56. // editable wikis have a "header-actions" area
  57. el.prepend(button);
  58. if (isHidden) {
  59. toggleSidebar();
  60. }
  61. }
  62. }
  63.  
  64. function toggleSidebar(button) {
  65. const sidebar = $(".wiki-rightbar");
  66. const wrapper = sidebar?.parentNode;
  67. if (sidebar && wrapper) {
  68. const action = isHidden ? "remove" : "add";
  69. button?.classList.toggle("selected", isHidden);
  70. wrapper.style.display = isHidden ? "none" : "";
  71. wrapper.classList[action]("has-rightbar");
  72. wrapper.previousElementSibling?.classList[action]("col-md-9");
  73. GM_setValue("sidebar-state", isHidden);
  74. }
  75. }
  76.  
  77. function toggleEvent(event) {
  78. const target = event.target;
  79. if (target && target.classList.contains("ghtws-button")) {
  80. isHidden = !isHidden;
  81. toggleSidebar(target);
  82. }
  83. }
  84.  
  85. function init() {
  86. isHidden = GM_getValue("sidebar-state", false);
  87. $("body").addEventListener("click", toggleEvent);
  88. addToggle();
  89. }
  90.  
  91. on(document, "ghmo:container", addToggle);
  92. init();
  93. })();

QingJ © 2025

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