GitHub Toggle Expanders

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

当前为 2017-05-14 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Toggle Expanders
  3. // @version 1.0.4
  4. // @description A userscript that toggles all expanders when one expander is shift-clicked
  5. // @license https://creativecommons.org/licenses/by-sa/4.0/
  6. // @author Rob Garrison
  7. // @namespace https://github.com/Mottie
  8. // @include https://github.com/*
  9. // @run-at document-idle
  10. // @icon https://github.com/fluidicon.png
  11. // ==/UserScript==
  12. (() => {
  13. "use strict";
  14.  
  15. function toggle(el) {
  16. const state = closest(".commits-list-item, .js-details-container", el)
  17. .classList.contains("open"),
  18. // target buttons inside commits_bucket - fixes #8
  19. selector = `.commits-listing .commits-list-item, #commits_bucket .js-details-container, .release-timeline-tags .js-details-container`;
  20. Array.from(document.querySelectorAll(selector)).forEach(el => {
  21. el.classList.toggle("open", state);
  22. });
  23. }
  24.  
  25. function closest(selector, el) {
  26. while (el && el.nodeType === 1) {
  27. if (el.matches(selector)) {
  28. return el;
  29. }
  30. el = el.parentNode;
  31. }
  32. return null;
  33. }
  34.  
  35. document.body.addEventListener("click", event => {
  36. const target = event.target;
  37. if (
  38. target && event.getModifierState("Shift") &&
  39. target.matches(".ellipsis-expander")
  40. ) {
  41. // give GitHub time to add the class
  42. setTimeout(() => {
  43. toggle(target);
  44. }, 100);
  45. }
  46. });
  47.  
  48. })();