siteEvents.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { createNanoEvents } from "nanoevents";
  2. import { error, info } from "./utils";
  3. import { FeatureConfig } from "./types";
  4. import { emitInterface } from "./interface";
  5. export interface SiteEventsMap {
  6. // misc:
  7. /** Emitted whenever the feature config is changed - initialization is not counted */
  8. configChanged: (config: FeatureConfig) => void;
  9. // TODO: implement
  10. /** Emitted whenever a config option is changed - contains the old and the new values */
  11. configOptionChanged: <TKey extends keyof FeatureConfig>(key: TKey, oldValue: FeatureConfig[TKey], newValue: FeatureConfig[TKey]) => void;
  12. /** Emitted whenever the config menu should be rebuilt, like when a config was imported */
  13. rebuildCfgMenu: (config: FeatureConfig) => void;
  14. /** Emitted whenever the config menu is closed */
  15. cfgMenuClosed: () => void;
  16. /** Emitted when the welcome menu is closed */
  17. welcomeMenuClosed: () => void;
  18. /** Emitted whenever the user interacts with a hotkey input, used so other keyboard input event listeners don't get called while mid-input */
  19. hotkeyInputActive: (active: boolean) => void;
  20. // DOM:
  21. /** Emitted whenever child nodes are added to or removed from the song queue */
  22. queueChanged: (queueElement: HTMLElement) => void;
  23. /** Emitted whenever child nodes are added to or removed from the autoplay queue underneath the song queue */
  24. autoplayQueueChanged: (queueElement: HTMLElement) => void;
  25. }
  26. /** EventEmitter instance that is used to detect changes to the site */
  27. export const siteEvents = createNanoEvents<SiteEventsMap>();
  28. let observers: MutationObserver[] = [];
  29. /** Disconnects and deletes all observers. Run `initSiteEvents()` again to create new ones. */
  30. export function removeAllObservers() {
  31. observers.forEach((observer, i) => {
  32. observer.disconnect();
  33. delete observers[i];
  34. });
  35. observers = [];
  36. }
  37. /** Creates MutationObservers that check if parts of the site have changed, then emit an event on the `siteEvents` instance. */
  38. export async function initSiteEvents() {
  39. try {
  40. //#SECTION queue
  41. // the queue container always exists so it doesn't need an extra init function
  42. const queueObs = new MutationObserver(([ { addedNodes, removedNodes, target } ]) => {
  43. if(addedNodes.length > 0 || removedNodes.length > 0) {
  44. info(`Detected queue change - added nodes: ${[...addedNodes.values()].length} - removed nodes: ${[...removedNodes.values()].length}`);
  45. emitSiteEvent("queueChanged", target as HTMLElement);
  46. }
  47. });
  48. // only observe added or removed elements
  49. queueObs.observe(document.querySelector("#side-panel #contents.ytmusic-player-queue")!, {
  50. childList: true,
  51. });
  52. const autoplayObs = new MutationObserver(([ { addedNodes, removedNodes, target } ]) => {
  53. if(addedNodes.length > 0 || removedNodes.length > 0) {
  54. info(`Detected autoplay queue change - added nodes: ${[...addedNodes.values()].length} - removed nodes: ${[...removedNodes.values()].length}`);
  55. emitSiteEvent("autoplayQueueChanged", target as HTMLElement);
  56. }
  57. });
  58. autoplayObs.observe(document.querySelector("#side-panel ytmusic-player-queue #automix-contents")!, {
  59. childList: true,
  60. });
  61. info("Successfully initialized SiteEvents observers");
  62. observers = observers.concat([
  63. queueObs,
  64. autoplayObs,
  65. ]);
  66. }
  67. catch(err) {
  68. error("Couldn't initialize SiteEvents observers due to an error:\n", err);
  69. }
  70. }
  71. /** Emits a site event with the given key and arguments */
  72. export function emitSiteEvent<TKey extends keyof SiteEventsMap>(key: TKey, ...args: Parameters<SiteEventsMap[TKey]>) {
  73. siteEvents.emit(key, ...args);
  74. emitInterface(`bytm:siteEvent:${key}`, args as unknown as undefined);
  75. }