ubertuber-frontend/script.js

50 lines
2 KiB
JavaScript
Raw Normal View History

2024-10-28 15:36:29 +00:00
document.getElementById('search-button').addEventListener('click', async () => {
const query = document.getElementById('search-input').value;
const resultsList = document.getElementById('video-results');
resultsList.innerHTML = ''; // Clear previous results
if (!query) {
alert('Please enter a search query.');
return;
}
try {
const response = await fetch(`https://ubertuberbe.nodemixaholic.com/search?q=${encodeURIComponent(query)}`);
const videos = await response.json();
videos.forEach(video => {
const listItem = document.createElement('li');
listItem.innerHTML = `
<strong>${video.title}</strong> (${video.views} views) by ${video.author}
<br>
2024-10-28 16:52:50 +00:00
<a href="#" class="download-link" data-url="${video.url}">Watch</a>
2024-10-28 15:36:29 +00:00
`;
resultsList.appendChild(listItem);
});
// Add event listeners to download links
document.querySelectorAll('.download-link').forEach(link => {
link.addEventListener('click', async (e) => {
e.preventDefault();
const videoUrl = e.target.dataset.url;
const downloadResponse = await fetch(`https://ubertuberbe.nodemixaholic.com/download?url=${encodeURIComponent(videoUrl)}`);
2024-10-28 16:52:50 +00:00
//const blob = await downloadResponse.blob();
2024-10-28 15:36:29 +00:00
2024-10-28 16:52:50 +00:00
//const url = window.URL.createObjectURL(blob);
2024-10-28 15:36:29 +00:00
const a = document.createElement('a');
a.style.display = 'none';
2024-10-28 16:57:09 +00:00
a.href = downloadResponse;
2024-10-28 16:52:50 +00:00
//a.download = 'video.mp4'; // You might want to set this dynamically
2024-10-28 15:36:29 +00:00
document.body.appendChild(a);
2024-10-28 16:57:09 +00:00
//a.click();
//window.URL.revokeObjectURL(url);
2024-10-28 15:36:29 +00:00
});
});
} catch (error) {
console.error('Error fetching videos:', error);
alert('An error occurred while searching for videos.');
}
});