From 86170d42c80391af300d43bd53a4e9faaa8694d3 Mon Sep 17 00:00:00 2001 From: GooseOb Date: Fri, 20 Dec 2024 20:12:14 +0100 Subject: [PATCH] utilize `window.onurlchange`, fallback to custom daemon if not available --- header.txt | 1 + package.json | 2 +- src/index.ts | 24 ++++++++++++++---------- src/listeners/page-change.ts | 11 +++++++++++ src/types.d.ts | 7 +++++++ 5 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 src/listeners/page-change.ts diff --git a/header.txt b/header.txt index 40615f0..93082c7 100644 --- a/header.txt +++ b/header.txt @@ -5,6 +5,7 @@ // @description Set speed, quality, subtitles and volume as default globally or specialize for each channel // @author GooseOb // @license MIT +// @grant window.onurlchange // @match https://www.youtube.com/* // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com // ==/UserScript== diff --git a/package.json b/package.json index b3c9305..9995be6 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "yt-defaulter", "author": "GooseOb", - "version": "1.11.11", + "version": "1.11.12", "repository": { "type": "git", "url": "git+https://github.com/GooseOb/YT-Defaulter.git" diff --git a/src/index.ts b/src/index.ts index 26a3b5d..a1cfac0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,8 @@ import * as config from './config'; import { text, translations } from './text'; import { style } from './style'; -import { onClick, onKeyup, onVideoPage } from './listeners'; +import { onClick, onKeyup } from './listeners'; +import { onPageChange } from './listeners/page-change'; Object.assign(text, translations[document.documentElement.lang]); @@ -9,15 +10,18 @@ if (config.update(config.value)) { config.saveLS(config.value); } -let lastHref: string; -setInterval(() => { - if (lastHref !== location.href) { - lastHref = location.href; - if (location.pathname === '/watch') { - setTimeout(onVideoPage, 1_000); - } - } -}, 1_000); +const updatePage = () => { + onPageChange(location.href); +}; + +if (window.onurlchange === null) { + window.addEventListener('urlchange', ({ url }) => { + onPageChange(url); + }); + updatePage(); +} else { + setInterval(updatePage, 1_000); +} document.addEventListener('click', onClick, { capture: true }); document.addEventListener('keyup', onKeyup, { capture: true }); diff --git a/src/listeners/page-change.ts b/src/listeners/page-change.ts new file mode 100644 index 0000000..9d5120a --- /dev/null +++ b/src/listeners/page-change.ts @@ -0,0 +1,11 @@ +import { onVideoPage } from './video-page'; + +let lastUrl: string; +export const onPageChange = (url: string) => { + if (lastUrl !== url) { + lastUrl = url; + if (location.pathname === '/watch') { + setTimeout(onVideoPage, 1_000); + } + } +}; diff --git a/src/types.d.ts b/src/types.d.ts index a74fcd4..a634a1e 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -12,3 +12,10 @@ type DeepReadonlyObject = { type Props = Partial & object; type ControlItem = { item: HTMLDivElement; elem: T }; +type UrlChangeEvent = { url: string }; +interface Window { + onurlchange: (e: UrlChangeEvent) => void | null; +} +interface WindowEventMap { + urlchange: UrlChangeEvent; +}