let videoDetails = {};
let comments = [];
let allVideos = [];
async function loadVideoData() {
try {
const response = await fetch('src/video.json');
const data = await response.json();
videoDetails = {};
allVideos = data.videos;
data.videos.forEach(video => {
videoDetails[video.id] = video;
});
comments = data.comments;
console.log('Video data loaded successfully:', allVideos.length, 'videos');
} catch (error) {
console.error('Failed to load video data:', error);
}
}
function getVideoIdFromUrl() {
const urlParams = new URLSearchParams(window.location.search);
const id = urlParams.get('id');
return id ? parseInt(id) : 1;
}
function loadVideoInfo() {
const videoId = getVideoIdFromUrl();
console.log('Video ID:', videoId);
console.log('All videos:', allVideos.length);
const videoInfo = videoDetails[videoId];
console.log('Video info:', videoInfo);
if (videoInfo) {
document.getElementById('videoTitle').textContent = videoInfo.title;
document.getElementById('channelName').textContent = videoInfo.channel;
document.getElementById('channelSubscribers').textContent = videoInfo.subscribers;
document.getElementById('likeCount').textContent = videoInfo.likes;
document.getElementById('videoViews').textContent = videoInfo.views + ' views';
document.getElementById('videoUploadDate').textContent = videoInfo.uploadDate;
document.getElementById('videoDescription').textContent = videoInfo.description;
const channelAvatar = document.getElementById('channelAvatar');
if (videoInfo.channelAvatar && videoInfo.channelAvatar !== 'img/avatar/default.jpg') {
channelAvatar.innerHTML = ``;
}
document.title = videoInfo.title + ' - YouTube Revived';
loadRelatedVideos(videoInfo);
}
renderComments();
}
function loadRelatedVideos(currentVideo) {
console.log('Loading related videos for:', currentVideo.title);
console.log('Current category:', currentVideo.category);
const currentCategory = currentVideo.category;
const currentId = currentVideo.id;
const shortsList = document.getElementById('shortsList');
const regularVideosList = document.getElementById('regularVideosList');
console.log('Shorts list element:', shortsList);
console.log('Regular videos list element:', regularVideosList);
shortsList.innerHTML = '';
regularVideosList.innerHTML = '';
const shorts = allVideos.filter(v => v.type === 'short' && v.id !== currentId);
const regularVideos = allVideos.filter(v => v.type === 'video' && v.id !== currentId);
console.log('Found shorts:', shorts.length);
console.log('Found regular videos:', regularVideos.length);
const sameCategoryShorts = shorts.filter(v => v.category === currentCategory);
const otherShorts = shorts.filter(v => v.category !== currentCategory);
const sameCategoryRegularVideos = regularVideos.filter(v => v.category === currentCategory);
const otherRegularVideos = regularVideos.filter(v => v.category !== currentCategory);
console.log('Same category shorts:', sameCategoryShorts.length);
console.log('Other shorts:', otherShorts.length);
console.log('Same category regular videos:', sameCategoryRegularVideos.length);
console.log('Other regular videos:', otherRegularVideos.length);
shuffleArray(otherShorts);
shuffleArray(otherRegularVideos);
const selectedShorts = [...sameCategoryShorts, ...otherShorts].slice(0, 3);
// Calculate needed regular videos based on page height
const calculateNeededVideos = () => {
const windowHeight = window.innerHeight;
const relatedSection = document.querySelector('.related-videos-section');
const shortsSection = document.querySelector('.shorts-section');
if (!relatedSection || !shortsSection) {
return 10; // Default to 10 if elements not found
}
const shortsHeight = shortsSection.offsetHeight;
const availableHeight = windowHeight - shortsHeight - 48; // 48px for padding/gap
// Estimate height per regular video item (including gap)
const estimatedVideoHeight = 100; // Average height of each video item
// Calculate needed videos
const neededVideos = Math.ceil(availableHeight / estimatedVideoHeight);
// Ensure we don't exceed available videos
const totalAvailableVideos = sameCategoryRegularVideos.length + otherRegularVideos.length;
return Math.min(neededVideos, totalAvailableVideos);
};
const neededVideosCount = calculateNeededVideos();
const selectedRegularVideos = [...sameCategoryRegularVideos, ...otherRegularVideos].slice(0, neededVideosCount);
console.log('Selected shorts:', selectedShorts.length);
console.log('Selected regular videos:', selectedRegularVideos.length, '(needed:', neededVideosCount, ')');
console.log('Window height:', window.innerHeight);
selectedShorts.forEach(short => {
const shortElement = document.createElement('div');
shortElement.className = 'short-item';
shortElement.innerHTML = `
${short.channel}
${short.views} views
${video.channel}
${comment.content}