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