GitHub Toggle Expanders

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

目前為 2016-09-18 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name GitHub Toggle Expanders
  3. // @version 1.0.0
  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 selector = ".commits-list-item, .js-details-container",
  17. state = closest(el, selector).classList.contains("open");
  18. Array.from(document.querySelectorAll(selector)).forEach(el => {
  19. el.classList[state ? "add" : "remove"]("open");
  20. });
  21. }
  22.  
  23. function closest(el, selector) {
  24. while (el && el.nodeName !== "BODY" && !el.matches(selector)) {
  25. el = el.parentNode;
  26. }
  27. return el && el.matches(selector) ? el : [];
  28. }
  29.  
  30. document.body.addEventListener("click", event => {
  31. const target = event.target;
  32. if (
  33. target && event.getModifierState("Shift") &&
  34. target.matches(".ellipsis-expander")
  35. ) {
  36. // give GitHub time to add the class
  37. setTimeout(() => {
  38. toggle(target);
  39. }, 100);
  40. }
  41. });
  42.  
  43. })();