// Keeps every authenticated page current without ever moving the article // list under the reader. One poll (/new-since/) drives three things: // - the "N new articles" banner, wherever in the sidebar tree you are; // - a red dot on the browser tab's favicon, visible from a background tab; // - the sidebar's unread/starred counts, patched in place. // New articles are never inserted automatically: the banner click reloads // the current view. A full page load resets the baseline (data-since, // rendered into the page) — exactly when the user has seen the new articles. // The poll deliberately keeps running in a hidden tab (that's what the dot is // for); it also fires when the tab becomes visible again, and as soon as a // read/star action tells us the counts changed (the countsChanged event the // server sends via HX-Trigger). (function () { var POLL_MS = 300000; var MIN_GAP_MS = 15000; // coalesce bursts (rapid reading, tab switches) var script = document.currentScript; var url = script && script.dataset.url; var icon = document.querySelector('link[rel="icon"]'); var banner = document.querySelector(".new-articles-banner"); if (!url) return; var originalHref = icon ? icon.href : null; var baseline = parseInt(script.dataset.since, 10); if (isNaN(baseline)) baseline = null; var timer = null; var inFlight = false; var rerun = false; // a read happened while a poll was in flight: its answer may predate it var lastPoll = 0; var badged = null; // the dotted icon's data: URL, drawn once on first need var showing = false; function drawDot() { return new Promise(function (resolve) { var image = new Image(); image.onload = function () { var size = 64; var canvas = document.createElement("canvas"); canvas.width = canvas.height = size; var ctx = canvas.getContext("2d"); ctx.drawImage(image, 0, 0, size, size); ctx.beginPath(); ctx.arc(size * 0.74, size * 0.26, size * 0.23, 0, 2 * Math.PI); ctx.fillStyle = "#e53935"; ctx.fill(); ctx.lineWidth = size * 0.07; ctx.strokeStyle = "#ffffff"; ctx.stroke(); resolve(canvas.toDataURL("image/png")); }; image.onerror = function () { resolve(null); }; image.src = originalHref; }); } // Replace the rather than editing href: some browsers ignore an // in-place change to an existing favicon element. function setIcon(href) { var fresh = icon.cloneNode(); fresh.href = href; icon.parentNode.replaceChild(fresh, icon); icon = fresh; } function showDot(on) { if (!icon || on === showing) return Promise.resolve(); showing = on; if (!on) { setIcon(originalHref); return Promise.resolve(); } var ready = badged ? Promise.resolve(badged) : drawDot(); return ready.then(function (href) { badged = href; if (href && showing) setIcon(href); }); } function setCount(el, value) { var text = el.hasAttribute("data-starred-count") ? "(" + value + ")" : String(value); el.textContent = value ? text : ""; el.hidden = !value; } function updateCounts(counts) { if (!counts) return; document.querySelectorAll(".unread-count[data-feed-id]").forEach(function (el) { setCount(el, counts.feeds[el.dataset.feedId] || 0); }); document.querySelectorAll(".unread-count[data-tag-id]").forEach(function (el) { setCount(el, counts.tags[el.dataset.tagId] || 0); }); document.querySelectorAll("[data-starred-count]").forEach(function (el) { setCount(el, counts.starred || 0); }); } function updateBanner(count) { if (!banner) return; banner.hidden = !count; if (count) { banner.textContent = count + " new article" + (count === 1 ? "" : "s") + " \u2014 click to refresh"; } } function schedule() { clearTimeout(timer); timer = setTimeout(poll, POLL_MS); } function poll() { if (inFlight) { rerun = true; return; } inFlight = true; lastPoll = Date.now(); var query = baseline === null ? "" : "?since=" + baseline; fetch(url + query, { credentials: "same-origin", headers: { Accept: "application/json" } }) .then(function (response) { // A redirect means the session ended (we'd be reading the login // page) — stop polling instead of hammering it. if (!response.ok || response.redirected) throw new Error("stop"); return response.json(); }) .then(function (data) { inFlight = false; if (baseline === null) baseline = data.latest_id; updateCounts(data.counts); updateBanner(data.new_count); showDot(data.new_count > 0); if (rerun) { rerun = false; poll(); } else { schedule(); } }) .catch(function (error) { inFlight = false; rerun = false; if (error.message !== "stop") schedule(); }); } function pollSoon() { var wait = Math.max(0, MIN_GAP_MS - (Date.now() - lastPoll)); clearTimeout(timer); timer = setTimeout(poll, wait); } document.addEventListener("visibilitychange", function () { if (!document.hidden) pollSoon(); }); document.body.addEventListener("countsChanged", function () { // Reading is interactive: refresh the sidebar now, not after the gap. clearTimeout(timer); timer = setTimeout(poll, 150); }); poll(); })();