-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter-script.js
134 lines (113 loc) · 5.3 KB
/
chapter-script.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
// Dark Mode Toggle
const darkModeToggle = document.getElementById('dark-mode-toggle');
const body = document.body;
// Load dark mode preference from localStorage
const savedTheme = localStorage.getItem('theme');
if (savedTheme) {
body.dataset.theme = savedTheme;
darkModeToggle.textContent = savedTheme === 'dark' ? '☀️ Light Mode' : '🌙 Dark Mode';
} else {
darkModeToggle.textContent = '🌙 Dark Mode'; // Default text
}
darkModeToggle.addEventListener('click', () => {
const newTheme = body.dataset.theme === 'dark' ? 'light' : 'dark';
body.dataset.theme = newTheme;
darkModeToggle.textContent = newTheme === 'dark' ? '☀️ Light Mode' : '🌙 Dark Mode';
localStorage.setItem('theme', newTheme); // Save preference
});
// Track read chapters
const readChapters = JSON.parse(localStorage.getItem('readChapters')) || [];
// Get chapter ID from URL
const urlParams = new URLSearchParams(window.location.search);
const chapterId = urlParams.get('chapter');
// Mark the chapter as read
if (chapterId && !readChapters.includes(chapterId)) {
readChapters.push(chapterId);
localStorage.setItem('readChapters', JSON.stringify(readChapters));
}
// Fetch chapter pages
async function fetchChapterPages(chapterId) {
const apiUrl = `https://api.allorigins.win/get?url=${encodeURIComponent(
`https://api.mangadex.org/at-home/server/${chapterId}`
)}`;
try {
const response = await fetch(apiUrl);
const data = await response.json();
const chapterData = JSON.parse(data.contents); // Parse the API response
const pages = chapterData.chapter.data; // Array of page filenames
// Display pages
const pagesContainer = document.getElementById('pages');
pagesContainer.innerHTML = ''; // Clear previous pages
pages.forEach((page, index) => {
const img = document.createElement('img');
img.src = `${chapterData.baseUrl}/data/${chapterData.chapter.hash}/${page}`;
img.alt = `Page ${index + 1}`;
pagesContainer.appendChild(img);
});
} catch (error) {
console.error('Error fetching chapter pages:', error);
}
}
fetchChapterPages(chapterId);
// Navigation functionality
const prevChapterButton = document.getElementById('prev-chapter');
const nextChapterButton = document.getElementById('next-chapter');
const chapterSelect = document.getElementById('chapter-select');
const prevChapterButtonBottom = document.getElementById('prev-chapter-bottom');
const nextChapterButtonBottom = document.getElementById('next-chapter-bottom');
const chapterSelectBottom = document.getElementById('chapter-select-bottom');
// Add event listeners for navigation
prevChapterButton.addEventListener('click', goToPreviousChapter);
nextChapterButton.addEventListener('click', goToNextChapter);
prevChapterButtonBottom.addEventListener('click', goToPreviousChapter);
nextChapterButtonBottom.addEventListener('click', goToNextChapter);
chapterSelect.addEventListener('change', (e) => {
window.location.href = `chapter.html?chapter=${e.target.value}`;
});
chapterSelectBottom.addEventListener('change', (e) => {
window.location.href = `chapter.html?chapter=${e.target.value}`;
});
// Function to go to the previous chapter
function goToPreviousChapter() {
const currentChapter = chapterSelect.value;
const chapters = Array.from(chapterSelect.options);
const currentIndex = chapters.findIndex(option => option.value === currentChapter);
if (currentIndex > 0) {
window.location.href = `chapter.html?chapter=${chapters[currentIndex - 1].value}`;
}
}
// Function to go to the next chapter
function goToNextChapter() {
const currentChapter = chapterSelect.value;
const chapters = Array.from(chapterSelect.options);
const currentIndex = chapters.findIndex(option => option.value === currentChapter);
if (currentIndex < chapters.length - 1) {
window.location.href = `chapter.html?chapter=${chapters[currentIndex + 1].value}`;
}
}
// Populate chapter select dropdown
async function populateChapterSelect() {
const mangaId = 'b0b721ff-c388-4486-aa0f-c2b0bb321512'; // Frieren manga ID
const apiUrl = `https://api.allorigins.win/get?url=${encodeURIComponent(
`https://api.mangadex.org/manga/${mangaId}/feed?order[chapter]=asc&translatedLanguage[]=en`
)}`;
try {
const response = await fetch(apiUrl);
const data = await response.json();
const chapters = JSON.parse(data.contents).data; // Parse the API response
chapters.forEach(chapter => {
const option = document.createElement('option');
option.value = chapter.id;
option.textContent = `Chapter ${chapter.attributes.chapter}`;
chapterSelect.appendChild(option.cloneNode(true));
chapterSelectBottom.appendChild(option);
});
// Set the selected chapter in the dropdown
const currentChapterId = new URLSearchParams(window.location.search).get('chapter');
chapterSelect.value = currentChapterId;
chapterSelectBottom.value = currentChapterId;
} catch (error) {
console.error('Error fetching chapters:', error);
}
}
populateChapterSelect();