Prompt
Create a single HTML file for a YouTube Thumbnail Downloader web application using only HTML, CSS, and JavaScript, without relying on any external APIs or libraries. The application should include the following features:
1. **Multiple Thumbnail Quality Options**: Allow users to select thumbnail resolutions (Max Resolution: maxresdefault, High Quality: hqdefault, Standard Quality: sddefault, Default: default) via a dropdown menu. If the selected quality is unavailable, automatically fall back to the next available lower quality.
2. **Loading Spinner**: Display a loading animation while the thumbnail is being fetched, hiding it when the thumbnail loads or an error occurs.
3. **Copy Thumbnail URL to Clipboard**: Include a button to copy the thumbnail URL to the clipboard using the navigator.clipboard API.
4. **Clear Input Button**: Provide a button to clear the input field and reset the UI (hide thumbnail, download link, copy button, error message, and loading spinner).
5. **Dark Mode Toggle**: Add a button to toggle between light and dark themes, affecting the background, text, and input/button styles.
6. **Support for Shorts URLs**: Handle various YouTube URL formats, including standard URLs (e.g., youtube.com/watch?v=VIDEO_ID, youtu.be/VIDEO_ID) and Shorts URLs (e.g., youtube.com/shorts/VIDEO_ID).
Additional requirements:
- The application should be contained in a single HTML file with embedded CSS (in a <style> tag) and JavaScript (in a <script> tag).
- Use YouTube’s public thumbnail URLs (e.g., https://img.youtube.com/vi/VIDEO_ID/maxresdefault.jpg) to fetch thumbnails.
- Include input validation to display an error message for invalid URLs.
- Support keyboard input (e.g., pressing Enter to fetch the thumbnail).
- Ensure the design is responsive, looking good on both desktop and mobile devices.
- Style the app with a clean, modern look, including hover effects for buttons and a centered layout.
- Use a red theme for the primary button (e.g., "Get Thumbnail") to match YouTube’s branding.
- Provide a download link to save the thumbnail as a JPG file.
- Ensure all features work without external dependencies or API keys.
The output should be a complete, functional HTML file that I can save and run locally in a browser. Include comments in the code to explain key sections. Provide only the code, without additional explanations, unless I request them.
Codes part 1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>YouTube Thumbnail Downloader</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
transition: background-color 0.3s, color 0.3s;
}
.container {
text-align: center;
background-color: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
max-width: 600px;
width: 90%;
transition: background-color 0.3s, box-shadow 0.3s;
}
h1 {
color: #333;
margin-bottom: 20px;
}
input {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 16px;
transition: background-color 0.3s, color 0.3s, border-color 0.3s;
}
button {
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
margin: 5px;
}
#getThumbnailBtn {
background-color: #ff0000;
color: white;
}
#getThumbnailBtn:hover {
background-color: #cc0000;
}
#clearBtn {
background-color: #6c757d;
}
#clearBtn:hover {
background-color: #5a6268;
}
#toggleThemeBtn {
background-color: #007bff;
color: white;
}
#toggleThemeBtn:hover {
background-color: #0056b3;
}
#copyLink {
background-color: #17a2b8;
color: white;
display: none;
}
#copyLink:hover {
background-color: #138496;
}
.thumbnail-container {
margin-top: 20px;
}
select {
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 16px;
transition: background-color 0.3s, border-color 0.3s;
}
#thumbnail {
max-width: 100%;
border-radius: 5px;
margin-bottom: 10px;
display: none;
}
#downloadLink {
display: none;
padding: 10px 20px;
background-color: #28a745;
color: white;
text-decoration: none;
border-radius: 5px;
margin-top: 10px;
}
#downloadLink:hover {
background-color: #218838;
}
.error {
color: red;
margin-top: 10px;
display: none;
}
.loading {
font-size: 16px;
color: #333;
margin-top: 10px;
display: none;
}
.loading::after {
content: '';
display: inline-block;
width: 16px;
height: 16px;
border: 2px solid #ff0000;
border-radius: 50%;
border-top-color: transparent;
animation: spin 1s linear infinite;
margin-left: 10px;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
.dark-mode {
background-color: #333;
color: #fff;
}
.dark-mode .container {
background-color: #444;
box-shadow: 0 0 10px rgba(255, 255, 255, 0.1);
}
.dark-mode input, .dark-mode select {
background-color: #555;
color: #fff;
border-color: #666;
}
.dark-mode h1, .dark-mode .loading {
color: #fff;
}
@media (max-width: 600px) {
.container {
padding: 15px;
}
input, button, select, #downloadLink {
font-size: 14px;
}
}
</style>
</head>
<body>
<div class="container">
<h1>YouTube Thumbnail Downloader</h1>
<input type="text" id="videoUrl" placeholder="Enter YouTube Video URL">
<div>
<button id="getThumbnailBtn" onclick="getThumbnail()">Get Thumbnail</button>
<button id="clearBtn" onclick="clearInput()">Clear</button>
<button id="toggleThemeBtn" onclick="toggleTheme()">Toggle Dark Mode</button>
</div>
<div id="thumbnailContainer" class="thumbnail-container">
<select id="qualitySelect">
<option value="maxresdefault">Max Resolution</option>
<option value="hqdefault">High Quality</option>
<option value="sddefault">Standard Quality</option>
<option value="default">Default Quality</option>
</select>
<div id="loading" class="loading">Loading...</div>
<img id="thumbnail" src="" alt="Thumbnail Preview">
<a id="downloadLink" href="#" download>Download Thumbnail</a>
<button id="copyLink">Copy Thumbnail URL</button>
<p id="error" class="error"></p>
</div>
</div>
Codes Part 2
<script>
function getThumbnail() {
const urlInput = document.getElementById('videoUrl').value;
const quality = document.getElementById('qualitySelect').value;
const thumbnailImg = document.getElementById('thumbnail');
const downloadLink = document.getElementById('downloadLink');
const copyLink = document.getElementById('copyLink');
const errorMsg = document.getElementById('error');
const loading = document.getElementById('loading');
// Reset UI
thumbnailImg.style.display = 'none';
downloadLink.style.display = 'none';
copyLink.style.display = 'none';
errorMsg.style.display = 'none';
loading.style.display = 'block';
// Regular expression to extract video ID, including Shorts URLs
const regex = /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|shorts\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})/;
const match = urlInput.match(regex);
if (match && match[1]) {
const videoId = match[1];
const qualities = ['maxresdefault', 'hqdefault', 'sddefault', 'default'];
let currentQualityIndex = qualities.indexOf(quality);
function tryThumbnail(qualityIndex) {
const thumbnailUrl = `https://img.youtube.com/vi/${videoId}/${qualities[qualityIndex]}.jpg`;
thumbnailImg.src = thumbnailUrl;
thumbnailImg.onload = () => {
thumbnailImg.style.display = 'block';
downloadLink.href = thumbnailUrl;
downloadLink.style.display = 'inline-block';
downloadLink.download = `${videoId}_${qualities[qualityIndex]}.jpg`;
copyLink.style.display = 'inline-block';
loading.style.display = 'none';
};
thumbnailImg.onerror = () => {
if (qualityIndex < qualities.length - 1) {
tryThumbnail(qualityIndex + 1); // Try next quality
} else {
errorMsg.textContent = 'No thumbnail available for this video.';
errorMsg.style.display = 'block';
loading.style.display = 'none';
}
};
}
tryThumbnail(currentQualityIndex);
} else {
errorMsg.textContent = 'Invalid YouTube URL. Please try again.';
errorMsg.style.display = 'block';
loading.style.display = 'none';
}
}
function clearInput() {
document.getElementById('videoUrl').value = '';
document.getElementById('thumbnail').style.display = 'none';
document.getElementById('downloadLink').style.display = 'none';
document.getElementById('copyLink').style.display = 'none';
document.getElementById('error').style.display = 'none';
document.getElementById('loading').style.display = 'none';
}
function toggleTheme() {
document.body.classList.toggle('dark-mode');
}
document.getElementById('copyLink').addEventListener('click', () => {
const thumbnailUrl = document.getElementById('thumbnail').src;
navigator.clipboard.writeText(thumbnailUrl).then(() => {
alert('Thumbnail URL copied to clipboard!');
}).catch(() => {
alert('Failed to copy URL. Please try again.');
});
});
// Add keyboard support for Enter key
document.getElementById('videoUrl').addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
getThumbnail();
}
});
</script>
</body>
</html>