GitHub Toggle Expanders

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

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

  1. // ==UserScript==
  2. // @name GitHub Toggle Expanders
  3. // @version 2.0.1
  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. // @icon https://github.githubassets.com/pinned-octocat.svg
  11. // @supportURL https://github.com/Mottie/GitHub-userscripts/issues
  12. // ==/UserScript==
  13. (() => {
  14. "use strict";
  15.  
  16. function toggleButton(el, modKey) {
  17. const stateNode = el.closest(".js-details-container");
  18. const state = stateNode && (
  19. // Resolved expander
  20. stateNode.classList.contains("open") ||
  21. // compare detail expander
  22. stateNode.classList.contains("Details--on")
  23. );
  24. const parentNode = stateNode && stateNode.closest(modKey
  25. // shift+ctrl+click = expand all on page
  26. ? ".repository-content"
  27. // shift+click = expand all in date
  28. : ".Box--condensed"
  29. );
  30.  
  31. if (parentNode) {
  32. const containers = parentNode.querySelectorAll(".js-details-container");
  33. [...containers].forEach(node => {
  34. node.classList.toggle("open", state);
  35. node.classList.toggle("Details--on", state);
  36. });
  37. }
  38. }
  39.  
  40. function toggleDetails(el, modKey) {
  41. const state = el && el.open;
  42. const parentNode = el && el.closest(modKey
  43. ? "#discussion_bucket" // .js-discussion
  44. : ".discussion-item-body" // .container?
  45. );
  46. if (parentNode) {
  47. const containers = parentNode.querySelectorAll(
  48. ".outdated-comment, .js-comment-container"
  49. );
  50. [...containers].forEach(node => {
  51. node.open = state;
  52. });
  53. }
  54. }
  55.  
  56. document.body.addEventListener("click", event => {
  57. const target = event.target;
  58. const mod = event.ctrlKey
  59. || event.metaKey
  60. || window.location.pathname.includes("/compare/");
  61.  
  62. if (target && event.getModifierState("Shift")) {
  63. // give GitHub time to update the elements
  64. setTimeout(() => {
  65. if (target.matches(".js-details-target")) {
  66. toggleButton(target, mod);
  67. } else if (
  68. target.matches(".Details-content--closed, .Details-content--open")
  69. ) {
  70. toggleDetails(target.closest("details"), mod);
  71. }
  72. }, 100);
  73. }
  74. });
  75.  
  76. })();