Update app.js: Support decimal chapters in navigation, use versioned cache

This commit is contained in:
小虾米 2026-03-27 16:25:31 +08:00
parent 422d9c8a32
commit 223d05299f

View File

@ -3,6 +3,9 @@
let chaptersData = [];
let isDataLoaded = false;
// 版本号,每次更新时修改
const CACHE_VERSION = '20260327-1615';
// 加载所有章节数据
async function loadChaptersData() {
if (isDataLoaded) return chaptersData;
@ -15,23 +18,11 @@ async function loadChaptersData() {
try {
// 格式化章节号(带前导零)
const chapterId = chapterNum.toString().padStart(2, '0');
const response = await fetch(`data/chapter-${chapterId}.json`, {
cache: 'no-store',
headers: {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache'
}
});
const response = await fetch(`data/chapter-${chapterId}.json?v=${CACHE_VERSION}`);
if (!response.ok) {
// 尝试不带前导零的格式
const response2 = await fetch(`data/chapter-${chapterNum}.json`, {
cache: 'no-store',
headers: {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache'
}
});
const response2 = await fetch(`data/chapter-${chapterNum}.json?v=${CACHE_VERSION}`);
if (!response2.ok) break;
const data = await response2.json();
chapters.push(normalizeChapterData(data, chapterNum));
@ -51,13 +42,7 @@ async function loadChaptersData() {
for (let i = 1; i <= chapterNum + 10; i++) {
try {
const decimalId = i + 0.5;
const response = await fetch(`data/chapter-${decimalId}.json`, {
cache: 'no-store',
headers: {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache'
}
});
const response = await fetch(`data/chapter-${decimalId}.json?v=${CACHE_VERSION}`);
if (response.ok) {
const data = await response.json();
@ -435,11 +420,15 @@ function initReaderNav(currentId) {
const data = window.chaptersData || chaptersData;
// 找到当前章节在数组中的索引
const currentIndex = data.findIndex(ch => parseFloat(ch.id) === parseFloat(currentId));
if (prevBtn) {
if (currentId > 1) {
if (currentIndex > 0) {
const prevChapter = data[currentIndex - 1];
prevBtn.disabled = false;
prevBtn.onclick = () => {
window.location.href = `reader.html?id=${currentId - 1}`;
window.location.href = `reader.html?id=${prevChapter.id}`;
};
} else {
prevBtn.disabled = true;
@ -447,10 +436,11 @@ function initReaderNav(currentId) {
}
if (nextBtn) {
if (currentId < data.length) {
if (currentIndex < data.length - 1) {
const nextChapter = data[currentIndex + 1];
nextBtn.disabled = false;
nextBtn.onclick = () => {
window.location.href = `reader.html?id=${currentId + 1}`;
window.location.href = `reader.html?id=${nextChapter.id}`;
};
} else {
nextBtn.disabled = true;