Last active
October 15, 2026 00:58
-
-
Save edsu/d908bc63617c9ada01b030fb65e25be7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Mastodon profile-timeline behavior for digipres.club/@ed (and any Mastodon | |
| // profile root). Two jobs the default crawl can't do on its own: | |
| // | |
| // 1. Queue each post's detail page. The timeline is a virtualized React list: | |
| // off-screen posts are empty placeholders, so their permalinks never reach | |
| // the crawler's link extractor. We scroll each post into view and addLink() | |
| // its /@user/<id> permalink so the crawler captures the standalone status | |
| // page (status + context API) that makes the detail view replayable. | |
| // | |
| // 2. Fully fetch media at full resolution. Mastodon gifv/video autoplay only | |
| // buffers a byte range before the post scrolls out (captured as an HTTP | |
| // 206 partial that can't replay), and gallery images render only the small | |
| // preview; the full-resolution original is in the thumbnail's <a href> and | |
| // the img srcset's /original/ entry. A plain fetch() with no Range header | |
| // pulls each whole file as a 200, so videos replay and the lightbox shows | |
| // the original. Alt text needs no click: it's in the status API JSON we | |
| // already capture (timeline + detail pages), which replay renders into the | |
| // img alt and the lightbox description. | |
| // | |
| // We do our own incremental scrolling (built-in autoscroll is disabled in the | |
| // config so the two don't fight) and harvest on every step, because virtualized | |
| // items must be read while they're on screen. | |
| class MastodonProfile { | |
| static id = "MastodonProfile"; | |
| // Only the profile root, e.g. /@ed — not detail pages (/@ed/123) or | |
| // /@ed/media, /@ed/followers, etc. | |
| static isMatch() { | |
| return /^\/@[^/]+\/?$/.test(window.location.pathname); | |
| } | |
| static init() { | |
| return {}; | |
| } | |
| async *run(ctx) { | |
| const { sleep, addLink, getState } = ctx.Lib; | |
| const origin = window.location.origin; | |
| const selfPath = window.location.pathname.replace(/\/+$/, ""); // e.g. "/@ed" | |
| const seenPosts = new Set(); | |
| const fetchedMedia = new Set(); | |
| // Fetch a URL with NO Range header (so the server returns a full 200) and | |
| // read the WHOLE body, so the browser actually downloads every byte and the | |
| // crawler records a complete resource. Just awaiting fetch() resolves on | |
| // headers alone and leaves the body unread, so only a truncated range gets | |
| // captured — which is what broke the larger videos. | |
| const fullFetch = async (url) => { | |
| if (!url || fetchedMedia.has(url)) return; | |
| fetchedMedia.add(url); | |
| try { | |
| const r = await fetch(url, { cache: "no-store" }); | |
| await r.arrayBuffer(); // consume the full body | |
| } catch (e) { /* ignore; a missing asset shouldn't stop the walk */ } | |
| }; | |
| // Pull every /original/ URL out of an img's srcset (the full-res variant; | |
| // the bare src is only the small preview). | |
| const originalsFromSrcset = (ss) => | |
| !ss ? [] : ss.split(",") | |
| .map((s) => s.trim().split(/\s+/)[0]) | |
| .filter((u) => u.indexOf("/original/") !== -1); | |
| const harvest = async () => { | |
| for (const art of document.querySelectorAll("article[data-id]")) { | |
| // Queue this post's own detail page. Only this account's own posts | |
| // (/@ed/<id>): a boost of a remote post has a /@user@instance/<id> | |
| // permalink that just redirects off to the origin instance, so queuing | |
| // it would only capture a redirect stub. Boost content is still visible | |
| // in the timeline itself. | |
| const t = art.querySelector("a.status__relative-time"); | |
| if (t) { | |
| const href = new URL(t.getAttribute("href"), origin).href.split("?")[0]; | |
| const path = new URL(href).pathname; | |
| if (path.startsWith(selfPath + "/") && !seenPosts.has(href)) { | |
| seenPosts.add(href); | |
| addLink(href); | |
| } | |
| } | |
| // Videos / gifv: fetch the whole file. | |
| for (const v of art.querySelectorAll("video")) { | |
| await fullFetch(v.currentSrc || v.src || (v.querySelector("source") || {}).src); | |
| } | |
| // Gallery images: fetch the full-resolution originals, not the preview. | |
| for (const a of art.querySelectorAll("a.media-gallery__item-thumbnail[href]")) { | |
| await fullFetch(a.href); // the "open original" link (source instance) | |
| } | |
| for (const img of art.querySelectorAll(".media-gallery__item img")) { | |
| for (const u of originalsFromSrcset(img.getAttribute("srcset"))) { | |
| await fullFetch(u); // local-cache original the lightbox requests | |
| } | |
| } | |
| } | |
| }; | |
| const se = document.scrollingElement; | |
| let lastH = 0, stuck = 0; | |
| const MAX_STEPS = 4000; // hard backstop | |
| for (let i = 0; i < MAX_STEPS; i++) { | |
| await harvest(); | |
| yield getState(ctx, `posts ${seenPosts.size}, media ${fetchedMedia.size}`, "mastodonPosts"); | |
| se.scrollBy(0, window.innerHeight * 0.9); | |
| await sleep(1000); | |
| const h = se.scrollHeight; | |
| const atBottom = se.scrollTop + window.innerHeight >= h - 5; | |
| if (h <= lastH && atBottom) { | |
| stuck++; | |
| if (stuck >= 4) break; // bottom reached and nothing new loaded | |
| await sleep(1500); // give the next batch a chance to arrive | |
| } else { | |
| stuck = 0; | |
| } | |
| lastH = h; | |
| } | |
| await harvest(); // final sweep of whatever's on screen at the end | |
| yield getState(ctx, `done: posts ${seenPosts.size}, media ${fetchedMedia.size}`, "mastodonPosts"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment