Steps 11-12: migrate chapter + book page reading-progress JS from localStorage to /api/progress

This commit is contained in:
Admin
2026-03-02 14:52:01 +05:00
parent af86c6f96f
commit 5ac89da513

View File

@@ -1769,7 +1769,7 @@ const bookTmpl = `
</div>
</div>
<!-- Resume reading button — populated by JS from localStorage -->
<!-- Resume reading button — populated by JS from /api/progress -->
<div id="resume-bar" class="mb-4 hidden">
<a id="resume-link"
hx-target="#main-content"
@@ -1874,16 +1874,8 @@ const bookTmpl = `
(function () {
var SLUG = '{{.Slug}}';
var LS_KEY = 'reading_progress';
function getProgress() {
try { return JSON.parse(localStorage.getItem(LS_KEY) || '{}'); } catch(_) { return {}; }
}
window.applyProgress = function(slug) {
var progress = getProgress();
var saved = progress[slug];
function applyProgressData(slug, saved) {
// Show/hide latest bar (only when no progress recorded for this book).
var latestBar = document.getElementById('latest-bar');
if (latestBar) {
@@ -1918,6 +1910,13 @@ const bookTmpl = `
htmx.process(link);
bar.classList.remove('hidden');
}
}
window.applyProgress = function(slug) {
fetch('/api/progress')
.then(function(r) { return r.ok ? r.json() : {}; })
.then(function(data) { applyProgressData(slug, data[slug] || null); })
.catch(function() { applyProgressData(slug, null); });
};
applyProgress(SLUG);
@@ -2471,14 +2470,11 @@ const chapterTmpl = `
// ── reading progress ──────────────────────────────────────────────────────
(function saveProgress() {
try {
var p = JSON.parse(localStorage.getItem('reading_progress') || '{}');
p[SLUG] = CHAPTER_N;
localStorage.setItem('reading_progress', JSON.stringify(p));
var ts = JSON.parse(localStorage.getItem('reading_progress_ts') || '{}');
ts[SLUG] = Date.now();
localStorage.setItem('reading_progress_ts', JSON.stringify(ts));
} catch(_) {}
fetch('/api/progress/' + encodeURIComponent(SLUG), {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ chapter: CHAPTER_N })
}).catch(function() {});
})();
// ── generation counter (stale-closure guard) ──────────────────────────────