GitHub Toggle Expanders

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

当前为 2016-09-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Toggle Expanders
  3. // @version 1.0.2
  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. // @namespace https://github.com/Mottie
  7. // @include https://github.com/*
  8. // @run-at document-idle
  9. // @author Rob Garrison
  10. // ==/UserScript==
  11. /* jshint esnext:true, unused:true */
  12. (() => {
  13. "use strict";
  14.  
  15. function toggle(el) {
  16. const state = closest(el, ".commits-list-item, .js-details-container")
  17. .classList.contains("open"),
  18. // target buttons inside commits_bucket - fixes #8
  19. selector = `.commits-listing .commits-list-item,
  20. #commits_bucket .js-details-container`;
  21. Array.from(document.querySelectorAll(selector)).forEach(el => {
  22. el.classList[state ? "add" : "remove"]("open");
  23. });
  24. }
  25.  
  26. function closest(el, selector) {
  27. while (el && el.nodeName !== "BODY" && !el.matches(selector)) {
  28. el = el.parentNode;
  29. }
  30. return el && el.matches(selector) ? el : [];
  31. }
  32.  
  33. document.body.addEventListener("click", event => {
  34. const target = event.target;
  35. if (
  36. target && event.getModifierState("Shift") &&
  37. target.matches(".ellipsis-expander")
  38. ) {
  39. // give GitHub time to add the class
  40. setTimeout(() => {
  41. toggle(target);
  42. }, 100);
  43. }
  44. });
  45.  
  46. })();