Skip to content

armgit / my-yt

Public Imported from GitHub
0

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

JavaScript

wip ignored terms

Christian Feiceda6a7648a2
diff --git a/client/index.html b/client/index.html
index 88672fd..5841111 100644
--- a/client/index.html
+++ b/client/index.html
@@ -46,6 +46,11 @@
</label>
<input type="checkbox" id="show-thumbnails"/>
</form>
+ <br>
+ <div>
+ <input type="text" id="add-ignored-term"/>
+ <ul id="ignored-terms"></ul>
+ </div>
</dialog>
<dialog id="summary">
diff --git a/client/lib/store.js b/client/lib/store.js
index 5d40f81..882f083 100644
--- a/client/lib/store.js
+++ b/client/lib/store.js
@@ -2,11 +2,13 @@ export default class Store {
showThumbnailsKey = 'showThumbnails'
lastVideosKey = 'lastVideos'
ignoreVideoKey = 'ignoreVideo'
+ ignoreTermsKey = 'ignoreTerms'
constructor() {
if (!localStorage.getItem(this.showThumbnailsKey)) localStorage.setItem(this.showThumbnailsKey, 'true')
if (!localStorage.getItem(this.lastVideosKey)) localStorage.setItem(this.lastVideosKey, '{}')
if (!localStorage.getItem(this.ignoreVideoKey)) localStorage.setItem(this.ignoreVideoKey, '[]')
+ if (!localStorage.getItem(this.ignoreTermsKey)) localStorage.setItem(this.ignoreTermsKey, '[]')
}
toggle (key) {
@@ -14,7 +16,7 @@ export default class Store {
localStorage.setItem(key, localStorage.getItem(key) === 'true' ? 'false' : 'true')
}
get(key) {
- if (![this.showThumbnailsKey, this.lastVideosKey, this.ignoreVideoKey].includes(key)) return console.error('invalid key', key)
+ if (![this.showThumbnailsKey, this.lastVideosKey, this.ignoreVideoKey, this.ignoreTermsKey].includes(key)) return console.error('invalid key', key)
return JSON.parse(localStorage.getItem(key))
}
set(key, value) {
@@ -25,14 +27,21 @@ export default class Store {
}
}
push(key, item) {
- if (![this.ignoreVideoKey].includes(key)) return console.error('invalid key', key)
+ if (![this.ignoreVideoKey, this.ignoreTermsKey].includes(key)) return console.error('invalid key', key)
const list = this.get(key)
list.push(item)
this.set(key, list)
return item
}
+ remove(key, item) {
+ if (![this.ignoreTermsKey].includes(key)) return console.error('invalid key', key)
+ let list = this.get(key)
+ list = list.filter(i => i !== item)
+ this.set(key, list)
+ return item
+ }
includes(key, item) {
- if (![this.ignoreVideoKey].includes(key)) return console.error('invalid key', key)
+ if (![this.ignoreVideoKey, this.ignoreTermsKey].includes(key)) return console.error('invalid key', key)
const list = this.get(key)
return list.includes(item)
diff --git a/client/main.js b/client/main.js
index 6f025da..c563597 100644
--- a/client/main.js
+++ b/client/main.js
@@ -25,9 +25,12 @@ eventSource.onmessage = (message) => {
if (data.type === 'all' && data.videos) {
$videosContainer.innerHTML = ''
+ const ignoredTerms = window.store.get(window.store.ignoreTermsKey)
+
let allVideos = Object.entries(data.videos).reduce((acc, curr) => acc.concat(curr[1]), [])
allVideos = allVideos
.filter(video => !window.store.includes(window.store.ignoreVideoKey, video.id))
+ .filter(video => video.title.split(' ').every(word => !ignoredTerms.includes(word.toLowerCase().replace(/('s|"|,|:)/,''))))
.sort((a, b) => +new Date(b.publishedAt) - +new Date(a.publishedAt))
.map(video => {
$videosContainer.appendChild(createVideoElement(video))
@@ -79,12 +82,34 @@ $summary.addEventListener('close', () => {})
// settings ui
const $showThumbnails = document.getElementById('show-thumbnails')
$showThumbnails.addEventListener('click', (event) => {
+ event.preventDefault()
store.toggle(store.showThumbnailsKey)
applyShowThumbnails(store.get(store.showThumbnailsKey))
})
+const $addIgnoredTerm = document.getElementById('add-ignored-term')
+$addIgnoredTerm.addEventListener('keyup', (event) => {
+ event.preventDefault()
+ if (event.key !== 'Enter') return
+ const ignoredTerm = $addIgnoredTerm.value.trim().toLowerCase()
+ if (ignoredTerm) {
+ store.push(store.ignoreTermsKey, ignoredTerm)
+ $addIgnoredTerm.value = ''
+ }
+ applyIgnoredTerms(store.get(store.ignoreTermsKey))
+})
+
+function applyIgnoredTerms (ignoredTerms) {
+ const $ignoredTerms = document.getElementById('ignored-terms')
+ $ignoredTerms.innerHTML = ignoredTerms.map(term => `<li class="ignored-term">${term}</li>`).join('')
+ if (ignoredTerms.length === 0) {
+ $ignoredTerms.innerHTML = '<li>No ignored terms</li>'
+ }
+}
+
// apply settings
applyShowThumbnails(store.get(store.showThumbnailsKey))
+applyIgnoredTerms(store.get(store.ignoreTermsKey))
// observe dialog open/close and prevent body background scroll
observeDialogOpenPreventScroll($settings)