GitHub Toggle Expanders

A userscript that toggles all expanders when one expander is shift-clicked

当前为 2022-10-24 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Toggle Expanders
  3. // @version 2.1.0
  4. // @description A userscript that toggles all expanders when one expander is shift-clicked
  5. // @license MIT
  6. // @author Rob Garrison
  7. // @namespace https://github.com/Mottie
  8. // @include https://github.com/*
  9. // @run-at document-idle
  10. // @require https://greasyfork.org/scripts/398877-utils-js/code/utilsjs.js?version=1079637
  11. // @icon https://github.githubassets.com/pinned-octocat.svg
  12. // @supportURL https://github.com/Mottie/GitHub-userscripts/issues
  13. // ==/UserScript==
  14. (() => {
  15. /* global $ $$ on */
  16. "use strict";
  17.  
  18. // Commit history toggle
  19. // https://github.com/torvalds/linux/commits/master
  20. function toggleButton(el, modKey) {
  21. const stateNode = el.closest(".js-details-container");
  22. const state = stateNode && (
  23. // Resolved expander
  24. stateNode.classList.contains("open") ||
  25. // compare detail expander
  26. stateNode.classList.contains("Details--on")
  27. );
  28. const parentNode = stateNode && stateNode.closest(modKey
  29. // shift+ctrl+click = expand all on page
  30. ?
  31. ".repository-content"
  32. // shift+click = expand all in date
  33. :
  34. ".Box--condensed"
  35. );
  36.  
  37. if (parentNode) {
  38. const containers = parentNode.querySelectorAll(".js-details-container");
  39. [...containers].forEach(node => {
  40. node.classList.toggle("open", state);
  41. node.classList.toggle("Details--on", state);
  42. });
  43. }
  44. }
  45.  
  46. // Toggle resolved/outdated comments
  47. // https://github.com/PowerShell/PowerShell/pull/18210
  48. function toggleDetails(el, modKey) {
  49. // clicked button has the previous state
  50. const state = el && el.classList.contains("Details-content--closed");
  51. const parentNode = el.closest(modKey
  52. // shift+ctrl+click = expand all on page
  53. ?
  54. ".js-discussion"
  55. // shift+click = expand all in date
  56. :
  57. ".js-timeline-item"
  58. );
  59.  
  60. if (parentNode) {
  61. $$("turbo-frame", parentNode).forEach(node => {
  62. const details = $("details", node);
  63. if (state) {
  64. details.setAttribute("open", state);
  65. } else {
  66. details.removeAttribute("open");
  67. }
  68. });
  69. }
  70. }
  71.  
  72. on($("body"), "click", event => {
  73. const target = event.target;
  74. const mod = event.ctrlKey ||
  75. event.metaKey ||
  76. window.location.pathname.includes("/compare/");
  77.  
  78. if (target && event.getModifierState("Shift")) {
  79. // give GitHub time to update the elements
  80. setTimeout(() => {
  81. if (target.matches(".js-details-target")) {
  82. toggleButton(target, mod);
  83. } else if (
  84. target.matches(".Details-content--closed, .Details-content--open")
  85. ) {
  86. toggleDetails(target, mod);
  87. }
  88. }, 100);
  89. }
  90. });
  91.  
  92. })();