bug fixes on the reader

This commit is contained in:
2025-12-31 18:25:42 +01:00
parent 1e144c4bad
commit e3c366836f
2 changed files with 75 additions and 51 deletions

View File

@@ -1,7 +1,4 @@
// reader.js refactorizado
const urlParams = new URLSearchParams(window.location.search); const urlParams = new URLSearchParams(window.location.search);
const lang = urlParams.get('lang') ?? 'none';
const reader = document.getElementById('reader'); const reader = document.getElementById('reader');
const panel = document.getElementById('settings-panel'); const panel = document.getElementById('settings-panel');
const overlay = document.getElementById('overlay'); const overlay = document.getElementById('overlay');
@@ -14,6 +11,16 @@ const nextBtn = document.getElementById('next-chapter');
const lnSettings = document.getElementById('ln-settings'); const lnSettings = document.getElementById('ln-settings');
const mangaSettings = document.getElementById('manga-settings'); const mangaSettings = document.getElementById('manga-settings');
const rawSource = urlParams.get('source') || 'anilist';
const sourceParts = rawSource.split('?');
const source = sourceParts[0];
let lang =
urlParams.get('lang') ??
new URLSearchParams(sourceParts[1] || '').get('lang') ??
'none';
const config = { const config = {
ln: { ln: {
fontSize: 18, fontSize: 18,
@@ -123,9 +130,6 @@ function updateSettingsVisibility() {
// === CAMBIO: Nueva función para traer la lista de capítulos y saber el orden === // === CAMBIO: Nueva función para traer la lista de capítulos y saber el orden ===
async function fetchChapterList() { async function fetchChapterList() {
const urlParams = new URLSearchParams(window.location.search);
const source = urlParams.get('source') || 'anilist';
try { try {
// Reusamos el endpoint que lista capítulos // Reusamos el endpoint que lista capítulos
const res = await fetch(`/api/book/${bookId}/chapters?source=${source}&provider=${provider}`); const res = await fetch(`/api/book/${bookId}/chapters?source=${source}&provider=${provider}`);
@@ -154,10 +158,6 @@ async function loadChapter() {
</div> </div>
`; `;
const urlParams = new URLSearchParams(window.location.search);
let source = urlParams.get('source');
if (!source) source = 'anilist';
// === CAMBIO: Si no tenemos la lista de capítulos (y no es local), la pedimos === // === CAMBIO: Si no tenemos la lista de capítulos (y no es local), la pedimos ===
if (provider !== 'local' && chaptersList.length === 0) { if (provider !== 'local' && chaptersList.length === 0) {
await fetchChapterList(); await fetchChapterList();
@@ -176,6 +176,15 @@ async function loadChapter() {
const res = await fetch(newEndpoint); const res = await fetch(newEndpoint);
const data = await res.json(); const data = await res.json();
const chapterMeta = chaptersList.find(
c => String(c.id) === String(currentChapterId)
);
if (chapterMeta) {
chapterLabel.textContent = `Chapter ${chapterMeta.number} - ${chapterMeta.title}`;
document.title = `Chapter ${chapterMeta.number} - ${chapterMeta.title}`;
}
// Lógica específica para contenido LOCAL // Lógica específica para contenido LOCAL
if (provider === 'local') { if (provider === 'local') {
const unitIndex = Number(currentChapterId); // En local el ID suele ser el índice const unitIndex = Number(currentChapterId); // En local el ID suele ser el índice
@@ -186,9 +195,6 @@ async function loadChapter() {
return; return;
} }
chapterLabel.textContent = unit.name;
document.title = unit.name;
const manifestRes = await fetch(`/api/library/${unit.id}/manifest`); const manifestRes = await fetch(`/api/library/${unit.id}/manifest`);
const manifest = await manifestRes.json(); const manifest = await manifestRes.json();
@@ -215,6 +221,8 @@ async function loadChapter() {
return; return;
} }
} }
const rawSource = urlParams.get('source') || 'anilist';
const source = rawSource.split('?')[0];
const res2 = await fetch(`/api/book/${bookId}?source=${source}`); const res2 = await fetch(`/api/book/${bookId}?source=${source}`);
const data2 = await res2.json(); const data2 = await res2.json();
@@ -234,12 +242,14 @@ async function loadChapter() {
return; return;
} }
if (data.title) { if (!chapterMeta) {
chapterLabel.textContent = data.title; if (data.title) {
document.title = data.title; chapterLabel.textContent = `Chapter ${data.number ?? ''} - ${data.title}`;
} else { document.title = chapterLabel.textContent;
chapterLabel.textContent = `Chapter ${data.number ?? currentChapterId}`; } else {
document.title = `Chapter ${data.number ?? currentChapterId}`; chapterLabel.textContent = `Chapter ${data.number ?? currentChapterId}`;
document.title = chapterLabel.textContent;
}
} }
setupProgressTracking(data, source); setupProgressTracking(data, source);
@@ -339,11 +349,9 @@ function changeChapter(newId) {
} }
function updateURL(newId) { function updateURL(newId) {
const urlParams = new URLSearchParams(window.location.search);
const source = urlParams.get('source') ?? 'anilist';
// La URL ahora contiene el ID en lugar del número/índice // La URL ahora contiene el ID en lugar del número/índice
const newUrl = `/read/${provider}/${newId}/${bookId}?source=${source}&lang=${lang}`; const newUrl =
`/read/${provider}/${newId}/${bookId}?source=${source}&lang=${lang}`;
window.history.pushState({}, '', newUrl); window.history.pushState({}, '', newUrl);
} }
@@ -602,8 +610,6 @@ document.querySelectorAll('[data-direction]').forEach(btn => {
// Botón "Atrás" // Botón "Atrás"
document.getElementById('back-btn').addEventListener('click', () => { document.getElementById('back-btn').addEventListener('click', () => {
const urlParams = new URLSearchParams(window.location.search);
let source = urlParams.get('source')?.split('?')[0];
if (source === 'anilist' || !source) { if (source === 'anilist' || !source) {
window.location.href = `/book/${bookId}`; window.location.href = `/book/${bookId}`;

View File

@@ -1,7 +1,4 @@
// reader.js refactorizado
const urlParams = new URLSearchParams(window.location.search); const urlParams = new URLSearchParams(window.location.search);
const lang = urlParams.get('lang') ?? 'none';
const reader = document.getElementById('reader'); const reader = document.getElementById('reader');
const panel = document.getElementById('settings-panel'); const panel = document.getElementById('settings-panel');
const overlay = document.getElementById('overlay'); const overlay = document.getElementById('overlay');
@@ -14,6 +11,16 @@ const nextBtn = document.getElementById('next-chapter');
const lnSettings = document.getElementById('ln-settings'); const lnSettings = document.getElementById('ln-settings');
const mangaSettings = document.getElementById('manga-settings'); const mangaSettings = document.getElementById('manga-settings');
const rawSource = urlParams.get('source') || 'anilist';
const sourceParts = rawSource.split('?');
const source = sourceParts[0];
let lang =
urlParams.get('lang') ??
new URLSearchParams(sourceParts[1] || '').get('lang') ??
'none';
const config = { const config = {
ln: { ln: {
fontSize: 18, fontSize: 18,
@@ -123,9 +130,6 @@ function updateSettingsVisibility() {
// === CAMBIO: Nueva función para traer la lista de capítulos y saber el orden === // === CAMBIO: Nueva función para traer la lista de capítulos y saber el orden ===
async function fetchChapterList() { async function fetchChapterList() {
const urlParams = new URLSearchParams(window.location.search);
const source = urlParams.get('source') || 'anilist';
try { try {
// Reusamos el endpoint que lista capítulos // Reusamos el endpoint que lista capítulos
const res = await fetch(`/api/book/${bookId}/chapters?source=${source}&provider=${provider}`); const res = await fetch(`/api/book/${bookId}/chapters?source=${source}&provider=${provider}`);
@@ -154,10 +158,6 @@ async function loadChapter() {
</div> </div>
`; `;
const urlParams = new URLSearchParams(window.location.search);
let source = urlParams.get('source');
if (!source) source = 'anilist';
// === CAMBIO: Si no tenemos la lista de capítulos (y no es local), la pedimos === // === CAMBIO: Si no tenemos la lista de capítulos (y no es local), la pedimos ===
if (provider !== 'local' && chaptersList.length === 0) { if (provider !== 'local' && chaptersList.length === 0) {
await fetchChapterList(); await fetchChapterList();
@@ -176,6 +176,15 @@ async function loadChapter() {
const res = await fetch(newEndpoint); const res = await fetch(newEndpoint);
const data = await res.json(); const data = await res.json();
const chapterMeta = chaptersList.find(
c => String(c.id) === String(currentChapterId)
);
if (chapterMeta) {
chapterLabel.textContent = `Chapter ${chapterMeta.number} - ${chapterMeta.title}`;
document.title = `Chapter ${chapterMeta.number} - ${chapterMeta.title}`;
}
// Lógica específica para contenido LOCAL // Lógica específica para contenido LOCAL
if (provider === 'local') { if (provider === 'local') {
const unitIndex = Number(currentChapterId); // En local el ID suele ser el índice const unitIndex = Number(currentChapterId); // En local el ID suele ser el índice
@@ -186,9 +195,6 @@ async function loadChapter() {
return; return;
} }
chapterLabel.textContent = unit.name;
document.title = unit.name;
const manifestRes = await fetch(`/api/library/${unit.id}/manifest`); const manifestRes = await fetch(`/api/library/${unit.id}/manifest`);
const manifest = await manifestRes.json(); const manifest = await manifestRes.json();
@@ -215,19 +221,35 @@ async function loadChapter() {
return; return;
} }
} }
const rawSource = urlParams.get('source') || 'anilist';
const source = rawSource.split('?')[0];
const res2 = await fetch(`/api/book/${bookId}?source=${source}`);
const data2 = await res2.json();
fetch("/api/rpc", {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({
details: data2.title.romaji ?? data2.title,
state: `Chapter ${data.title}`,
mode: "reading"
})
});
// Lógica para Extensiones / Anilist
if (data.error) { if (data.error) {
reader.innerHTML = `<div class="loading-container"><span style="color: #ef4444;">Error: ${data.error}</span></div>`; reader.innerHTML = `<div class="loading-container"><span style="color: #ef4444;">Error: ${data.error}</span></div>`;
return; return;
} }
if (data.title) { if (!chapterMeta) {
chapterLabel.textContent = data.title; if (data.title) {
document.title = data.title; chapterLabel.textContent = `Chapter ${data.number ?? ''} - ${data.title}`;
} else { document.title = chapterLabel.textContent;
chapterLabel.textContent = `Chapter ${data.number ?? currentChapterId}`; } else {
document.title = `Chapter ${data.number ?? currentChapterId}`; chapterLabel.textContent = `Chapter ${data.number ?? currentChapterId}`;
document.title = chapterLabel.textContent;
}
} }
setupProgressTracking(data, source); setupProgressTracking(data, source);
@@ -327,11 +349,9 @@ function changeChapter(newId) {
} }
function updateURL(newId) { function updateURL(newId) {
const urlParams = new URLSearchParams(window.location.search);
const source = urlParams.get('source') ?? 'anilist';
// La URL ahora contiene el ID en lugar del número/índice // La URL ahora contiene el ID en lugar del número/índice
const newUrl = `/read/${provider}/${newId}/${bookId}?source=${source}&lang=${lang}`; const newUrl =
`/read/${provider}/${newId}/${bookId}?source=${source}&lang=${lang}`;
window.history.pushState({}, '', newUrl); window.history.pushState({}, '', newUrl);
} }
@@ -590,8 +610,6 @@ document.querySelectorAll('[data-direction]').forEach(btn => {
// Botón "Atrás" // Botón "Atrás"
document.getElementById('back-btn').addEventListener('click', () => { document.getElementById('back-btn').addEventListener('click', () => {
const urlParams = new URLSearchParams(window.location.search);
let source = urlParams.get('source')?.split('?')[0];
if (source === 'anilist' || !source) { if (source === 'anilist' || !source) {
window.location.href = `/book/${bookId}`; window.location.href = `/book/${bookId}`;