A clean and minimal youtube frontend, without all the ads and whistles
JavaScript
extract video-element
Christian Fei8f82af6d95e8
diff --git a/client/components/video-element.js b/client/components/video-element.jsnew file mode 100644index 0000000..a51ec17--- /dev/null+++ b/client/components/video-element.js@@ -0,0 +1,113 @@+class VideoElement extends HTMLElement {+ constructor () {+ super()+ }++ connectedCallback () {+ this.video = JSON.parse(this.dataset['data'])+ this.render()+ this.registerEvents()+ }+ disconnectedCallback () {+ this.unregisterEvents()+ }+ static get observedAttributes() {+ return ['data-data'];+ }++ attributeChangedCallback(name, _, newValue) {+ if (name === 'data-data') {+ this.video = JSON.parse(this.dataset['data'])+ this.unregisterEvents()+ this.render()+ this.registerEvents()+ }+ }+ render () {+ if (!this.video) return+ this.classList.add('video')+ this.dataset['videoId'] = this.video.id+ this.innerHTML = `+ ${this.video.downloaded+ ? `<video controls width="280">+ <source src="/videos/${this.video.id}" type="video/mp4" />+ <p>+ Your browser does not support the video tag.+ Download the video instead <a href="/videos/${this.video.id}" target="_blank">here</a>+ </p>+ </video>`+ : `<img loading="lazy" src="${this.video.thumbnail}"/>`}+ <div class="info">+ <span>${this.video.channelName}</span>+ <br>+ <span>${new Date(this.video.publishedAt).toISOString().substring(0, 10)}</span> | <span>${this.video.viewCount}</span> | <span>${this.video.duration || 'N/A'}</span><br/>+ </div>+ <h4 class="title">${this.video.title}</h4>+ <div class="actions">+ ${this.video.downloaded+ ? ''+ : `<span tabindex="0" class="action download" data-video-id="${this.video.id}">⬇️ Download</span>`}+ ${!this.video.summary+ ? `<span tabindex="0" class="action summarize" data-video-id="${this.video.id}">📖 Summarize</span>`+ : `<span tabindex="0" class="action show-summary" data-video-id="${this.video.id}">📖 Summary</span>`}+ </div>+ `+ }+ registerEvents () {+ handleClick(this.querySelector('.action.download'), this.downloadVideoHandler.bind(this))+ handleClick(this.querySelector('.action.summarize'), this.summarizeVideoHandler.bind(this))+ handleClick(this.querySelector('.action.show-summary'), this.showSummaryHandler.bind(this))++ function handleClick ($el, handler) {+ if (!$el) return+ $el.addEventListener('click', handler)+ $el.addEventListener('keydown', (event) => event.key === 'Enter' && handler(event))+ }+ }+ unregisterEvents () {+ handleClick(this.querySelector('.action.download'), this.downloadVideoHandler.bind(this))+ handleClick(this.querySelector('.action.summarize'), this.summarizeVideoHandler.bind(this))+ handleClick(this.querySelector('.action.show-summary'), this.showSummaryHandler.bind(this))++ function handleClick ($el, handler) {+ if (!$el) return+ $el.removeEventListener('click', handler)+ $el.removeEventListener('keydown', (event) => event.key === 'Enter' && handler(event))+ }+ }+ downloadVideoHandler (event) {+ event.preventDefault()+ const downloadStartedText = '⚡️ Download started'+ if (event.target.innerText === downloadStartedText) return console.log('already downloading')+ event.target.innerText = downloadStartedText+ fetch('/download-video', {+ method: 'POST',+ headers: { 'Content-Type': 'application/json' },+ body: JSON.stringify({ id: this.video.id }),+ })+ .then(() => console.log('Download started'))+ .catch((error) => console.error('Error starting download:', error))+ }+ summarizeVideoHandler (event) {+ event.preventDefault()+ const summaryStartedText = '⚡️ summary started'+ if (event.target.innerText === summaryStartedText) return console.log('already summarizing')+ event.target.innerText = summaryStartedText+ fetch('/summarize-video', {+ method: 'POST',+ headers: { 'Content-Type': 'application/json' },+ body: JSON.stringify({ id: this.video.id }),+ })+ .then(() => console.log('summary started'))+ .catch((error) => console.error('Error starting summary:', error))+ }+ showSummaryHandler (event) {+ event.preventDefault()+ const video = this.video+ if (video) {+ document.querySelector('dialog#summary').showModal()+ document.querySelector('dialog#summary div').innerHTML = `<pre>${video.summary}</pre><details><summary>transcript</summary><pre>${video.transcript}</pre></details>`+ }+ }+}+customElements.define('video-element', VideoElement)diff --git a/client/index.html b/client/index.htmlindex 588fcb0..e35e16d 100644--- a/client/index.html+++ b/client/index.html@@ -51,12 +51,13 @@</label><input type="checkbox" id="show-thumbnails"/></dialog>-+<dialog id="summary"><button id="close">X</button><div></div></dialog>- <script src="/main.js"></script>+ <script type="module" src="/components/video-element.js"></script>+ <script type="module" src="/main.js"></script></body></html>diff --git a/client/main.js b/client/main.jsindex b1323e2..07c3f7a 100644--- a/client/main.js+++ b/client/main.js@@ -1,118 +1,3 @@--class VideoElement extends HTMLElement {- constructor () {- super()- }-- connectedCallback () {- this.video = JSON.parse(this.dataset['data'])- this.render()- this.registerEvents()- }- disconnectedCallback () {- this.unregisterEvents()- }- static get observedAttributes() {- return ['data-data'];- }-- attributeChangedCallback(name, _, newValue) {- if (name === 'data-data') {- this.video = JSON.parse(this.dataset['data'])- this.unregisterEvents()- this.render()- this.registerEvents()- }- }- render () {- if (!this.video) return- this.classList.add('video')- this.dataset['videoId'] = this.video.id- this.innerHTML = `- ${this.video.downloaded- ? `<video controls width="280">- <source src="/videos/${this.video.id}" type="video/mp4" />- <p>- Your browser does not support the video tag.- Download the video instead <a href="/videos/${this.video.id}" target="_blank">here</a>- </p>- </video>`- : `<img loading="lazy" src="${this.video.thumbnail}"/>`}- <div class="info">- <span>${this.video.channelName}</span>- <br>- <span>${new Date(this.video.publishedAt).toISOString().substring(0, 10)}</span> | <span>${this.video.viewCount}</span> | <span>${this.video.duration || 'N/A'}</span><br/>- </div>- <h4 class="title">${this.video.title}</h4>- <div class="actions">- ${this.video.downloaded- ? ''- : `<span tabindex="0" class="action download" data-video-id="${this.video.id}">⬇️ Download</span>`}- ${!this.video.summary- ? `<span tabindex="0" class="action summarize" data-video-id="${this.video.id}">📖 Summarize</span>`- : `<span tabindex="0" class="action show-summary" data-video-id="${this.video.id}">📖 Summary</span>`}- </div>- `- }- registerEvents () {- handleClick(this.querySelector('.action.download'), this.downloadVideoHandler.bind(this))- handleClick(this.querySelector('.action.summarize'), this.summarizeVideoHandler.bind(this))- handleClick(this.querySelector('.action.show-summary'), this.showSummaryHandler.bind(this))-- function handleClick ($el, handler) {- if (!$el) return- $el.addEventListener('click', handler)- $el.addEventListener('keydown', (event) => event.key === 'Enter' && handler(event))- }- }- unregisterEvents () {- handleClick(this.querySelector('.action.download'), this.downloadVideoHandler.bind(this))- handleClick(this.querySelector('.action.summarize'), this.summarizeVideoHandler.bind(this))- handleClick(this.querySelector('.action.show-summary'), this.showSummaryHandler.bind(this))-- function handleClick ($el, handler) {- if (!$el) return- $el.removeEventListener('click', handler)- $el.removeEventListener('keydown', (event) => event.key === 'Enter' && handler(event))- }- }- downloadVideoHandler (event) {- event.preventDefault()- const downloadStartedText = '⚡️ Download started'- if (event.target.innerText === downloadStartedText) return console.log('already downloading')- event.target.innerText = downloadStartedText- fetch('/download-video', {- method: 'POST',- headers: { 'Content-Type': 'application/json' },- body: JSON.stringify({ id: this.video.id }),- })- .then(() => console.log('Download started'))- .catch((error) => console.error('Error starting download:', error))- }- summarizeVideoHandler (event) {- event.preventDefault()- const summaryStartedText = '⚡️ summary started'- if (event.target.innerText === summaryStartedText) return console.log('already summarizing')- event.target.innerText = summaryStartedText- fetch('/summarize-video', {- method: 'POST',- headers: { 'Content-Type': 'application/json' },- body: JSON.stringify({ id: this.video.id }),- })- .then(() => console.log('summary started'))- .catch((error) => console.error('Error starting summary:', error))- }- showSummaryHandler (event) {- event.preventDefault()- const video = this.video- if (video) {- document.querySelector('dialog#summary').showModal()- document.querySelector('dialog#summary div').innerHTML = `<pre>${video.summary}</pre><details><summary>transcript</summary><pre>${video.transcript}</pre></details>`- }- }-}-customElements.define('video-element', VideoElement)-class AddChannelForm extends HTMLElement {constructor () {super()diff --git a/lib/server.js b/lib/server.jsindex 055d211..4ffc67a 100644--- a/lib/server.js+++ b/lib/server.js@@ -39,6 +39,8 @@ export function createServer ({repo, connections = []}) {return fileHandler('client/normalize.css', 'text/css')(req, res)if (url.pathname === '/main.js')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)return fileHandler('client/index.html', 'text/html')(req, res)})