GitHub Toggle Expanders

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

目前為 2020-12-19 提交的版本,檢視 最新版本

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