Update app.js: Support decimal chapters (e.g., 107.5)

This commit is contained in:
小虾米 2026-03-27 16:05:44 +08:00
parent d74328959e
commit a0b3188994

View File

@ -8,13 +8,27 @@ async function loadChaptersData() {
if (isDataLoaded) return chaptersData; if (isDataLoaded) return chaptersData;
const chapters = []; const chapters = [];
let chapterNum = 1; const chapterIds = [];
// 循环加载章节,直到找不到文件 // 先尝试加载1-200的整数章节
while (true) { for (let i = 1; i <= 200; i++) {
chapterIds.push(i);
// 检查是否有x.5章节
chapterIds.push(i + 0.5);
}
// 循环加载章节
for (const chapterNum of chapterIds) {
try { try {
// 格式化章节号(带前导零) // 格式化章节号
const chapterId = chapterNum.toString().padStart(2, '0'); let chapterId;
if (Number.isInteger(chapterNum)) {
chapterId = chapterNum.toString().padStart(2, '0');
} else {
// 小数章节如 107.5
chapterId = chapterNum.toString();
}
const response = await fetch(`data/chapter-${chapterId}.json`, { const response = await fetch(`data/chapter-${chapterId}.json`, {
cache: 'no-store', cache: 'no-store',
headers: { headers: {
@ -23,30 +37,18 @@ async function loadChaptersData() {
} }
}); });
if (!response.ok) { if (response.ok) {
// 尝试不带前导零的格式
const response2 = await fetch(`data/chapter-${chapterNum}.json`, {
cache: 'no-store',
headers: {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache'
}
});
if (!response2.ok) break;
const data = await response2.json();
chapters.push(normalizeChapterData(data, chapterNum));
} else {
const data = await response.json(); const data = await response.json();
chapters.push(normalizeChapterData(data, chapterNum)); chapters.push(normalizeChapterData(data, chapterNum));
} }
chapterNum++;
} catch (error) { } catch (error) {
// 没有更多章节了 // 忽略错误,继续加载下一章
break;
} }
} }
// 按id排序
chapters.sort((a, b) => a.id - b.id);
chaptersData = chapters; chaptersData = chapters;
isDataLoaded = true; isDataLoaded = true;