From fb11054d5346a84cac89c886b4cb006fdd5e3c13 Mon Sep 17 00:00:00 2001 From: GooseOb Date: Fri, 6 Dec 2024 22:06:14 +0100 Subject: [PATCH] move listeners to `listeners/` dir --- package.json | 2 +- src/index.ts | 41 ++-------------------------- src/listeners/index.ts | 3 ++ src/{ => listeners}/on-click.ts | 2 +- src/listeners/on-keyup.ts | 34 +++++++++++++++++++++++ src/{ => listeners}/on-video-page.ts | 12 ++++---- 6 files changed, 48 insertions(+), 46 deletions(-) create mode 100644 src/listeners/index.ts rename src/{ => listeners}/on-click.ts (93%) create mode 100644 src/listeners/on-keyup.ts rename src/{ => listeners}/on-video-page.ts (67%) diff --git a/package.json b/package.json index e52f60f..bd82e28 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "yt-defaulter", "author": "GooseOb", - "version": "1.11.0-alpha.15", + "version": "1.11.0-alpha.16", "repository": { "type": "git", "url": "git+https://github.com/GooseOb/YT-Defaulter.git" diff --git a/src/index.ts b/src/index.ts index d75fade..26a3b5d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,12 +1,7 @@ -import { restoreFocusAfter } from './utils'; +import * as config from './config'; import { text, translations } from './text'; import { style } from './style'; -import { valueSetters } from './player'; -import { computeSettings } from './compute-settings'; -import * as config from './config'; -import * as get from './element-getters'; -import { onVideoPage } from './on-video-page'; -import { onClick } from './on-click'; +import { onClick, onKeyup, onVideoPage } from './listeners'; Object.assign(text, translations[document.documentElement.lang]); @@ -25,36 +20,6 @@ setInterval(() => { }, 1_000); document.addEventListener('click', onClick, { capture: true }); -document.addEventListener( - 'keyup', - (e) => { - if (e.code === 'Enter') { - onClick(e); - } else if (e.ctrlKey && !e.shiftKey) { - if (config.value.flags.copySubs && e.code === 'KeyC') { - const plr = get.videoPlr(); - if (plr?.classList.contains('ytp-fullscreen')) { - const text = Array.from( - get.videoPlrCaptions(plr), - (line) => line.textContent - ).join(' '); - navigator.clipboard.writeText(text); - } - } else if (e.code === 'Space') { - e.stopPropagation(); - e.preventDefault(); - const settings = computeSettings(false); - if (settings.speed) { - restoreFocusAfter(() => { - valueSetters.speed(settings.speed); - }); - } else if (settings.customSpeed) { - valueSetters.customSpeed(settings.customSpeed); - } - } - } - }, - { capture: true } -); +document.addEventListener('keyup', onKeyup, { capture: true }); document.head.append(style); diff --git a/src/listeners/index.ts b/src/listeners/index.ts new file mode 100644 index 0000000..d81dd52 --- /dev/null +++ b/src/listeners/index.ts @@ -0,0 +1,3 @@ +export * from './on-click'; +export * from './on-keyup'; +export * from './on-video-page'; diff --git a/src/on-click.ts b/src/listeners/on-click.ts similarity index 93% rename from src/on-click.ts rename to src/listeners/on-click.ts index 05ea8aa..48c0931 100644 --- a/src/on-click.ts +++ b/src/listeners/on-click.ts @@ -1,4 +1,4 @@ -import { value } from './config'; +import { value } from '../config'; export const onClick = (e: Event) => { const { shortsToUsual, newTab } = value.flags; diff --git a/src/listeners/on-keyup.ts b/src/listeners/on-keyup.ts new file mode 100644 index 0000000..f8b5c36 --- /dev/null +++ b/src/listeners/on-keyup.ts @@ -0,0 +1,34 @@ +import { restoreFocusAfter } from '../utils'; +import { valueSetters } from '../player'; +import { computeSettings } from '../compute-settings'; +import * as config from '../config'; +import * as get from '../element-getters'; +import { onClick } from './on-click'; + +export const onKeyup = (e: KeyboardEvent) => { + if (e.code === 'Enter') { + onClick(e); + } else if (e.ctrlKey && !e.shiftKey) { + if (config.value.flags.copySubs && e.code === 'KeyC') { + const plr = get.videoPlr(); + if (plr?.classList.contains('ytp-fullscreen')) { + const text = Array.from( + get.videoPlrCaptions(plr), + (line) => line.textContent + ).join(' '); + navigator.clipboard.writeText(text); + } + } else if (e.code === 'Space') { + e.stopPropagation(); + e.preventDefault(); + const settings = computeSettings(false); + if (settings.speed) { + restoreFocusAfter(() => { + valueSetters.speed(settings.speed); + }); + } else if (settings.customSpeed) { + valueSetters.customSpeed(settings.customSpeed); + } + } + } +}; diff --git a/src/on-video-page.ts b/src/listeners/on-video-page.ts similarity index 67% rename from src/on-video-page.ts rename to src/listeners/on-video-page.ts index bb8479e..5e12af2 100644 --- a/src/on-video-page.ts +++ b/src/listeners/on-video-page.ts @@ -1,9 +1,9 @@ -import { untilAppear } from './utils'; -import { applySettings, plr } from './player'; -import { computeSettings } from './compute-settings'; -import * as get from './element-getters'; -import * as config from './config'; -import * as menu from './menu'; +import { untilAppear } from '../utils'; +import { applySettings, plr } from '../player'; +import { computeSettings } from '../compute-settings'; +import * as get from '../element-getters'; +import * as config from '../config'; +import * as menu from '../menu'; export const onVideoPage = async () => { const aboveTheFold = await untilAppear(get.aboveTheFold);