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