A clean and minimal youtube frontend, without all the ads and whistles
JavaScript
feat: add watched toggle with unwatched filter
Christian Feib5815ecf4b92
diff --git a/client/components/search-videos.js b/client/components/search-videos.jsindex faeb392..05d334c 100644--- a/client/components/search-videos.js+++ b/client/components/search-videos.js@@ -20,12 +20,14 @@ class SearchVideos extends HTMLElement {const ignored = searchParams.get('ignored') || ''const downloaded = searchParams.get('downloaded') || ''const summarized = searchParams.get('summarized') || ''+ const unwatched = searchParams.get('unwatched') || ''const searchTerm = searchParams.get('filter') || ''this.querySelector('#search').value = searchTermthis.querySelector('#excluded').checked = excluded === 'true'this.querySelector('#ignored').checked = ignored === 'true'this.querySelector('#downloaded').checked = downloaded === 'true'this.querySelector('#summarized').checked = summarized === 'true'+ this.querySelector('#unwatched').checked = unwatched === 'true'}disconnectedCallback () {@@ -38,6 +40,7 @@ class SearchVideos extends HTMLElement {this.querySelector('#ignored').addEventListener('change', this.searchHandler.bind(this))this.querySelector('#downloaded').addEventListener('change', this.searchHandler.bind(this))this.querySelector('#summarized').addEventListener('change', this.searchHandler.bind(this))+ this.querySelector('#unwatched').addEventListener('change', this.searchHandler.bind(this))}unregisterEvents () {@@ -46,6 +49,7 @@ class SearchVideos extends HTMLElement {this.querySelector('#ignored').removeEventListener('change', this.searchHandler.bind(this))this.querySelector('#downloaded').removeEventListener('change', this.searchHandler.bind(this))this.querySelector('#summarized').removeEventListener('change', this.searchHandler.bind(this))+ this.querySelector('#unwatched').removeEventListener('change', this.searchHandler.bind(this))}render () {@@ -71,6 +75,10 @@ class SearchVideos extends HTMLElement {<input type="checkbox" id="excluded"/><label for="excluded">excluded</label></div>+ <div class="flex-1">+ <input type="checkbox" id="unwatched"/>+ <label for="unwatched">unwatched</label>+ </div></div></details></div>@@ -105,6 +113,7 @@ class SearchVideos extends HTMLElement {const ignored = this.querySelector('#ignored').checkedconst downloaded = this.querySelector('#downloaded').checkedconst summarized = this.querySelector('#summarized').checked+ const unwatched = this.querySelector('#unwatched').checkedif (excluded) {this.querySelector('#ignored').disabled = true@@ -117,7 +126,7 @@ class SearchVideos extends HTMLElement {this.querySelector('#excluded').disabled = false}- const query = `?filter=${encodeURIComponent(searchTerm)}${excluded ? '&excluded=true' : ''}${downloaded ? '&downloaded=true' : ''}${ignored ? '&ignored=true' : ''}${summarized ? '&summarized=true' : ''}`+ const query = `?filter=${encodeURIComponent(searchTerm)}${excluded ? '&excluded=true' : ''}${downloaded ? '&downloaded=true' : ''}${ignored ? '&ignored=true' : ''}${summarized ? '&summarized=true' : ''}${unwatched ? '&unwatched=true' : ''}`window.history.pushState({}, '', query)fetch(ENDPOINTS.VIDEOS + query)diff --git a/client/components/video-element.js b/client/components/video-element.jsindex cd6cf86..3182356 100644--- a/client/components/video-element.js+++ b/client/components/video-element.js@@ -96,6 +96,7 @@ class VideoElement extends HTMLElement {? /* html */`<span tabindex="0" class="action summarize" data-video-id="${this.video.id}">π Summarize</span>`: /* html */`<span tabindex="0" class="action show-summary" data-video-id="${this.video.id}">π Summary</span>`}<a href="https://www.youtube.com/watch?v=${this.video.id}" class="action open-externally" target="_blank">πΊ external</a>+ <span tabindex="0" class="action watched" data-video-id="${this.video.id}">${store.isWatched(this.video.id) ? 'β Watched' : 'β Unwatched'}</span></div>`@@ -116,6 +117,7 @@ class VideoElement extends HTMLElement {addClickListener(this.querySelector('.action.show-summary'), this.showSummaryHandler.bind(this))addClickListener(this.querySelector('.action.ignore'), this.toggleIgnoreVideoHandler.bind(this))addClickListener(this.querySelector('.action.open-externally'), this.openExternallyHandler.bind(this))+ addClickListener(this.querySelector('.action.watched'), this.toggleWatchedVideoHandler.bind(this))addClickListener(this.querySelector('.channel-name'), this.filterByChannelHandler.bind(this))addClickListener(this.querySelector('.play.video-placeholder'), this.watchVideoHandler.bind(this))}@@ -127,6 +129,7 @@ class VideoElement extends HTMLElement {removeClickListener(this.querySelector('.action.show-summary'), this.showSummaryHandler.bind(this))removeClickListener(this.querySelector('.action.ignore'), this.toggleIgnoreVideoHandler.bind(this))removeClickListener(this.querySelector('.action.open-externally'), this.openExternallyHandler.bind(this))+ removeClickListener(this.querySelector('.action.watched'), this.toggleWatchedVideoHandler.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'))@@ -244,6 +247,21 @@ class VideoElement extends HTMLElement {this.querySelector('video') && this.querySelector('video').pause()}+ toggleWatchedVideoHandler (event) {+ event.preventDefault()++ fetch(ENDPOINTS.WATCHED_VIDEO, {+ method: 'POST',+ headers: { 'Content-Type': 'application/json' },+ body: JSON.stringify({ id: this.video.id })+ })+ .then(() => {+ store.toggleWatched(this.video.id)+ this.render()+ })+ .catch((error) => console.error('Error toggling watched status:', error))+ }+filterByChannelHandler (event) {const $searchInput = document.querySelector('#search')if (!$searchInput) returndiff --git a/client/lib/api.js b/client/lib/api.jsindex cbe9367..815ffbd 100644--- a/client/lib/api.js+++ b/client/lib/api.js@@ -12,7 +12,8 @@ export const ENDPOINTS = {DISK_USAGE: '/api/disk-usage',RECLAIM_DISK_SPACE: '/api/reclaim-disk-space',TRANSCODE_VIDEOS: '/api/transcode-videos',- EXCLUDED_TERMS: '/api/excluded-terms'+ EXCLUDED_TERMS: '/api/excluded-terms',+ WATCHED_VIDEO: '/api/watched-video'}export class ApiClient {diff --git a/client/lib/store.js b/client/lib/store.jsindex 2bd32fe..3541f6e 100644--- a/client/lib/store.js+++ b/client/lib/store.js@@ -7,6 +7,7 @@ export default class Store {useTLDWTubeKey = 'useTLDWTube'lastVideosKey = 'lastVideos'ignoreTermsKey = 'ignoreTerms'+ watchedVideosKey = 'watchedVideos'constructor () {if (!localStorage.getItem(this.showThumbnailsKey)) localStorage.setItem(this.showThumbnailsKey, 'true')@@ -16,6 +17,7 @@ export default class Store {if (!localStorage.getItem(this.useTLDWTubeKey)) localStorage.setItem(this.useTLDWTubeKey, 'false')if (!localStorage.getItem(this.lastVideosKey)) localStorage.setItem(this.lastVideosKey, '{}')if (!localStorage.getItem(this.ignoreTermsKey)) localStorage.setItem(this.ignoreTermsKey, '[]')+ if (!localStorage.getItem(this.watchedVideosKey)) localStorage.setItem(this.watchedVideosKey, '[]')}toggle (key) {@@ -24,7 +26,7 @@ export default class Store {}get (key) {- if (![this.showThumbnailsKey, this.showBigPlayerKey, this.showOriginalThumbnailKey, this.showCaptionsKey, this.useTLDWTubeKey, this.lastVideosKey, this.ignoreTermsKey].includes(key)) return console.error('invalid key', key)+ if (![this.showThumbnailsKey, this.showBigPlayerKey, this.showOriginalThumbnailKey, this.showCaptionsKey, this.useTLDWTubeKey, this.lastVideosKey, this.ignoreTermsKey, this.watchedVideosKey].includes(key)) return console.error('invalid key', key)return JSON.parse(localStorage.getItem(key))}@@ -37,7 +39,7 @@ export default class Store {}push (key, item) {- if (![this.ignoreTermsKey].includes(key)) return console.error('invalid key', key)+ if (![this.ignoreTermsKey, this.watchedVideosKey].includes(key)) return console.error('invalid key', key)const list = this.get(key)list.push(item)this.set(key, list)@@ -45,7 +47,7 @@ export default class Store {}remove (key, item) {- if (![this.ignoreTermsKey].includes(key)) return console.error('invalid key', key)+ if (![this.ignoreTermsKey, this.watchedVideosKey].includes(key)) return console.error('invalid key', key)let list = this.get(key)list = list.filter(i => i !== item)this.set(key, list)@@ -53,8 +55,20 @@ export default class Store {}includes (key, item) {- if (![this.ignoreTermsKey].includes(key)) return console.error('invalid key', key)+ if (![this.ignoreTermsKey, this.watchedVideosKey].includes(key)) return console.error('invalid key', key)const list = this.get(key)return list.includes(item)}++ isWatched (videoId) {+ return this.get(this.watchedVideosKey).includes(videoId)+ }++ toggleWatched (videoId) {+ if (this.isWatched(videoId)) {+ this.remove(this.watchedVideosKey, videoId)+ } else {+ this.push(this.watchedVideosKey, videoId)+ }+ }}diff --git a/client/main.js b/client/main.jsindex 73d9452..f570fcd 100644--- a/client/main.js+++ b/client/main.js@@ -1,4 +1,8 @@/* global EventSource, MutationObserver */+import Store from './lib/store.js'++const store = new Store()+window.state = {downloading: {},summarizing: {},@@ -111,6 +115,14 @@ window.eventSource.onmessage = (message) => {})return}+ if (data.type === 'watched' && data.videoId) {+ store.toggleWatched(data.videoId)+ ;[...document.querySelectorAll(`[data-video-id="${data.videoId}"]`)].forEach($video => {+ if (!$video.dataset.data) return+ $video.render()+ })+ return+ }console.warn('unhandled', data)} catch (err) {console.error('sse parse error', err)diff --git a/lib/repository.js b/lib/repository.jsindex 75323f8..ceee0b3 100644--- a/lib/repository.js+++ b/lib/repository.js@@ -22,7 +22,8 @@ export default class Repository {fs.writeFileSync(this.paths.settings, JSON.stringify({videoQuality: 720,transcodeVideos: true,- excludedTerms: []+ excludedTerms: [],+ watchedVideos: []}, null, 2))}@@ -38,6 +39,10 @@ export default class Repository {this.settings.excludedTerms = []this.saveSettings()}+ if (this.settings.watchedVideos === undefined) {+ this.settings.watchedVideos = []+ this.saveSettings()+ }fs.readdirSync(`${this.basePath}/videos`).forEach(file => {const format = file.substring(file.lastIndexOf('.') + 1)@@ -115,7 +120,11 @@ export default class Repository {if (query.summarized === 'true') {videos = videos.filter(v => v.summary)}- if (filterKeys.length === 0) {++ const watchedVideos = this.getWatchedVideos()+ if (query.unwatched === 'true') {+ videos = videos.filter(v => !watchedVideos.includes(v.id))+ } else if (filterKeys.length === 0) {videos = videos.filter(v => !v.ignored)}@@ -256,4 +265,23 @@ export default class Repository {this.saveSettings()}}++ getWatchedVideos () {+ return (this.settings.watchedVideos || [])+ }++ isWatched (videoId) {+ return this.getWatchedVideos().includes(videoId)+ }++ toggleWatched (videoId) {+ const watched = this.getWatchedVideos()+ if (watched.includes(videoId)) {+ watched.splice(watched.indexOf(videoId), 1)+ } else {+ watched.push(videoId)+ }+ this.settings.watchedVideos = watched+ this.saveSettings()+ }}diff --git a/server/router/handlers/settings.js b/server/router/handlers/settings.jsindex a1304c9..f1a806c 100644--- a/server/router/handlers/settings.js+++ b/server/router/handlers/settings.js@@ -1,4 +1,4 @@-import { parseBody } from '../_helpers.js'+import { parseBody, broadcastMessage } from '../_helpers.js'import { getRepository } from '../../service-container.js'export function getVideoQualityHandler (req, res, connections = [], state = {}) {@@ -68,4 +68,19 @@ export async function removeExcludedTermHandler (req, res) {res.end()}-export default { getVideoQualityHandler, setVideoQualityHandler, getTranscodeVideosHandler, setTranscodeVideosHandler, getExcludedTermsHandler, addExcludedTermHandler, removeExcludedTermHandler }+export async function toggleWatchedVideoHandler (req, res, connections = [], state = {}) {+ const repo = getRepository()+ const parsed = await parseBody(req, res)+ if (parsed === null) return+ const videoId = parsed.id+ repo.toggleWatched(videoId)++ // Broadcast SSE to update all connected clients+ const updated = repo.isWatched(videoId)+ broadcastMessage('watched', { videoId, watched: updated }, connections)++ res.writeHead(200)+ res.end()+}++export default { getVideoQualityHandler, setVideoQualityHandler, getTranscodeVideosHandler, setTranscodeVideosHandler, getExcludedTermsHandler, addExcludedTermHandler, removeExcludedTermHandler, toggleWatchedVideoHandler }diff --git a/server/router/index.js b/server/router/index.jsindex f3943f1..ccf81ba 100644--- a/server/router/index.js+++ b/server/router/index.js@@ -46,6 +46,9 @@ const routeMap = {'POST /api/excluded-terms': settings.addExcludedTermHandler,'DELETE /api/excluded-terms': settings.removeExcludedTermHandler,+ // Watched videos: POST+ 'POST /api/watched-video': settings.toggleWatchedVideoHandler,+// Scraper health: GET'GET /api/scraper-status': scraperStatusHandler}