// Fetch EPG Data async function fetchEPG() { try { const response = await fetch('https://epg.guideus.tv'); const data = await response.json(); renderEPG(data); } catch (error) { console.error('Error fetching EPG:', error); document.getElementById('epg-container').innerHTML = `

Failed to load program guide. Please try again later.

`; feather.replace(); } } // Render EPG Data function renderEPG(data) { const epgContainer = document.getElementById('epg-container'); if (!data || !data.length) { epgContainer.innerHTML = `

No program information available at this time.

`; feather.replace(); return; } let html = `
`; data.forEach((program, index) => { html += ` `; }); html += `
Time Program Description
${program.time} ${program.title} ${program.description || 'No description available'}
`; epgContainer.innerHTML = html; } // Fetch Recent Videos from RSS feed async function fetchRecentVideos() { try { const response = await fetch('https://api.rss2json.com/v1/api.json?rss_url=https%3A%2F%2Fguideus.tv%2Ffeed%2F'); const data = await response.json(); // Transform RSS feed items to match our expected format const videos = data.items.map(item => { // Extract first image from content if available let thumbnail = 'http://static.photos/technology/640x360'; if (item.enclosure && item.enclosure.link) { thumbnail = item.enclosure.link; } else if (item.description) { const imgMatch = item.description.match(/]+src="([^">]+)"/); if (imgMatch && imgMatch[1]) { thumbnail = imgMatch[1]; } } // Clean up description let description = item.description || 'No description available'; description = description.replace(/<[^>]*>?/gm, '').substring(0, 100) + '...'; // Extract video ID if YouTube link let videoId = null; if (item.link && item.link.includes('youtube.com')) { const youtubeMatch = item.link.match(/[?&]v=([^&]+)/); videoId = youtubeMatch ? youtubeMatch[1] : null; } return { title: item.title, description: description, date: new Date(item.pubDate).toLocaleDateString(), thumbnail: thumbnail, link: item.link, videoId: videoId }; }); renderVideos(videos); } catch (error) { console.error('Error fetching videos:', error); document.getElementById('videos-container').innerHTML = `

Failed to load recent broadcasts. Please try again later.

`; feather.replace(); } } // Render Recent Videos function renderVideos(videos) { const videosContainer = document.getElementById('videos-container'); if (!videos || !videos.length) { videosContainer.innerHTML = `

No recent broadcasts available at this time.

`; feather.replace(); return; } let html = ''; videos.forEach(video => { html += `
${video.videoId ? ` ` : ` ${video.title} `}

${video.title}

${video.date || ''}

${video.description.replace(/<[^>]*>?/gm, '') || ''}

`; }); videosContainer.innerHTML = html; feather.replace(); } // Initialize the app document.addEventListener('DOMContentLoaded', () => { // Fetch data after splash screen hides setTimeout(() => { fetchEPG(); fetchRecentVideos(); }, 2000); // Register service worker for PWA // Mobile menu toggle document.addEventListener('click', (e) => { if (e.target.closest('#mobile-menu-button')) { const menu = document.querySelector('custom-navbar').shadowRoot.getElementById('mobile-menu'); menu.classList.toggle('hidden'); feather.replace(); } }); if ('serviceWorker' in navigator) { window.addEventListener('load', () => { navigator.serviceWorker.register('/sw.js').then(registration => { console.log('ServiceWorker registration successful'); }).catch(err => { console.log('ServiceWorker registration failed: ', err); }); }); } });