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 captions in settings

Christian Fei9cc686994dc0
diff --git a/client/components/video-element.js b/client/components/video-element.js
index be5fca6..feace74 100644
--- a/client/components/video-element.js
+++ b/client/components/video-element.js
@@ -100,7 +100,9 @@ class VideoElement extends HTMLElement {
this.querySelector('.play.video-placeholder').outerHTML = /*html*/`
<video controls playsinline style="user-select: none; width: 400px; width: -webkit-fill-available;">
<source src="/api/videos/${this.video.id}.${this.video.format || 'mp4'}" type="video/${this.video.format || 'mp4'}" />
- <track default kind="captions" srclang="en" src="/api/captions/${this.video.id}" />
+ ${store.get(store.showCaptionsKey) ? /*html*/`
+ <track default kind="captions" srclang="en" src="/api/captions/${this.video.id}" />
+ ` : ''}
<p>
Your browser does not support the video tag.
Download the video instead <a href="/api/videos/${this.video.id}" target="_blank">here</a>
diff --git a/client/index.html b/client/index.html
index 2f43e62..1fded2c 100644
--- a/client/index.html
+++ b/client/index.html
@@ -79,6 +79,13 @@
<div><small>Show the creators chosen thumbnail, or a neutral one based on the actual video frames</small></div>
</form>
<br>
+ <form>
+ <label for="show-captions">
+ Show captions for video
+ </label>
+ <input type="checkbox" id="show-captions"/>
+ </form>
+ <br>
<form>
<label for="use-tldw-tube">
Use TLDW.tube for video summarization (opens externally)
diff --git a/client/lib/router.js b/client/lib/router.js
index 63e25b8..ee2a928 100644
--- a/client/lib/router.js
+++ b/client/lib/router.js
@@ -70,6 +70,13 @@ const routes = {
$useTLDWTube.addEventListener('click', (event) => {
store.toggle(store.useTLDWTubeKey)
})
+
+ const $showCaptions = document.getElementById('show-captions')
+ store.get(store.showCaptionsKey) ? $showCaptions.setAttribute('checked', 'true') : $showCaptions.removeAttribute('checked')
+
+ $showCaptions.addEventListener('click', (event) => {
+ store.toggle(store.showCaptionsKey)
+ })
} },
'/404': { template: document.getElementById('not-found-template'), async initialize () {
document.querySelector('search-videos #search').setAttribute('disabled', 'disabled')
diff --git a/client/lib/store.js b/client/lib/store.js
index 215347d..0c9ae35 100644
--- a/client/lib/store.js
+++ b/client/lib/store.js
@@ -2,6 +2,7 @@ export default class Store {
showThumbnailsKey = 'showThumbnails'
showBigPlayerKey = 'showBigPlayer'
showOriginalThumbnailKey = 'showOriginalThumbnail'
+ showCaptionsKey = 'showCaptions'
useTLDWTubeKey = 'useTLDWTube'
lastVideosKey = 'lastVideos'
ignoreTermsKey = 'ignoreTerms'
@@ -10,17 +11,18 @@ export default class Store {
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.showCaptionsKey)) localStorage.setItem(this.showCaptionsKey, 'true')
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, '[]')
}
toggle (key) {
- if (![this.showThumbnailsKey, this.showBigPlayerKey, this.showOriginalThumbnailKey, this.useTLDWTubeKey].includes(key)) return console.error('invalid key', key)
+ if (![this.showThumbnailsKey, this.showBigPlayerKey, this.showOriginalThumbnailKey, this.showCaptionsKey, this.useTLDWTubeKey].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.showOriginalThumbnailKey, 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].includes(key)) return console.error('invalid key', key)
return JSON.parse(localStorage.getItem(key))
}
set(key, value) {