GitHub Toggle Expanders

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

目前為 2017-01-12 提交的版本,檢視 最新版本

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