-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
105 lines (94 loc) · 2.88 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* @type {HTMLInputElement}
*/
const input = document.getElementById('url-input');
/**
* @type {NodeListOf<HTMLImageElement>} Nodelist of images
*/
const images = document.querySelectorAll('.thumbnail');
/**
* @type {HTMLButtonElement} button
*/
const button = document.getElementById('input-btn');
/**
* @type {HTMLLinkElement} favicon
*/
const link = document.querySelector("link[rel~='icon']");
/**
* @type {HTMLElement}
*/
const header = document.querySelector("header");
let prevScrollPos = window.scrollY;
input.addEventListener('focus', () => { input.select() });
input.addEventListener('input', processThumbnail);
button.addEventListener('click', processThumbnail);
window.addEventListener('scroll', handleScroll);
images.forEach(image => image.addEventListener('load', e => {
/**
* @type {HTMLImageElement}
*/
let thumb = e.target;
if (thumb.naturalWidth == 120 && thumb.naturalHeight == 90) {
thumb.parentElement.classList.add('hidden');
} else {
thumb.parentElement.classList.remove('hidden');
}
}));
let id = document.URL.split('#')[1];
if (id?.length == 11) {
input.value = `https://www.youtube.com/watch?v=${id}`;
}
processThumbnail();
if (!link) {
link = document.createElement('link');
link.rel = 'icon';
document.head.appendChild(link);
}
if (matchMedia && matchMedia('(prefers-color-scheme: dark)').matches) {
link.href = "./favicon-dark.png";
}
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => {
if (e.matches) {
link.href = "./favicon-dark.png";
} else {
link.href = "./favicon-light.png";
}
});
function processThumbnail() {
let url = input.value.trim();
let id = parseID(url);
if (id === undefined) return;
let thumbURLS = [
`https://i.ytimg.com/vi_webp/${id}/maxresdefault.webp`,
`https://i.ytimg.com/vi_webp/${id}/sddefault.webp`,
`https://i.ytimg.com/vi_webp/${id}/hqdefault.webp`,
`https://i.ytimg.com/vi_webp/${id}/mqdefault.webp`,
];
for (let i = 0; i < images.length; i++) {
images[i].src = thumbURLS[i];
}
window.location.assign(`#${id}`)
}
/**
* Parses video ID from YouTube URL
* @param {string} url YouTube video URL
* @return {string | undefined} YouTube video ID
*/
function parseID(url) {
let matches = url.match(/[/=]([0-9a-zA-Z-_]{11})[?&]?/);
if (matches && matches.length) {
return matches[1];
}
return;
}
function handleScroll() {
const currentScrollPos = window.scrollY;
if (currentScrollPos > prevScrollPos && currentScrollPos > header.offsetHeight) {
// Scrolling down
header.classList.add('retracted');
} else {
// Scrolling up
header.classList.remove('retracted');
}
prevScrollPos = currentScrollPos;
}