mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-12 22:47:18 +03:00
Add better hotkey detection
This commit is contained in:
@@ -44,7 +44,6 @@ let video: HTMLVideoElement;
|
|||||||
let videoMutationObserver: MutationObserver = null;
|
let videoMutationObserver: MutationObserver = null;
|
||||||
// List of videos that have had event listeners added to them
|
// List of videos that have had event listeners added to them
|
||||||
const videosWithEventListeners: HTMLVideoElement[] = [];
|
const videosWithEventListeners: HTMLVideoElement[] = [];
|
||||||
const videoRootsWithEventListeners: HTMLDivElement[] = [];
|
|
||||||
|
|
||||||
let onInvidious;
|
let onInvidious;
|
||||||
let onMobileYouTube;
|
let onMobileYouTube;
|
||||||
@@ -77,6 +76,7 @@ const playerButtons: Record<string, {button: HTMLButtonElement, image: HTMLImage
|
|||||||
|
|
||||||
// Direct Links after the config is loaded
|
// Direct Links after the config is loaded
|
||||||
utils.wait(() => Config.config !== null, 1000, 1).then(() => videoIDChange(getYouTubeVideoID(document.URL)));
|
utils.wait(() => Config.config !== null, 1000, 1).then(() => videoIDChange(getYouTubeVideoID(document.URL)));
|
||||||
|
addHotkeyListener();
|
||||||
|
|
||||||
//the amount of times the sponsor lookup has retried
|
//the amount of times the sponsor lookup has retried
|
||||||
//this only happens if there is an error
|
//this only happens if there is an error
|
||||||
@@ -493,7 +493,6 @@ function refreshVideoAttachments() {
|
|||||||
if (newVideo && newVideo !== video) {
|
if (newVideo && newVideo !== video) {
|
||||||
video = newVideo;
|
video = newVideo;
|
||||||
|
|
||||||
addHotkeyListener();
|
|
||||||
if (!videosWithEventListeners.includes(video)) {
|
if (!videosWithEventListeners.includes(video)) {
|
||||||
videosWithEventListeners.push(video);
|
videosWithEventListeners.push(video);
|
||||||
|
|
||||||
@@ -1563,21 +1562,14 @@ function getSegmentsMessage(sponsorTimes: SponsorTime[]): string {
|
|||||||
return sponsorTimesMessage;
|
return sponsorTimesMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
function addHotkeyListener(): boolean {
|
function addHotkeyListener(): void {
|
||||||
let videoRoot = document.getElementById("movie_player") as HTMLDivElement;
|
document.addEventListener("keydown", hotkeyListener);
|
||||||
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 hotkeyListener(e: KeyboardEvent): void {
|
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 key = e.key;
|
||||||
|
|
||||||
const skipKey = Config.config.skipKeybind;
|
const skipKey = Config.config.skipKeybind;
|
||||||
|
|||||||
Reference in New Issue
Block a user