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ventListener('DOMContentLoaded', () => {
|
| 2 |
+
// DOM Elements
|
| 3 |
+
const videoList = document.getElementById('videoList');
|
| 4 |
+
const videoPlayer = document.getElementById('videoPlayer');
|
| 5 |
+
const currentVideoTitle = document.getElementById('currentVideoTitle');
|
| 6 |
+
const noVideoMessage = document.getElementById('noVideoMessage');
|
| 7 |
+
const searchInput = document.getElementById('searchInput');
|
| 8 |
+
const searchButton = document.getElementById('searchButton');
|
| 9 |
+
const fullscreenBtn = document.getElementById('fullscreenBtn');
|
| 10 |
+
|
| 11 |
+
// State
|
| 12 |
+
let videos = [];
|
| 13 |
+
let currentVideoIndex = -1;
|
| 14 |
+
|
| 15 |
+
// Fetch video data from JSON file
|
| 16 |
+
async function fetchVideos() {
|
| 17 |
+
try {
|
| 18 |
+
const response = await fetch('database.json');
|
| 19 |
+
if (!response.ok) {
|
| 20 |
+
throw new Error('Failed to load video database');
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
videos = await response.json();
|
| 24 |
+
renderVideoList(videos);
|
| 25 |
+
} catch (error) {
|
| 26 |
+
console.error('Error loading videos:', error);
|
| 27 |
+
videoList.innerHTML = `
|
| 28 |
+
<div class="error-message">
|
| 29 |
+
<p>Failed to load videos. Please try again later.</p>
|
| 30 |
+
</div>
|
| 31 |
+
`;
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
// Render video list
|
| 36 |
+
function renderVideoList(videosToRender) {
|
| 37 |
+
// Clear loading state
|
| 38 |
+
videoList.innerHTML = '';
|
| 39 |
+
|
| 40 |
+
if (videosToRender.length === 0) {
|
| 41 |
+
videoList.innerHTML = `
|
| 42 |
+
<div class="error-message">
|
| 43 |
+
<p>No videos found</p>
|
| 44 |
+
</div>
|
| 45 |
+
`;
|
| 46 |
+
return;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
// Create video list items
|
| 50 |
+
videosToRender.forEach((video, index) => {
|
| 51 |
+
const videoItem = document.createElement('div');
|
| 52 |
+
videoItem.className = 'video-item';
|
| 53 |
+
videoItem.innerHTML = `
|
| 54 |
+
<h3>${video.title}</h3>
|
| 55 |
+
`;
|
| 56 |
+
|
| 57 |
+
videoItem.addEventListener('click', () => {
|
| 58 |
+
playVideo(index);
|
| 59 |
+
});
|
| 60 |
+
|
| 61 |
+
videoList.appendChild(videoItem);
|
| 62 |
+
});
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
// Play selected video
|
| 66 |
+
function playVideo(index) {
|
| 67 |
+
if (index < 0 || index >= videos.length) return;
|
| 68 |
+
|
| 69 |
+
// Update UI
|
| 70 |
+
const videoItems = document.querySelectorAll('.video-item');
|
| 71 |
+
videoItems.forEach(item => item.classList.remove('active'));
|
| 72 |
+
videoItems[index].classList.add('active');
|
| 73 |
+
|
| 74 |
+
// Update video source and title
|
| 75 |
+
const video = videos[index];
|
| 76 |
+
videoPlayer.src = video.url;
|
| 77 |
+
currentVideoTitle.textContent = video.title;
|
| 78 |
+
|
| 79 |
+
// Show video player and hide placeholder
|
| 80 |
+
noVideoMessage.style.display = 'none';
|
| 81 |
+
videoPlayer.style.display = 'block';
|
| 82 |
+
|
| 83 |
+
// Play video
|
| 84 |
+
videoPlayer.load();
|
| 85 |
+
videoPlayer.play()
|
| 86 |
+
.catch(error => {
|
| 87 |
+
console.error('Failed to play video:', error);
|
| 88 |
+
// Handle formats that might not be supported
|
| 89 |
+
if (video.url.toLowerCase().endsWith('.mkv')) {
|
| 90 |
+
alert('MKV format may not be supported in your browser. Consider using MP4 files for better compatibility.');
|
| 91 |
+
}
|
| 92 |
+
});
|
| 93 |
+
|
| 94 |
+
currentVideoIndex = index;
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
// Search functionality
|
| 98 |
+
function searchVideos() {
|
| 99 |
+
const searchTerm = searchInput.value.toLowerCase().trim();
|
| 100 |
+
|
| 101 |
+
if (!searchTerm) {
|
| 102 |
+
renderVideoList(videos);
|
| 103 |
+
return;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
const filteredVideos = videos.filter(video =>
|
| 107 |
+
video.title.toLowerCase().includes(searchTerm)
|
| 108 |
+
);
|
| 109 |
+
|
| 110 |
+
renderVideoList(filteredVideos);
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
// Toggle fullscreen
|
| 114 |
+
function toggleFullscreen() {
|
| 115 |
+
if (!document.fullscreenElement) {
|
| 116 |
+
if (videoPlayer.requestFullscreen) {
|
| 117 |
+
videoPlayer.requestFullscreen();
|
| 118 |
+
} else if (videoPlayer.webkitRequestFullscreen) { /* Safari */
|
| 119 |
+
videoPlayer.webkitRequestFullscreen();
|
| 120 |
+
} else if (videoPlayer.msRequestFullscreen) { /* IE11 */
|
| 121 |
+
videoPlayer.msRequestFullscreen();
|
| 122 |
+
}
|
| 123 |
+
} else {
|
| 124 |
+
if (document.exitFullscreen) {
|
| 125 |
+
document.exitFullscreen();
|
| 126 |
+
} else if (document.webkitExitFullscreen) { /* Safari */
|
| 127 |
+
document.webkitExitFullscreen();
|
| 128 |
+
} else if (document.msExitFullscreen) { /* IE11 */
|
| 129 |
+
document.msExitFullscreen();
|
| 130 |
+
}
|
| 131 |
+
}
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
// Handle video ended event
|
| 135 |
+
videoPlayer.addEventListener('ended', () => {
|
| 136 |
+
// Optionally auto-play next video
|
| 137 |
+
if (currentVideoIndex < videos.length - 1) {
|
| 138 |
+
playVideo(currentVideoIndex + 1);
|
| 139 |
+
}
|
| 140 |
+
});
|
| 141 |
+
|
| 142 |
+
// Handle video error
|
| 143 |
+
videoPlayer.addEventListener('error', () => {
|
| 144 |
+
console.error('Video error:', videoPlayer.error);
|
| 145 |
+
alert('Error playing video. This could be due to format incompatibility or access issues.');
|
| 146 |
+
});
|
| 147 |
+
|
| 148 |
+
// Event listeners
|
| 149 |
+
searchButton.addEventListener('click', searchVideos);
|
| 150 |
+
searchInput.addEventListener('keyup', (e) => {
|
| 151 |
+
if (e.key === 'Enter') {
|
| 152 |
+
searchVideos();
|
| 153 |
+
}
|
| 154 |
+
});
|
| 155 |
+
fullscreenBtn.addEventListener('click', toggleFullscreen);
|
| 156 |
+
|
| 157 |
+
// Initial load
|
| 158 |
+
fetchVideos();
|
| 159 |
+
});
|