diff --git a/static/js/app.js b/static/js/app.js index d43a22a..2ca54f8 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -3157,7 +3157,7 @@ async function openBook(bookId) { progressInput.addEventListener('click', function () { this.select(); }); } - // Restore scroll position + // Restore scroll position — must happen BEFORE auto-save timer is started try { const progressRes = await fetch('/books/'); const allBooks = await progressRes.json(); @@ -3169,8 +3169,13 @@ async function openBook(bookId) { if (fraction > 0 && pdfTotalPages > 1) pdfCurrentPage = Math.max(1, Math.round(fraction * (pdfTotalPages - 1)) + 1); enterPdfPaginatedMode(); - } else if (!isPdf) { - // Wait for all images so scrollHeight is final + } else if (isPdf) { + // PDF scroll mode + await new Promise(r => requestAnimationFrame(r)); + if (fraction > 0) + contentEl.scrollTop = fraction * (contentEl.scrollHeight - contentEl.clientHeight); + } else { + // EPUB — wait for images so scrollHeight is final const imgs = Array.from(contentEl.querySelectorAll('img')); if (imgs.length) { await Promise.all(imgs.map(img => @@ -3206,6 +3211,7 @@ async function openBook(bookId) { }); // Auto-save progress every 10s and on scroll (debounced 2s) + // Started AFTER restore so an early visibilitychange can't overwrite with position 0 readerScrollSaveTimer = setInterval(saveReaderProgress, 10000); let _scrollDebounce = null; contentEl.addEventListener('scroll', () => {