GitHub Toggle Expanders

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

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

  1. // ==UserScript==
  2. // @name GitHub Toggle Expanders
  3. // @version 1.1.2
  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, ctrlKeyPressed) {
  16. const stateNode = closest(".js-details-container", el),
  17. state = stateNode.classList.contains("open"),
  18. parentNode = closest(ctrlKeyPressed ?
  19. ".container, .js-discussion" :
  20. ".commits-listing, .discussion-item-body, .release-timeline-tags",
  21. stateNode
  22. ),
  23. containerNodes = parentNode.querySelectorAll(".js-details-container");
  24.  
  25. Array.from(containerNodes).forEach(node => {
  26. node.classList.toggle("open", state);
  27. });
  28. }
  29.  
  30. function closest(selector, el) {
  31. while (el && el.nodeType === 1) {
  32. if (el.matches(selector)) {
  33. return el;
  34. }
  35. el = el.parentNode;
  36. }
  37. return null;
  38. }
  39.  
  40. document.body.addEventListener("click", event => {
  41. const target = event.target;
  42. if (
  43. target && event.getModifierState("Shift") &&
  44. target.matches(".js-details-target")
  45. ) {
  46. // give GitHub time to add the class
  47. setTimeout(() => {
  48. toggle(target, event.ctrlKey || event.metaKey);
  49. }, 100);
  50. }
  51. });
  52.  
  53. })();