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.5
  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. selectors = `
  20. .commits-listing .commits-list-item,
  21. #commits_bucket .js-details-container,
  22. .release-timeline-tags .js-details-container`;
  23. Array.from(document.querySelectorAll(selectors)).forEach(el => {
  24. el.classList.toggle("open", state);
  25. });
  26. }
  27.  
  28. function closest(selector, el) {
  29. while (el && el.nodeType === 1) {
  30. if (el.matches(selector)) {
  31. return el;
  32. }
  33. el = el.parentNode;
  34. }
  35. return null;
  36. }
  37.  
  38. document.body.addEventListener("click", event => {
  39. const target = event.target;
  40. if (
  41. target && event.getModifierState("Shift") &&
  42. target.matches(".ellipsis-expander")
  43. ) {
  44. // give GitHub time to add the class
  45. setTimeout(() => {
  46. toggle(target);
  47. }, 100);
  48. }
  49. });
  50.  
  51. })();