From b0400f98bec31ec9dddb420495c86df73e301e1b Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Fri, 11 Jun 2021 00:50:38 -0400 Subject: [PATCH] Add better hotkey detection --- src/content.ts | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/content.ts b/src/content.ts index c1c334ed..13843746 100644 --- a/src/content.ts +++ b/src/content.ts @@ -44,7 +44,6 @@ let video: HTMLVideoElement; let videoMutationObserver: MutationObserver = null; // List of videos that have had event listeners added to them const videosWithEventListeners: HTMLVideoElement[] = []; -const videoRootsWithEventListeners: HTMLDivElement[] = []; let onInvidious; let onMobileYouTube; @@ -77,6 +76,7 @@ const playerButtons: Record Config.config !== null, 1000, 1).then(() => videoIDChange(getYouTubeVideoID(document.URL))); +addHotkeyListener(); //the amount of times the sponsor lookup has retried //this only happens if there is an error @@ -493,7 +493,6 @@ function refreshVideoAttachments() { if (newVideo && newVideo !== video) { video = newVideo; - addHotkeyListener(); if (!videosWithEventListeners.includes(video)) { videosWithEventListeners.push(video); @@ -1563,21 +1562,14 @@ function getSegmentsMessage(sponsorTimes: SponsorTime[]): string { return sponsorTimesMessage; } -function addHotkeyListener(): boolean { - let videoRoot = document.getElementById("movie_player") as HTMLDivElement; - if (onInvidious) videoRoot = (document.getElementById("player-container") ?? document.getElementById("player")) as HTMLDivElement; - if (video.baseURI.startsWith("https://www.youtube.com/tv#/")) videoRoot = document.querySelector("ytlr-watch-page") as HTMLDivElement; - - if (videoRoot && !videoRootsWithEventListeners.includes(videoRoot)) { - videoRoot.addEventListener("keydown", hotkeyListener); - videoRootsWithEventListeners.push(videoRoot); - return true; - } - - return false; +function addHotkeyListener(): void { + document.addEventListener("keydown", hotkeyListener); } function hotkeyListener(e: KeyboardEvent): void { + if (["textarea", "input"].includes(document.activeElement?.tagName?.toLowerCase()) + || document.activeElement?.id?.toLowerCase()?.includes("editable")) return; + const key = e.key; const skipKey = Config.config.skipKeybind;