GitHub Toggle Expanders

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

当前为 2018-08-15 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Toggle Expanders
  3. // @version 1.1.3
  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://assets-cdn.github.com/pinned-octocat.svg
  11. // ==/UserScript==
  12. (() => {
  13. "use strict";
  14.  
  15. function toggle(el, modKey) {
  16. const stateNode = el.closest(".js-details-container, details");
  17. const state = stateNode.nodeName === "DETAILS" ?
  18. stateNode.open :
  19. stateNode.classList.contains("open");
  20. const parentNode = stateNode.closest(modKey ?
  21. ".container, .js-discussion" :
  22. ".commit-group, .js-timeline-item"
  23. );
  24. const containers = parentNode.querySelectorAll(
  25. ".js-details-container, .outdated-comment"
  26. );
  27.  
  28. [...containers].forEach(node => {
  29. if (node.nodeName === "DETAILS") {
  30. node.open = state;
  31. } else {
  32. node.classList.toggle("open", state);
  33. }
  34. });
  35. }
  36.  
  37. document.body.addEventListener("click", event => {
  38. const target = event.target;
  39. const mod = event.ctrlKey || event.metaKey;
  40. if (target && event.getModifierState("Shift")) {
  41. // give GitHub time to update the elements
  42. setTimeout(() => {
  43. if (target.matches(".js-details-target")) {
  44. toggle(target, mod);
  45. } else if (target.matches(".btn-link, .js-toggle-outdated-comments")) {
  46. toggle(target.closest("details"), mod);
  47. }
  48. }, 100);
  49. }
  50. });
  51.  
  52. })();