Skip to content

armgit / my-yt

Public Imported from GitHub
0

A clean and minimal youtube frontend, without all the ads and whistles

JavaScript

feat: toggle show original thumbnail

Christian Fei2787fcfe8bc6
diff --git a/client/index.html b/client/index.html
index c7a595d..257a8e5 100644
--- a/client/index.html
+++ b/client/index.html
@@ -60,6 +60,13 @@
</label>
<input type="checkbox" id="show-big-player"/>
</form>
+ <br>
+ <form>
+ <label for="show-original-thumbnail">
+ Show original thumbnail (potentially clickbait)
+ </label>
+ <input type="checkbox" id="show-original-thumbnail"/>
+ </form>
<!--
<br>
<div>
diff --git a/client/lib/store.js b/client/lib/store.js
index 15c9245..0580d5a 100644
--- a/client/lib/store.js
+++ b/client/lib/store.js
@@ -1,22 +1,24 @@
export default class Store {
showThumbnailsKey = 'showThumbnails'
showBigPlayerKey = 'showBigPlayer'
+ showOriginalThumbnailKey = 'showOriginalThumbnail'
lastVideosKey = 'lastVideos'
ignoreTermsKey = 'ignoreTerms'
constructor() {
if (!localStorage.getItem(this.showThumbnailsKey)) localStorage.setItem(this.showThumbnailsKey, 'true')
if (!localStorage.getItem(this.showBigPlayerKey)) localStorage.setItem(this.showBigPlayerKey, 'true')
+ if (!localStorage.getItem(this.showOriginalThumbnailKey)) localStorage.setItem(this.showOriginalThumbnailKey, 'false')
if (!localStorage.getItem(this.lastVideosKey)) localStorage.setItem(this.lastVideosKey, '{}')
if (!localStorage.getItem(this.ignoreTermsKey)) localStorage.setItem(this.ignoreTermsKey, '[]')
}
toggle (key) {
- if (![this.showThumbnailsKey, this.showBigPlayerKey].includes(key)) return console.error('invalid key', key)
+ if (![this.showThumbnailsKey, this.showBigPlayerKey, this.showOriginalThumbnailKey].includes(key)) return console.error('invalid key', key)
localStorage.setItem(key, localStorage.getItem(key) === 'true' ? 'false' : 'true')
}
get(key) {
- if (![this.showThumbnailsKey, this.showBigPlayerKey, this.lastVideosKey, this.ignoreTermsKey].includes(key)) return console.error('invalid key', key)
+ if (![this.showThumbnailsKey, this.showBigPlayerKey, this.showOriginalThumbnailKey, this.lastVideosKey, this.ignoreTermsKey].includes(key)) return console.error('invalid key', key)
return JSON.parse(localStorage.getItem(key))
}
set(key, value) {
diff --git a/client/main.js b/client/main.js
index 25e9ecb..d1fd29d 100644
--- a/client/main.js
+++ b/client/main.js
@@ -52,14 +52,15 @@ const routes = {
$videosContainer.innerHTML = ''
- const ignoredTerms = window.store.get(window.store.ignoreTermsKey)
+ const ignoredTerms = store.get(store.ignoreTermsKey)
+ const showOriginalThumbnail = store.get(store.showOriginalThumbnailKey)
videos = videos
.filter(video => video.title.split(' ').every(word => !ignoredTerms.includes(word.toLowerCase().replace(/('s|"|,|:)/,''))))
.sort((a, b) => +new Date(b.publishedAt) - +new Date(a.publishedAt))
// .filter((_, i) => i < 1000)
- videos.forEach(video => $videosContainer.appendChild(createVideoElement(video)))
+ videos.forEach(video => $videosContainer.appendChild(createVideoElement(video, showOriginalThumbnail)))
const channelsList = videos.reduce((acc, video) => {
if (!acc.includes(video.channelName)) acc.push(video.channelName)
@@ -89,6 +90,13 @@ const routes = {
store.toggle(store.showBigPlayerKey)
applyShowBigPlayer(store.get(store.showBigPlayerKey))
})
+
+ const $showOriginalThumbnail = document.getElementById('show-original-thumbnail')
+ store.get(store.showOriginalThumbnailKey) ? $showOriginalThumbnail.setAttribute('checked', 'true') : $showOriginalThumbnail.removeAttribute('checked')
+
+ $showOriginalThumbnail.addEventListener('click', (event) => {
+ store.toggle(store.showOriginalThumbnailKey)
+ })
} },
'/404': { template: document.getElementById('not-found-template') }
}
@@ -134,11 +142,12 @@ eventSource.onmessage = (message) => {
if (data.type === 'new-videos' && data.videos) {
$videosContainer = document.querySelector('.main-videos-container')
if (!$videosContainer) return
+ const showOriginalThumbnail = store.get(store.showOriginalThumbnailKey)
data.videos.forEach(video => {
const $videoElement = $videosContainer.querySelector('video-element')
- if (!$videoElement) return $videosContainer.appendChild(createVideoElement(video))
- $videoElement.parentNode.insertBefore(createVideoElement(video), $videoElement.nextSibling)
+ if (!$videoElement) return $videosContainer.appendChild(createVideoElement(video, showOriginalThumbnail))
+ $videoElement.parentNode.insertBefore(createVideoElement(video, showOriginalThumbnail), $videoElement.nextSibling)
})
return
}
@@ -198,9 +207,11 @@ function observeDialogOpenPreventScroll (dialog) {
}).observe(dialog, { attributes: true, childList: true, subtree: true })
}
-function createVideoElement (video) {
+function createVideoElement (video, showOriginalThumbnail = false) {
const $video = document.createElement('video-element')
- $video.dataset['data'] = JSON.stringify(video)
+ $video.dataset['data'] = JSON.stringify(Object.assign(video, showOriginalThumbnail ? {
+ thumbnail: video.thumbnail.replace('mq2.jpg', 'mqdefault.jpg')
+ } : {}))
$video.dataset['videoId'] = video.id
return $video
}