A clean and minimal youtube frontend, without all the ads and whistles
JavaScript
chore: remove window references
Christian Feib3df6b8509bb
diff --git a/client/components/channels-list.js b/client/components/channels-list.jsindex 79889ee..f4f1343 100644--- a/client/components/channels-list.js+++ b/client/components/channels-list.js@@ -36,11 +36,11 @@ class ChannelsList extends HTMLElement {}registerEvents() {const channels = this.querySelectorAll('.channel')- channels.forEach(channel => window.utils.addClickListener(channel, this.channelClick.bind(this)))+ channels.forEach(channel => utils.addClickListener(channel, this.channelClick.bind(this)))}unregisterEvents() {const channels = this.querySelectorAll('.channel')- channels.forEach(channel => window.utils.removeClickListener(channel, this.channelClick.bind(this)))+ channels.forEach(channel => utils.removeClickListener(channel, this.channelClick.bind(this)))}channelClick(event) {const $searchInput = document.querySelector('#search')diff --git a/client/components/manage-channels-form.js b/client/components/manage-channels-form.jsindex f1ff8d5..5cb16a1 100644--- a/client/components/manage-channels-form.js+++ b/client/components/manage-channels-form.js@@ -23,11 +23,11 @@ class ManageChannelsForm extends HTMLElement {}registerEvents () {this.querySelectorAll('[data-channel]')- .forEach($channel => window.utils.addClickListener($channel, this.removeChannel.bind(this)))+ .forEach($channel => utils.addClickListener($channel, this.removeChannel.bind(this)))}unregisterEvents () {this.querySelectorAll('[data-channel]')- .forEach($channel => window.utils.removeClickListener($channel, this.removeChannel.bind(this)))+ .forEach($channel => utils.removeClickListener($channel, this.removeChannel.bind(this)))}render () {this.innerHTML = /*html*/`diff --git a/client/components/manage-disk-space-form.js b/client/components/manage-disk-space-form.jsindex a2e33b0..5bbd404 100644--- a/client/components/manage-disk-space-form.js+++ b/client/components/manage-disk-space-form.js@@ -41,7 +41,7 @@ class ManageDiskSpaceForm extends HTMLElement {updateDiskSpaceUsed() {const onlyIgnored = this.querySelector('#delete-only-ignored').checkedconst $diskSpaceUsed = this.querySelector('#disk-space-used')- window.fetch(onlyIgnored ? '/api/disk-usage?onlyIgnored=true' : '/api/disk-usage')+ fetch(onlyIgnored ? '/api/disk-usage?onlyIgnored=true' : '/api/disk-usage').then(response => response.text()).then((diskSpaceUsed) => {$diskSpaceUsed.innerText = diskSpaceUsed@@ -54,7 +54,7 @@ class ManageDiskSpaceForm extends HTMLElement {if (!confirm('About to delete downloaded videos, are you sure?')) returnconst onlyIgnored = this.querySelector('#delete-only-ignored').checkedconst $diskUsage = this.querySelector('#disk-usage')- window.fetch('/api/reclaim-disk-space', {+ fetch('/api/reclaim-disk-space', {method: 'POST',body: JSON.stringify({ onlyIgnored })})diff --git a/client/components/search-videos.js b/client/components/search-videos.jsindex de8c5fc..4955a52 100644--- a/client/components/search-videos.js+++ b/client/components/search-videos.js@@ -52,9 +52,9 @@ class SearchVideos extends HTMLElement {const $videosContainer = document.querySelector('.main-videos-container')if (!$videosContainer) return$videosContainer.innerHTML = ''- const showOriginalThumbnail = window.store.get(store.showOriginalThumbnailKey)+ const showOriginalThumbnail = store.get(store.showOriginalThumbnailKey)videos.forEach(video =>- $videosContainer.appendChild(window.createVideoElement(video, showOriginalThumbnail))+ $videosContainer.appendChild(utils.createVideoElement(video, showOriginalThumbnail)))if (!searchTerm) {$status.innerText = ''diff --git a/client/components/video-element.js b/client/components/video-element.jsindex 43dfcd0..1846bf8 100644--- a/client/components/video-element.js+++ b/client/components/video-element.js@@ -69,24 +69,24 @@ class VideoElement extends HTMLElement {this.registerEvents()}registerEvents () {- window.utils.addClickListener(this.querySelector('.action.download'), this.downloadVideoHandler.bind(this))- window.utils.addClickListener(this.querySelector('img.download'), this.downloadVideoHandler.bind(this))- window.utils.addClickListener(this.querySelector('.action.delete'), this.deleteVideoHandler.bind(this))- window.utils.addClickListener(this.querySelector('.action.summarize'), this.summarizeVideoHandler.bind(this))- window.utils.addClickListener(this.querySelector('.action.show-summary'), this.showSummaryHandler.bind(this))- window.utils.addClickListener(this.querySelector('.action.ignore'), this.toggleIgnoreVideoHandler.bind(this))- window.utils.addClickListener(this.querySelector('.channel-name'), this.filterByChannelHandler.bind(this))- window.utils.addClickListener(this.querySelector('.play.video-placeholder'), this.watchVideoHandler.bind(this))+ utils.addClickListener(this.querySelector('.action.download'), this.downloadVideoHandler.bind(this))+ utils.addClickListener(this.querySelector('img.download'), this.downloadVideoHandler.bind(this))+ utils.addClickListener(this.querySelector('.action.delete'), this.deleteVideoHandler.bind(this))+ utils.addClickListener(this.querySelector('.action.summarize'), this.summarizeVideoHandler.bind(this))+ utils.addClickListener(this.querySelector('.action.show-summary'), this.showSummaryHandler.bind(this))+ utils.addClickListener(this.querySelector('.action.ignore'), this.toggleIgnoreVideoHandler.bind(this))+ utils.addClickListener(this.querySelector('.channel-name'), this.filterByChannelHandler.bind(this))+ utils.addClickListener(this.querySelector('.play.video-placeholder'), this.watchVideoHandler.bind(this))}unregisterEvents () {- window.utils.removeClickListener(this.querySelector('.action.download'), this.downloadVideoHandler.bind(this))- window.utils.removeClickListener(this.querySelector('img.download'), this.downloadVideoHandler.bind(this))- window.utils.removeClickListener(this.querySelector('.action.delete'), this.deleteVideoHandler.bind(this))- window.utils.removeClickListener(this.querySelector('.action.summarize'), this.summarizeVideoHandler.bind(this))- window.utils.removeClickListener(this.querySelector('.action.show-summary'), this.showSummaryHandler.bind(this))- window.utils.removeClickListener(this.querySelector('.action.ignore'), this.toggleIgnoreVideoHandler.bind(this))- window.utils.removeClickListener(this.querySelector('.channel-name'), this.filterByChannelHandler.bind(this))- window.utils.removeClickListener(this.querySelector('.play.video-placeholder'), this.watchVideoHandler.bind(this))+ utils.removeClickListener(this.querySelector('.action.download'), this.downloadVideoHandler.bind(this))+ utils.removeClickListener(this.querySelector('img.download'), this.downloadVideoHandler.bind(this))+ utils.removeClickListener(this.querySelector('.action.delete'), this.deleteVideoHandler.bind(this))+ utils.removeClickListener(this.querySelector('.action.summarize'), this.summarizeVideoHandler.bind(this))+ utils.removeClickListener(this.querySelector('.action.show-summary'), this.showSummaryHandler.bind(this))+ utils.removeClickListener(this.querySelector('.action.ignore'), this.toggleIgnoreVideoHandler.bind(this))+ utils.removeClickListener(this.querySelector('.channel-name'), this.filterByChannelHandler.bind(this))+ utils.removeClickListener(this.querySelector('.play.video-placeholder'), this.watchVideoHandler.bind(this))this.querySelector('video') && this.unregisterVideoEvents(this.querySelector('video'))}watchVideoHandler (event) {@@ -214,7 +214,7 @@ class VideoElement extends HTMLElement {scrollIntoViewWithOffset (offset, behavior = 'smooth') {const top = this.getBoundingClientRect().top - offset - document.body.getBoundingClientRect().top- window.scrollTo({ top, behavior })+ scrollTo({ top, behavior })}}diff --git a/client/components/video-quality-form.js b/client/components/video-quality-form.jsindex 5398f20..177f739 100644--- a/client/components/video-quality-form.js+++ b/client/components/video-quality-form.js@@ -6,7 +6,7 @@ class VideoQualityForm extends HTMLElement {this.render()this.registerEvents()- window.fetch('/api/video-quality', {+ fetch('/api/video-quality', {method: 'GET'}).then(response => response.json()).then((videoQuality) => {@@ -47,7 +47,7 @@ class VideoQualityForm extends HTMLElement {setVideoQualityHandler (event) {event.preventDefault()console.log('change video quality', event.target.value)- window.fetch('/api/video-quality', {+ fetch('/api/video-quality', {method: 'POST',headers: {'Content-Type': 'application/json'diff --git a/client/lib/router.js b/client/lib/router.jsindex 8d4bae6..541ccf7 100644--- a/client/lib/router.js+++ b/client/lib/router.js@@ -22,13 +22,13 @@ const routes = {videos = videos.sort((a, b) => +new Date(b.publishedAt) - +new Date(a.publishedAt))- videos.forEach(video => $videosContainer.appendChild(createVideoElement(video, showOriginalThumbnail)))+ videos.forEach(video => $videosContainer.appendChild(utils.createVideoElement(video, showOriginalThumbnail)))const channels = await fetch('/api/channels').then(res => res.json())document.querySelector('channels-list').dataset['list'] = JSON.stringify(channels.map(c => c.name).filter(Boolean))- window.utils.applyShowThumbnails(store.get(store.showThumbnailsKey))- window.utils.applyShowBigPlayer(store.get(store.showBigPlayerKey))+ utils.applyShowThumbnails(store.get(store.showThumbnailsKey))+ utils.applyShowBigPlayer(store.get(store.showBigPlayerKey))} },'/settings': { template: document.getElementById('settings-template'), async initialize () {console.log('initalize /settings')@@ -43,7 +43,7 @@ const routes = {$showThumbnails.addEventListener('click', (event) => {store.toggle(store.showThumbnailsKey)- window.utils.applyShowThumbnails(store.get(store.showThumbnailsKey))+ utils.applyShowThumbnails(store.get(store.showThumbnailsKey))})const $showBigPlayer = document.getElementById('show-big-player')@@ -51,7 +51,7 @@ const routes = {$showBigPlayer.addEventListener('click', (event) => {store.toggle(store.showBigPlayerKey)- window.utils.applyShowBigPlayer(store.get(store.showBigPlayerKey))+ utils.applyShowBigPlayer(store.get(store.showBigPlayerKey))})const $showOriginalThumbnail = document.getElementById('show-original-thumbnail')@@ -68,23 +68,23 @@ const routes = {}handleRoute()-window.addEventListener('popstate', (event) => {- console.log('popstate', window.location.pathname)+addEventListener('popstate', (event) => {+ console.log('popstate', location.pathname)handleRoute()})document.querySelectorAll('[href="/"],[href="/settings"]').forEach(($el) => {$el.addEventListener('click', (event) => {event.preventDefault()- const path = new URL($el.href, window.location.origin).pathname+ const path = new URL($el.href, location.origin).pathnameconsole.log('navigating ->', path)- window.history.pushState({}, '', path)+ history.pushState({}, '', path)var popStateEvent = new PopStateEvent('popstate', {})dispatchEvent(popStateEvent)})})-function handleRoute(route = window.location.pathname) {+function handleRoute(route = location.pathname) {console.log('handling route', route)if (routes[route]) {document.querySelector('main').replaceChildren(routes[route].template.content.cloneNode(true))diff --git a/client/lib/utils.js b/client/lib/utils.jsindex 22b0aa5..83863d7 100644--- a/client/lib/utils.js+++ b/client/lib/utils.js@@ -1,4 +1,13 @@window.utils = {+ createVideoElement (video, showOriginalThumbnail = false) {+ const $video = document.createElement('video-element')+ $video.dataset['data'] = JSON.stringify(Object.assign(video, showOriginalThumbnail ? {+ thumbnail: video.thumbnail.replace('mq2.jpg', 'mqdefault.jpg')+ } : {}))+ $video.dataset['videoId'] = video.id+ return $video+ },+applyShowThumbnails(showThumbnails) {if (showThumbnails) {document.body.classList.remove('hide-thumbnails')diff --git a/client/main.js b/client/main.jsindex 355d0bb..2cf32da 100644--- a/client/main.js+++ b/client/main.js@@ -1,11 +1,9 @@import Store from '/lib/store.js'const store = new Store()window.store = store-window.createVideoElement = createVideoElement-// app: sse updates and renders-const eventSource = new window.EventSource('/')+const eventSource = new EventSource('/')let $videosContainer = document.querySelector('.main-videos-container')eventSource.onmessage = (message) => {@@ -33,8 +31,8 @@ eventSource.onmessage = (message) => {data.videos.forEach(video => {const $videoElement = $videosContainer.querySelector('video-element')- if (!$videoElement) return $videosContainer.appendChild(createVideoElement(video, showOriginalThumbnail))- $videoElement.parentNode.insertBefore(createVideoElement(video, showOriginalThumbnail), $videoElement.nextSibling)+ if (!$videoElement) return $videosContainer.appendChild(utils.createVideoElement(video, showOriginalThumbnail))+ $videoElement.parentNode.insertBefore(utils.createVideoElement(video, showOriginalThumbnail), $videoElement.nextSibling)})return}@@ -96,13 +94,4 @@ function observeDialogOpenPreventScroll (dialog) {}}}).observe(dialog, { attributes: true, childList: true, subtree: true })-}--function createVideoElement (video, showOriginalThumbnail = false) {- const $video = document.createElement('video-element')- $video.dataset['data'] = JSON.stringify(Object.assign(video, showOriginalThumbnail ? {- thumbnail: video.thumbnail.replace('mq2.jpg', 'mqdefault.jpg')- } : {}))- $video.dataset['videoId'] = video.id- return $video-}+}\ No newline at end of file