From 1bda59e3fc6dc1813416d156f1ca7e26d2204dd8 Mon Sep 17 00:00:00 2001 From: marwin Date: Wed, 1 Apr 2026 23:46:57 +0200 Subject: [PATCH] Fix PDF position restore: timer after restore, add scroll-mode case - Move auto-save timer setup to after position restore so an early visibilitychange can't overwrite the saved position with 0 - Add missing restore path for non-paginated PDF scroll mode Co-Authored-By: Claude Sonnet 4.6 --- static/js/app.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) 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', () => {