Skip to content

Commit

Permalink
utilize window.onurlchange, fallback to custom daemon if not available
Browse files Browse the repository at this point in the history
  • Loading branch information
GooseOb committed Dec 20, 2024
1 parent dfaa9e2 commit 86170d4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
1 change: 1 addition & 0 deletions header.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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==
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
24 changes: 14 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
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]);

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 });
Expand Down
11 changes: 11 additions & 0 deletions src/listeners/page-change.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
};
7 changes: 7 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ type DeepReadonlyObject<T> = {

type Props<T extends HTMLElement> = Partial<T> & object;
type ControlItem<T extends HTMLElement> = { item: HTMLDivElement; elem: T };
type UrlChangeEvent = { url: string };
interface Window {
onurlchange: (e: UrlChangeEvent) => void | null;
}
interface WindowEventMap {
urlchange: UrlChangeEvent;
}

0 comments on commit 86170d4

Please sign in to comment.