A clean and minimal youtube frontend, without all the ads and whistles
JavaScript
refactor: import utils
Christian Fei270e692f887b
diff --git a/client/components/channels-list.js b/client/components/channels-list.jsindex bbed4f2..403e748 100644--- a/client/components/channels-list.js+++ b/client/components/channels-list.js@@ -1,3 +1,4 @@+import { addClickListener, removeClickListener } from "/lib/utils.js"class ChannelsList extends HTMLElement {constructor () {super()@@ -36,11 +37,11 @@ class ChannelsList extends HTMLElement {}registerEvents() {const channels = this.querySelectorAll('.channel')- channels.forEach(channel => utils.addClickListener(channel, this.channelClick.bind(this)))+ channels.forEach(channel => addClickListener(channel, this.channelClick.bind(this)))}unregisterEvents() {const channels = this.querySelectorAll('.channel')- channels.forEach(channel => utils.removeClickListener(channel, this.channelClick.bind(this)))+ channels.forEach(channel => 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 5cb16a1..fcf87bb 100644--- a/client/components/manage-channels-form.js+++ b/client/components/manage-channels-form.js@@ -1,3 +1,4 @@+import { addClickListener, removeClickListener } from "/lib/utils.js"class ManageChannelsForm extends HTMLElement {constructor () {super()@@ -23,11 +24,11 @@ class ManageChannelsForm extends HTMLElement {}registerEvents () {this.querySelectorAll('[data-channel]')- .forEach($channel => utils.addClickListener($channel, this.removeChannel.bind(this)))+ .forEach($channel => addClickListener($channel, this.removeChannel.bind(this)))}unregisterEvents () {this.querySelectorAll('[data-channel]')- .forEach($channel => utils.removeClickListener($channel, this.removeChannel.bind(this)))+ .forEach($channel => removeClickListener($channel, this.removeChannel.bind(this)))}render () {this.innerHTML = /*html*/`diff --git a/client/components/search-videos.js b/client/components/search-videos.jsindex c447363..7ac247e 100644--- a/client/components/search-videos.js+++ b/client/components/search-videos.js@@ -1,4 +1,4 @@-+import { createVideoElement } from "/lib/utils.js"class SearchVideos extends HTMLElement {constructor () {super()@@ -53,7 +53,7 @@ class SearchVideos extends HTMLElement {$videosContainer.innerHTML = ''const showOriginalThumbnail = store.get(store.showOriginalThumbnailKey)videos.forEach(video =>- $videosContainer.appendChild(utils.createVideoElement(video, showOriginalThumbnail))+ $videosContainer.appendChild(createVideoElement(video, showOriginalThumbnail)))if (!searchTerm) {$status.innerText = ''diff --git a/client/components/video-element.js b/client/components/video-element.jsindex 2f19121..7e54c25 100644--- a/client/components/video-element.js+++ b/client/components/video-element.js@@ -1,3 +1,4 @@+import { addClickListener, removeClickListener } from "/lib/utils.js"class VideoElement extends HTMLElement {constructor () {super()@@ -61,24 +62,24 @@ class VideoElement extends HTMLElement {this.registerEvents()}registerEvents () {- 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))+ addClickListener(this.querySelector('.action.download'), this.downloadVideoHandler.bind(this))+ addClickListener(this.querySelector('img.download'), this.downloadVideoHandler.bind(this))+ addClickListener(this.querySelector('.action.delete'), this.deleteVideoHandler.bind(this))+ addClickListener(this.querySelector('.action.summarize'), this.summarizeVideoHandler.bind(this))+ addClickListener(this.querySelector('.action.show-summary'), this.showSummaryHandler.bind(this))+ addClickListener(this.querySelector('.action.ignore'), this.toggleIgnoreVideoHandler.bind(this))+ addClickListener(this.querySelector('.channel-name'), this.filterByChannelHandler.bind(this))+ addClickListener(this.querySelector('.play.video-placeholder'), this.watchVideoHandler.bind(this))}unregisterEvents () {- 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))+ removeClickListener(this.querySelector('.action.download'), this.downloadVideoHandler.bind(this))+ removeClickListener(this.querySelector('img.download'), this.downloadVideoHandler.bind(this))+ removeClickListener(this.querySelector('.action.delete'), this.deleteVideoHandler.bind(this))+ removeClickListener(this.querySelector('.action.summarize'), this.summarizeVideoHandler.bind(this))+ removeClickListener(this.querySelector('.action.show-summary'), this.showSummaryHandler.bind(this))+ removeClickListener(this.querySelector('.action.ignore'), this.toggleIgnoreVideoHandler.bind(this))+ removeClickListener(this.querySelector('.channel-name'), this.filterByChannelHandler.bind(this))+ removeClickListener(this.querySelector('.play.video-placeholder'), this.watchVideoHandler.bind(this))this.querySelector('video') && this.unregisterVideoEvents(this.querySelector('video'))}watchVideoHandler (event) {diff --git a/client/index.html b/client/index.htmlindex 3654124..2a7265c 100644--- a/client/index.html+++ b/client/index.html@@ -88,7 +88,6 @@</div></template>- <script type="module" src="/lib/utils.js"></script><script type="module" src="/components/empty-state.js"></script><script type="module" src="/components/search-videos.js"></script><script type="module" src="/components/channels-list.js"></script>diff --git a/client/lib/router.js b/client/lib/router.jsindex 8456642..2fbe7af 100644--- a/client/lib/router.js+++ b/client/lib/router.js@@ -1,3 +1,5 @@+import { createVideoElement, applyShowBigPlayer, applyShowThumbnails } from "/lib/utils.js"+const routes = {'/': { template: document.getElementById('main-template'), async initialize() {document.getElementById('home-link').classList.add('hide')@@ -20,13 +22,13 @@ const routes = {videos = videos.sort((a, b) => +new Date(b.publishedAt) - +new Date(a.publishedAt))- videos.forEach(video => $videosContainer.appendChild(utils.createVideoElement(video, showOriginalThumbnail)))+ videos.forEach(video => $videosContainer.appendChild(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))- utils.applyShowThumbnails(store.get(store.showThumbnailsKey))- utils.applyShowBigPlayer(store.get(store.showBigPlayerKey))+ applyShowThumbnails(store.get(store.showThumbnailsKey))+ applyShowBigPlayer(store.get(store.showBigPlayerKey))} },'/settings': { template: document.getElementById('settings-template'), async initialize () {document.getElementById('home-link').classList.remove('hide')@@ -39,7 +41,7 @@ const routes = {$showThumbnails.addEventListener('click', (event) => {store.toggle(store.showThumbnailsKey)- utils.applyShowThumbnails(store.get(store.showThumbnailsKey))+ applyShowThumbnails(store.get(store.showThumbnailsKey))})const $showBigPlayer = document.getElementById('show-big-player')@@ -47,7 +49,7 @@ const routes = {$showBigPlayer.addEventListener('click', (event) => {store.toggle(store.showBigPlayerKey)- utils.applyShowBigPlayer(store.get(store.showBigPlayerKey))+ applyShowBigPlayer(store.get(store.showBigPlayerKey))})const $showOriginalThumbnail = document.getElementById('show-original-thumbnail')diff --git a/client/lib/utils.js b/client/lib/utils.jsindex 83863d7..4fcce36 100644--- a/client/lib/utils.js+++ b/client/lib/utils.js@@ -1,43 +1,40 @@-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')- } else {- document.body.classList.add('hide-thumbnails')- }- const $showThumbnailsCheckbox = document.getElementById('show-thumbnails')- if ($showThumbnailsCheckbox) $showThumbnailsCheckbox.checked = showThumbnails- },-- applyShowBigPlayer(showBigPlayer) {- if (showBigPlayer) {- document.body.classList.add('show-big-player')- } else {- document.body.classList.remove('show-big-player')- }- const $showBigPlayerCheckbox = document.getElementById('show-big-player')- if ($showBigPlayerCheckbox) $showBigPlayerCheckbox.checked = showBigPlayer- },+export 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+}- addClickListener ($el, handler) {- if (!$el) return- $el.addEventListener('click', handler)- $el.addEventListener('keydown', (event) => event.key === 'Enter' && handler(event))- },+export function applyShowThumbnails(showThumbnails) {+ if (showThumbnails) {+ document.body.classList.remove('hide-thumbnails')+ } else {+ document.body.classList.add('hide-thumbnails')+ }+ const $showThumbnailsCheckbox = document.getElementById('show-thumbnails')+ if ($showThumbnailsCheckbox) $showThumbnailsCheckbox.checked = showThumbnails+}- removeClickListener ($el, handler) {- if (!$el) return- $el.removeEventListener('click', handler)- $el.removeEventListener('keydown', (event) => event.key === 'Enter' && handler(event))+export function applyShowBigPlayer(showBigPlayer) {+ if (showBigPlayer) {+ document.body.classList.add('show-big-player')+ } else {+ document.body.classList.remove('show-big-player')}+ const $showBigPlayerCheckbox = document.getElementById('show-big-player')+ if ($showBigPlayerCheckbox) $showBigPlayerCheckbox.checked = showBigPlayer+}++export function addClickListener ($el, handler) {+ if (!$el) return+ $el.addEventListener('click', handler)+ $el.addEventListener('keydown', (event) => event.key === 'Enter' && handler(event))}+export function removeClickListener ($el, handler) {+ if (!$el) return+ $el.removeEventListener('click', handler)+ $el.removeEventListener('keydown', (event) => event.key === 'Enter' && handler(event))+}diff --git a/client/main.js b/client/main.jsindex 2cf32da..d148a93 100644--- a/client/main.js+++ b/client/main.js@@ -1,4 +1,5 @@import Store from '/lib/store.js'+import { createVideoElement } from '/lib/utils.js'const store = new Store()window.store = store@@ -31,8 +32,8 @@ eventSource.onmessage = (message) => {data.videos.forEach(video => {const $videoElement = $videosContainer.querySelector('video-element')- if (!$videoElement) return $videosContainer.appendChild(utils.createVideoElement(video, showOriginalThumbnail))- $videoElement.parentNode.insertBefore(utils.createVideoElement(video, showOriginalThumbnail), $videoElement.nextSibling)+ if (!$videoElement) return $videosContainer.appendChild(createVideoElement(video, showOriginalThumbnail))+ $videoElement.parentNode.insertBefore(createVideoElement(video, showOriginalThumbnail), $videoElement.nextSibling)})return}