A clean and minimal youtube frontend, without all the ads and whistles
JavaScript
extract modules
Christian Feifcb8a5a0c846
diff --git a/client/components/add-channel-form.js b/client/components/add-channel-form.jsnew file mode 100644index 0000000..97dd8f0--- /dev/null+++ b/client/components/add-channel-form.js@@ -0,0 +1,63 @@+class AddChannelForm extends HTMLElement {+ constructor () {+ super()+ }+ connectedCallback () {+ this.render()+ this.registerEvents()+ }+ disconnectedCallback () {+ this.unregisterEvents()+ }+ registerEvents () {+ this.querySelector('form').addEventListener('submit', this.addChannelHandler.bind(this))+ }+ unregisterEvents () {+ this.querySelector('form').removeEventListener('submit', this.addChannelHandler.bind(this))+ }+ render () {+ this.innerHTML = `+ <form id="add-channel-form">+ <label for="channel-name">Add a new channel</label>+ <input type="text" id="channel-name" placeholder="Channel Name" required>+ <button type="submit">Submit</button>+ <span class="loader"></span>+ </form>+ `+ }++ addChannelHandler (event) {+ event.preventDefault()++ const form = event.target+ const button = form.querySelector('button')+ const input = form.querySelector('input')+ const loader = form.querySelector('.loader')+ freezeForm()++ const channelName = document.getElementById('channel-name').value++ if (!channelName) return alert('empty channel name'), unfreezeForm();++ fetch('/channels', {+ method: 'POST',+ headers: { 'Content-Type': 'application/json' },+ body: JSON.stringify({ name: channelName })+ })+ .then(() => form.reset())+ .catch(error => console.error('Error adding channel:', error))+ .finally(unfreezeForm)++ function freezeForm() {+ input.disabled = true+ button.disabled = true+ loader.classList.add('show')+ }+ function unfreezeForm() {+ input.disabled = false+ button.disabled = false+ loader.classList.remove('show')+ }+ }+}+customElements.define('add-channel-form', AddChannelForm)diff --git a/client/components/search-videos.js b/client/components/search-videos.jsnew file mode 100644index 0000000..2e118e0--- /dev/null+++ b/client/components/search-videos.js@@ -0,0 +1,40 @@++class SearchVideos extends HTMLElement {+ constructor () {+ super()+ }+ connectedCallback () {+ this.render()+ this.registerEvents()+ }+ disconnectedCallback () {+ this.deregisterEvents()+ }+ registerEvents () {+ this.querySelector('input').addEventListener('keyup', this.searchHandler.bind(this))+ }+ deregisterEvents () {+ this.querySelector('input').removeEventListener('keyup', this.searchHandler.bind(this))+ }+ render () {+ this.innerHTML = `<input type="text" id="search" placeholder="Search videos" autofocus>`+ }+ searchHandler (event) {+ event.preventDefault()+ const searchTerm = event.target.value.toLowerCase()+ document.querySelectorAll('.video').forEach(video => {+ const videoData = JSON.parse(video.dataset['data'])+ if (+ videoData.title.toLowerCase().includes(searchTerm)+ || videoData.summary?.toLowerCase().includes(searchTerm)+ || videoData.channelName?.toLowerCase().includes(searchTerm)+ ) {+ video.style.display = ''+ } else {+ video.style.display = 'none'+ }+ })++ }+}+customElements.define('search-videos', SearchVideos)diff --git a/client/index.html b/client/index.htmlindex e35e16d..0ca33f5 100644--- a/client/index.html+++ b/client/index.html@@ -57,6 +57,8 @@<div></div></dialog>+ <script type="module" src="/components/search-videos.js"></script>+ <script type="module" src="/components/add-channel-form.js"></script><script type="module" src="/components/video-element.js"></script><script type="module" src="/main.js"></script></body>diff --git a/client/lib/store.js b/client/lib/store.jsnew file mode 100644index 0000000..5a45570--- /dev/null+++ b/client/lib/store.js@@ -0,0 +1,32 @@+export default class Store {+ showThumbnailsKey = 'showThumbnails'+ lastVideosKey = 'lastVideos'++ constructor() {+ if (!localStorage.getItem(this.showThumbnailsKey)) localStorage.setItem(this.showThumbnailsKey, 'true')+ if (!localStorage.getItem(this.lastVideosKey)) localStorage.setItem(this.lastVideosKey, '{}')+ }++ toggle (key) {+ if (![this.showThumbnailsKey].includes(key)) return console.error('invalid key', key)+ localStorage.setItem(key, localStorage.getItem(key) === 'true' ? 'false' : 'true')+ }+ get(key) {+ if (![this.showThumbnailsKey, this.lastVideosKey].includes(key)) return console.error('invalid key', key)+ return JSON.parse(localStorage.getItem(key))+ }+ set(key, value) {+ if (typeof value === 'string') {+ localStorage.setItem(key, value)+ } else {+ localStorage.setItem(key, JSON.stringify(value))+ }+ }+ // push(key, item) {+ // if (![this.ignoreVideoKey].includes(key)) return console.error('invalid key', key)+ // const list = this.get(key)+ // list.push(item)+ // this.set(key, list)+ // return item+ // }+}\ No newline at end of filediff --git a/client/main.js b/client/main.jsindex 07c3f7a..b6c3d50 100644--- a/client/main.js+++ b/client/main.js@@ -1,100 +1,5 @@-class AddChannelForm extends HTMLElement {- constructor () {- super()- }- connectedCallback () {- this.render()- this.registerEvents()- }- disconnectedCallback () {- this.unregisterEvents()- }- registerEvents () {- this.querySelector('form').addEventListener('submit', this.addChannelHandler.bind(this))- }- unregisterEvents () {- this.querySelector('form').removeEventListener('submit', this.addChannelHandler.bind(this))- }- render () {- this.innerHTML = `- <form id="add-channel-form">- <label for="channel-name">Add a new channel</label>- <input type="text" id="channel-name" placeholder="Channel Name" required>- <button type="submit">Submit</button>- <span class="loader"></span>- </form>- `- }-- addChannelHandler (event) {- event.preventDefault()-- const form = event.target- const button = form.querySelector('button')- const input = form.querySelector('input')- const loader = form.querySelector('.loader')- freezeForm()-- const channelName = document.getElementById('channel-name').value-- if (!channelName) return alert('empty channel name'), unfreezeForm();-- fetch('/channels', {- method: 'POST',- headers: { 'Content-Type': 'application/json' },- body: JSON.stringify({ name: channelName })- })- .then(() => form.reset())- .catch(error => console.error('Error adding channel:', error))- .finally(unfreezeForm)-- function freezeForm() {- input.disabled = true- button.disabled = true- loader.classList.add('show')- }- function unfreezeForm() {- input.disabled = false- button.disabled = false- loader.classList.remove('show')- }- }-}-customElements.define('add-channel-form', AddChannelForm)---class Store {- showThumbnailsKey = 'showThumbnails'- lastVideosKey = 'lastVideos'-- constructor() {- if (!localStorage.getItem(this.showThumbnailsKey)) localStorage.setItem(this.showThumbnailsKey, 'true')- if (!localStorage.getItem(this.lastVideosKey)) localStorage.setItem(this.lastVideosKey, '{}')- }-- toggle (key) {- if (![this.showThumbnailsKey].includes(key)) return console.error('invalid key', key)- localStorage.setItem(key, localStorage.getItem(key) === 'true' ? 'false' : 'true')- }- get(key) {- if (![this.showThumbnailsKey, this.lastVideosKey].includes(key)) return console.error('invalid key', key)- return JSON.parse(localStorage.getItem(key))- }- set(key, value) {- if (typeof value === 'string') {- localStorage.setItem(key, value)- } else {- localStorage.setItem(key, JSON.stringify(value))- }- }- // push(key, item) {- // if (![this.ignoreVideoKey].includes(key)) return console.error('invalid key', key)- // const list = this.get(key)- // list.push(item)- // this.set(key, list)- // return item- // }-}+import Store from '/lib/store.js'+const store = new Store()// app: sse updates and rendersconst eventSource = new window.EventSource('/')@@ -152,47 +57,6 @@ eventSource.onmessage = (message) => {}}-class SearchVideos extends HTMLElement {- constructor () {- super()- }- connectedCallback () {- this.render()- this.registerEvents()- }- disconnectedCallback () {- this.deregisterEvents()- }- registerEvents () {- this.querySelector('input').addEventListener('keyup', this.searchHandler.bind(this))- }- deregisterEvents () {- this.querySelector('input').removeEventListener('keyup', this.searchHandler.bind(this))- }- render () {- this.innerHTML = `<input type="text" id="search" placeholder="Search videos" autofocus>`- }- searchHandler (event) {- event.preventDefault()- const searchTerm = event.target.value.toLowerCase()- document.querySelectorAll('.video').forEach(video => {- const videoData = JSON.parse(video.dataset['data'])- if (- videoData.title.toLowerCase().includes(searchTerm)- || videoData.summary?.toLowerCase().includes(searchTerm)- || videoData.channelName?.toLowerCase().includes(searchTerm)- ) {- video.style.display = ''- } else {- video.style.display = 'none'- }- })-- }-}-customElements.define('search-videos', SearchVideos)--const store = new Store()// settings modalconst $settings = document.querySelector("dialog#settings")diff --git a/lib/server.js b/lib/server.jsindex 4ffc67a..3433544 100644--- a/lib/server.js+++ b/lib/server.js@@ -41,6 +41,12 @@ export function createServer ({repo, connections = []}) {return fileHandler('client/main.js', 'application/javascript')(req, res)if (url.pathname === '/components/video-element.js')return fileHandler('client/components/video-element.js', 'application/javascript')(req, res)+ if (url.pathname === '/components/add-channel-form.js')+ return fileHandler('client/components/add-channel-form.js', 'application/javascript')(req, res)+ if (url.pathname === '/components/search-videos.js')+ return fileHandler('client/components/search-videos.js', 'application/javascript')(req, res)+ if (url.pathname === '/lib/store.js')+ return fileHandler('client/lib/store.js', 'application/javascript')(req, res)return fileHandler('client/index.html', 'text/html')(req, res)})