Skip to content

armgit / my-yt

Public Imported from GitHub
0

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

JavaScript

freeze form, show loader, just add channel

Christian Fei4800a99007c8
diff --git a/index.html b/index.html
index 6ea3a1b..b8a803a 100644
--- a/index.html
+++ b/index.html
@@ -8,21 +8,38 @@
body {
font-family: Arial, sans-serif;
margin: 20px;
+ /* white on black style */
+ background-color: #000;
+ color: #fff;
+
}
form {
margin-bottom: 20px;
}
- ul {
- list-style-type: none;
- padding: 0;
- }
- li {
- margin-bottom: 10px;
- }
main {
user-select: none;
-webkit-user-select: none;
}
+ .m-0 {
+ margin: 0;
+ }
+ .loader {
+ display: inline-block;
+ border: 2px solid #f3f3f3;
+ border-top: 2px solid #3498db;
+ border-radius: 50%;
+ width: 0.5rem;
+ height: 0.5rem;
+ animation: spin 2s linear infinite;
+ opacity: 0;
+ }
+ .loader.show {
+ opacity: 1;
+ }
+ @keyframes spin {
+ 0% { transform: rotate(0deg); }
+ 100% { transform: rotate(360deg); }
+ }
</style>
<style>
.videos-container {
@@ -110,9 +127,13 @@
</head>
<body>
<header>
+ <h1 class="m-0">My-YT</h1>
+ <br>
<form id="add-channel-form">
+ <label for="channel-name">Add a new channel</label>
<input type="text" id="channel-name" placeholder="Channel Name" required>
- <button type="submit">Add Channel</button>
+ <button type="submit">Submit</button>
+ <span class="loader"></span>
</form>
</header>
@@ -204,10 +225,16 @@
document.getElementById('add-channel-form').addEventListener('submit', (event) => {
event.preventDefault()
+
+ const form = event.target
+ const button = form.querySelector('button')
+ const input = form.querySelector('input')
+ const loader = form.querySelector('.loader')
+ freezeForm()
const channelName = document.getElementById('channel-name').value
- if (!channelName) return alert('empty channel name')
+ if (!channelName) return alert('empty channel name'), unfreezeForm();
fetch('/channels', {
method: 'POST',
@@ -221,6 +248,17 @@
.catch(error => {
console.error('Error adding channel:', error)
})
+ .finally(unfreezeForm)
+ function freezeForm() {
+ input.disabled = true
+ button.disabled = true
+ loader.classList.add('show')
+ }
+ function unfreezeForm() {
+ input.disabled = false
+ button.disabled = false
+ loader.classList.remove('show')
+ }
})
async function fetchVideosByChannel () {
diff --git a/server.js b/server.js
index 143e67e..5417b7d 100644
--- a/server.js
+++ b/server.js
@@ -88,6 +88,12 @@ async function updateAndPersistVideos (repo) {
console.log('All videos updated')
}
+async function updateAndPersistVideosForChannel(repo, name) {
+ const videos = await getVideosFor(name)
+ if (videos) repo.saveChannelVideos(name, videos)
+ console.log('videos for channel updated', name)
+}
+
async function downloadVideo(id, repo) {
try {
await execa({stdio:'inherit'})`yt-dlp -f mp4 ${id} -o ./data/videos/${id}.mp4`
@@ -129,7 +135,7 @@ async function getVideosFor(channelName) {
description: v.richItemRenderer.content.videoRenderer.descriptionSnippet?.runs[0].text,
id: v.richItemRenderer.content.videoRenderer.videoId,
publishedTime: v.richItemRenderer.content.videoRenderer.publishedTimeText?.simpleText,
- viewCount: v.richItemRenderer.content.videoRenderer.viewCountText.simpleText,
+ viewCount: v.richItemRenderer.content.videoRenderer.viewCountText?.simpleText,
duration: v.richItemRenderer.content.videoRenderer.lengthText ? v.richItemRenderer.content.videoRenderer.lengthText.simpleText : null,
}
}
@@ -153,7 +159,7 @@ function createServer ({repo, port = 3000}) {
name = name.trim()
console.log('adding channel', name)
repo.addChannel(name)
- await updateAndPersistVideos(repo)
+ await updateAndPersistVideosForChannel(repo, name)
res.writeHead(201, { 'Content-Type': 'text/plain' })
res.end('Channel added')
})