Browse Source

feat: getEvtData

Sv443 1 year ago
parent
commit
df63c469e5
3 changed files with 14 additions and 5 deletions
  1. 2 2
      src/constants.ts
  2. 2 2
      src/features/layout.ts
  3. 10 1
      src/utils.ts

+ 2 - 2
src/constants.ts

@@ -10,8 +10,8 @@ export const branch = "develop";
  */
 export const logLevel: LogLevel = 0;
 /** Specifies the hard limit for repetitive tasks */
-export const triesLimit = 25;
-/** Specifies the interval for repetitive tasks */
+export const triesLimit = 50;
+/** Specifies the interval in ms for repetitive tasks */
 export const triesInterval = 200;
 
 /** Info about the userscript, parsed from the userscript header (tools/post-build.js) */

+ 2 - 2
src/features/layout.ts

@@ -1,6 +1,6 @@
 import { scriptInfo, triesInterval, triesLimit } from "../constants";
 import { getFeatures } from "../config";
-import { addGlobalStyle, error, insertAfter, log, siteEvents } from "../utils";
+import { addGlobalStyle, error, getEvtData, insertAfter, log, siteEvents } from "../utils";
 import type { FeatureConfig } from "../types";
 import { openMenu } from "./menu/menu_old";
 import "./layout.css";
@@ -80,7 +80,7 @@ export function setVolSliderStep() {
 // TODO: account for the fact initially the elements might not exist, if the site was not opened directly with a video playing or via the /watch path
 export function initQueueButtons() {
   siteEvents.on("queueChanged", (evt) => {
-    for(const queueItm of ((evt.data as HTMLElement).childNodes as NodeListOf<HTMLElement>)) {
+    for(const queueItm of getEvtData<HTMLElement>(evt).childNodes as NodeListOf<HTMLElement>) {
       if(!queueItm.classList.contains("bytm-has-queue-btns"))
         addQueueButtons(queueItm);
     }

+ 10 - 1
src/utils.ts

@@ -1,4 +1,4 @@
-import { EventEmitter, EventHandler } from "@billjs/event-emitter";
+import { Event as EventParam, EventEmitter, EventHandler } from "@billjs/event-emitter";
 import type { Domain, LogLevel } from "./types";
 import { scriptInfo } from "./constants";
 
@@ -185,6 +185,15 @@ export interface SiteEvents extends EventEmitter {
 
 export const siteEvents = new EventEmitter() as SiteEvents;
 
+/**
+ * Returns the data of an event from the @billjs/event-emitter library
+ * @param evt Event object from the `.on()` or `.once()` method
+ * @template T Type of the data passed by `.fire(type: string, data: T)`
+ */
+export function getEvtData<T>(evt: EventParam): T {
+  return evt.data as T;
+}
+
 let observers: MutationObserver[] = [];
 
 /** Creates MutationObservers that check if parts of the site have changed, then emit an event on the `siteEvents` instance. */