/** * Main initialization and event handlers */ // Initialize on DOM load document.addEventListener('DOMContentLoaded', () => { initializeForms(); loadProviders(); loadDownloads(); setInterval(loadDownloads, 1000); // Load home content (recommendations & releases) loadHomeContent(); }); /** * Initialize form event listeners */ function initializeForms() { // Search form document.getElementById('searchInput').addEventListener('keypress', (e) => { if (e.key === 'Enter') { handleSearch(); } }); // Direct download form document.getElementById('downloadForm').addEventListener('submit', handleDirectDownload); } /** * Load providers dynamically */ async function loadProviders() { try { const data = await getProvidersInfo(); // Update anime tabs const animeTabsContainer = document.querySelector('.tabs'); const existingTabs = animeTabsContainer.querySelectorAll('.tab[data-tab-type="anime"]'); existingTabs.forEach(tab => tab.remove()); // Add anime provider tabs Object.entries(data.anime_providers).forEach(([id, provider]) => { // Check if tab doesn't exist if (!document.querySelector(`.tab[data-provider="${id}"]`)) { const button = document.createElement('button'); button.className = 'tab'; button.setAttribute('data-tab-type', 'anime'); button.setAttribute('data-provider', id); button.innerHTML = `${provider.icon} ${provider.name}`; button.onclick = () => switchTab(`anime-${id}`); animeTabsContainer.appendChild(button); // Create corresponding tab content const tabContent = document.createElement('div'); tabContent.id = `tab-anime-${id}`; tabContent.className = 'tab-content'; tabContent.innerHTML = createAnimeTabContent(id, provider); document.querySelector('.container').insertBefore( tabContent, document.getElementById('downloadsList') ); } }); // Update supported hosts badges const hostsContainer = document.querySelector('.supported-hosts'); hostsContainer.innerHTML = ''; Object.values(data.file_hosts).forEach(host => { const badge = document.createElement('span'); badge.className = 'host-badge'; badge.textContent = `${host.icon} ${host.name}`; hostsContainer.appendChild(badge); }); } catch (error) { console.error('Error loading providers:', error); } } /** * Create anime provider tab content */ function createAnimeTabContent(providerId, provider) { return `
`; } /** * Handle load provider episodes */ async function handleLoadProviderEpisodes(providerId) { const animeUrl = document.getElementById(`${providerId}UrlInput`).value; if (!animeUrl) { alert('Veuillez entrer une URL d\'anime'); return; } try { const data = await loadEpisodes(animeUrl, null); if (data.episodes && data.episodes.length > 0) { const select = document.getElementById(`${providerId}EpisodeSelect`); select.innerHTML = ''; data.episodes.forEach(ep => { const option = document.createElement('option'); option.value = ep.url; option.textContent = `Épisode ${ep.episode}`; select.appendChild(option); }); document.getElementById(`${providerId}EpisodeSelector`).style.display = 'flex'; } else { alert('Aucun épisode trouvé'); } } catch (error) { console.error('Error loading episodes:', error); alert('Erreur lors du chargement des épisodes'); } } /** * Handle download provider episode */ async function handleDownloadProviderEpisode(providerId) { const episodeUrl = document.getElementById(`${providerId}EpisodeSelect`).value; if (!episodeUrl) { alert('Veuillez sélectionner un épisode'); return; } try { await downloadEpisode(episodeUrl); document.getElementById(`${providerId}EpisodeSelect`).value = ''; loadDownloads(); } catch (error) { console.error('Download error:', error); alert('Erreur lors du téléchargement'); } } /** * Switch between tabs */ function switchTab(tabName) { // Hide all tabs document.querySelectorAll('.tab-content').forEach(tab => { tab.classList.remove('active'); }); document.querySelectorAll('.tab').forEach(btn => { btn.classList.remove('active'); }); // Show selected tab const tabElement = document.getElementById(`tab-${tabName}`); if (tabElement) { tabElement.classList.add('active'); } // Find and activate the button const buttons = document.querySelectorAll('.tab'); buttons.forEach(btn => { const tabType = btn.getAttribute('data-tab-type'); if (tabType === 'home' && tabName === 'home') { btn.classList.add('active'); } else if (tabType === 'search' && tabName === 'search') { btn.classList.add('active'); } else if (tabType === 'direct' && tabName === 'direct') { btn.classList.add('active'); } else if (tabType === 'anime' && btn.getAttribute('data-provider') === tabName.replace('anime-', '')) { btn.classList.add('active'); } }); // Load home content when switching to home tab if (tabName === 'home') { // Content is already loaded on init, but you can reload if needed if (typeof loadHomeContent === 'function' && !document.getElementById('recommendationsList').hasChildNodes()) { loadHomeContent(); } } }