← All examples

JavaScript Productivity substack.com

Substack: auto-mark post as read

After 80% of the post is scrolled through it is stored as read in localStorage and a "Read" badge appears.

Code to copy

// Mark a post as "read" in localStorage after 80% of it has been scrolled through
(function () {
  var KEY = 'jz-substack-read';
  function load() { try { return JSON.parse(localStorage.getItem(KEY) || '{}'); } catch (e) { return {}; } }
  function save(map) { try { localStorage.setItem(KEY, JSON.stringify(map)); } catch (e) {} }
  function root() {
    return document.querySelector('.body.markup, .available-content, [class*="postBody"]');
  }
  function badge() {
    if (document.getElementById('jz-sub-read-badge')) { return; }
    var d = document.createElement('div');
    d.id = 'jz-sub-read-badge';
    d.textContent = '✓ Read';
    d.style.cssText = 'position:fixed;right:14px;bottom:14px;background:#16a34a;color:#fff;padding:6px 10px;border-radius:14px;font:600 12px/1 system-ui,sans-serif;z-index:99999;box-shadow:0 2px 6px rgba(0,0,0,.2);';
    document.body.appendChild(d);
  }
  function check() {
    var r = root(); if (!r) { return; }
    var rect = r.getBoundingClientRect();
    var seen = window.innerHeight - rect.top;
    var pct = seen / rect.height;
    if (pct < 0.8) { return; }
    var key = location.pathname;
    var map = load();
    if (!map[key]) { map[key] = Date.now(); save(map); }
    badge();
  }
  check();
  var map = load();
  if (map[location.pathname]) { badge(); }
  window.addEventListener('scroll', function () {
    clearTimeout(window.__jzSubRead);
    window.__jzSubRead = setTimeout(check, 200);
  }, { passive: true });
})();

How to use this example

  1. Copy the code with the button above.
  2. Install JustZix (2 minutes) and open the extension on the target page.
  3. Add a new rule matching that page.
  4. Paste the code into the rule's JavaScript panel and save — it runs on every page visit.

Rate this example

No ratings yet — be the first.

Does this example work?

Snippets are useless without somewhere to paste them.

JustZix takes 2 minutes to install and runs your code on every matching page. No account, no payment.

Download free See use cases