-
Notifications
You must be signed in to change notification settings - Fork 3
/
anime-details.js
140 lines (116 loc) · 4.82 KB
/
anime-details.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
document.addEventListener('DOMContentLoaded', async () => {
let animeId;
const selectedAnime = JSON.parse(sessionStorage.getItem('selectedAnime'));
if (selectedAnime) {
animeId = selectedAnime.id;
} else {
const urlParams = new URLSearchParams(window.location.search);
animeId = urlParams.get('animeId');
}
if (animeId) {
const primaryApiUrl = `https://api-consumet-org-six.vercel.app/anime/gogoanime/info/${animeId}`;
const fallbackUrl = `https://api-consumet-psi.vercel.app/anime/gogoanime/info/${animeId}`;
try {
const response = await fetch(primaryApiUrl);
if (!response.ok) {
throw new Error('Error fetching data from primary API:', response.statusText);
}
const data = await response.json();
await displayDetails(data);
} catch (primaryApiError) {
console.error('Error:', primaryApiError);
console.log('Fetching data from fallback API...');
try {
const fallbackResponse = await fetch(fallbackUrl);
if (!fallbackResponse.ok) {
throw new Error('Error fetching data from fallback API:', fallbackResponse.statusText);
}
const fallbackData = await fallbackResponse.json();
await displayDetails(fallbackData);
} catch (fallbackApiError) {
console.error('Error:', fallbackApiError);
}
}
} else {
window.location.href = 'index.html';
}
});
async function displayDetails(anime) {
const title = document.getElementById('anime-title');
const image = document.getElementById('anime-image');
const description = document.getElementById('anime-description');
const episodesList = document.getElementById('anime-episodes');
const genres = document.getElementById('anime-genres');
const episodeDropdown = document.getElementById('episode-dropdown');
title.textContent = anime.title;
image.src = anime.image;
image.alt = anime.title;
description.textContent = anime.description;
genres.innerHTML = '';
episodesList.innerHTML = '';
for (const genre of anime.genres) {
const genreItem = document.createElement('span');
genreItem.textContent = genre;
genres.appendChild(genreItem);
}
populateEpisodeDropdown(anime.episodes);
const defaultRange = episodeDropdown.options[0].value;
const [startEpisode, endEpisode] = defaultRange.split('-');
await displayEpisodes(anime.episodes, parseInt(startEpisode), parseInt(endEpisode));
episodeDropdown.addEventListener('change', async (event) => {
const selectedRange = event.target.value.split('-');
const startEpisode = parseInt(selectedRange[0]);
const endEpisode = parseInt(selectedRange[1]);
await displayEpisodes(anime.episodes, startEpisode, endEpisode);
});
}
async function displayEpisodes(allEpisodes, startEpisode, endEpisode) {
const episodesList = document.getElementById('anime-episodes');
episodesList.innerHTML = '';
for (let i = startEpisode - 1; i < endEpisode; i++) {
const episode = allEpisodes[i];
const listItem = document.createElement('li');
const link = document.createElement('a');
link.textContent = `Episode ${episode.number}`;
link.tabIndex = 0; // Make the link focusable
link.href = `javascript:void(0);`; // Prevent default navigation
link.dataset.episodeNumber = episode.number;
link.dataset.episodeId = episode.id;
link.classList.add('episode');
// Handle click and Enter key press
link.addEventListener('click', navigateToEpisode);
link.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
navigateToEpisode(event);
}
});
listItem.appendChild(link);
episodesList.appendChild(listItem);
}
}
function navigateToEpisode(event) {
const episodeNumber = event.target.dataset.episodeNumber;
const episodeId = event.target.dataset.episodeId;
window.location.href = `video-player.html?episodeNumber=${episodeNumber}&episodeId=${episodeId}`;
}
function populateEpisodeDropdown(allEpisodes) {
const episodeDropdown = document.getElementById('episode-dropdown');
episodeDropdown.tabIndex = 0;
const maxEpisodesPerRange = 100;
let startEpisode = 1;
episodeDropdown.innerHTML = '';
while (startEpisode <= allEpisodes.length) {
const endEpisode = Math.min(startEpisode + maxEpisodesPerRange - 1, allEpisodes.length);
const option = document.createElement('option');
option.value = `${startEpisode}-${endEpisode}`;
option.textContent = `${startEpisode}-${endEpisode}`;
episodeDropdown.appendChild(option);
startEpisode += maxEpisodesPerRange;
}
if (allEpisodes.length > 1) {
episodeDropdown.tabIndex = 0;
} else {
// If there is only one option, no need to focus on the dropdown
episodeDropdown.tabIndex = -1;
}
}