GitHub Toggle Wiki Sidebar

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

当前为 2021-02-21 提交的版本,查看 最新版本

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

QingJ © 2025

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