Initial commit: read social posts back without the app
antisocial is the other half of a StopTheMadness redirect rule. Links to X, Threads, Instagram, TikTok and Bluesky get rewritten to /<prefix>/<original path>, and this resolves the post and shows the media and the words, with a badge saying where it came from and a button to copy the original URL. Every request drives a real headless Chromium, logged out, from a residential IP. One code path, and it survives markup changes better than parsing from the outside would. Extraction is layered, most structured first: the platform's own API response caught in flight, then an inline payload, then the rendered DOM, then Open Graph tags. Media is never linked straight at a CDN. Instagram and TikTok reject requests without a matching Referer and cookies, and proxying keeps the viewer's browser from talking to the platform at all. Range is forwarded so the native video scrubber can seek. HLS is the exception, since proxying it would mean rewriting playlists. TikTok sometimes answers with a slider puzzle. Rather than reporting that as a failure, the page is parked and the viewer is handed the puzzle: screenshots stream out, pointer events are replayed back. Solving it leaves the cookie in the shared browser context, so the retry is an ordinary request. A failed resolve is never a blank error page. The card carries the platform, the original URL and the copy button, so a broken adapter still leaves the link one tap away. Verified end to end against real shared links on all five platforms, in the container, including multi-image carousels, reels, TikTok short links and photo posts. 49 tests run the adapters against captured payloads with no network. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BGkRmLfiWuJHx6tQ12EELY
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
import { ResolveError, type Media, type Post } from '../types.ts';
|
||||
import { readOpenGraph } from '../browser/capture.ts';
|
||||
import { findCaption, findMetaMedia, mediaFromMetaNode, metaHeaders } from './meta-media.ts';
|
||||
import { hostMatcher } from './url.ts';
|
||||
import type { PlatformSpec, ResolveContext } from './types.ts';
|
||||
|
||||
const REFERER = 'https://www.threads.com/';
|
||||
|
||||
function handleFromPath(pathRest: string): string | undefined {
|
||||
const match = /^(@[^/]+)/.exec(pathRest);
|
||||
return match?.[1];
|
||||
}
|
||||
|
||||
/** Threads ships its Relay payloads in `<script type="application/json">`
|
||||
* tags, several dozen of them, in an order that is not worth relying on. */
|
||||
async function inlinePayloads(ctx: ResolveContext): Promise<unknown[]> {
|
||||
const raw = await ctx.page
|
||||
.evaluate(() =>
|
||||
[...document.querySelectorAll('script[type="application/json"]')]
|
||||
.map((el) => el.textContent ?? '')
|
||||
.filter((text) => text.length > 64),
|
||||
)
|
||||
.catch(() => [] as string[]);
|
||||
|
||||
return raw.flatMap((text) => {
|
||||
try {
|
||||
return [JSON.parse(text) as unknown];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function fromOpenGraph(ctx: ResolveContext): Promise<{ media: Media[]; text?: string }> {
|
||||
const og = await readOpenGraph(ctx.page);
|
||||
const fetchHeaders = metaHeaders(REFERER);
|
||||
|
||||
if (og['og:video']) {
|
||||
return {
|
||||
media: [{
|
||||
kind: 'video',
|
||||
url: og['og:video'],
|
||||
fetchHeaders,
|
||||
...(og['og:image'] ? { poster: { url: og['og:image'], fetchHeaders } } : {}),
|
||||
}],
|
||||
...(og['og:description'] ? { text: og['og:description'] } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
media: og['og:image'] ? [{ kind: 'image', url: og['og:image'], fetchHeaders }] : [],
|
||||
...(og['og:description'] ? { text: og['og:description'] } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
async function resolve(ctx: ResolveContext): Promise<Post> {
|
||||
const { page, originalUrl, pathRest } = ctx;
|
||||
|
||||
await page.goto(originalUrl, { waitUntil: 'domcontentloaded' });
|
||||
|
||||
const payloads = await inlinePayloads(ctx);
|
||||
const node = findMetaMedia(payloads);
|
||||
|
||||
let media = node ? mediaFromMetaNode(node, REFERER) : [];
|
||||
let text = findCaption(payloads);
|
||||
|
||||
if (media.length === 0 || !text) {
|
||||
const og = await fromOpenGraph(ctx);
|
||||
if (media.length === 0) media = og.media;
|
||||
text ??= og.text;
|
||||
}
|
||||
|
||||
// A Threads post is often only words, so unlike the others an empty media
|
||||
// list is a perfectly good result — but nothing at all is a failure.
|
||||
if (media.length === 0 && !text) {
|
||||
throw new ResolveError('Threads returned nothing for that URL', 'threads', originalUrl);
|
||||
}
|
||||
|
||||
const handle = handleFromPath(pathRest) ?? '@threads';
|
||||
return {
|
||||
platform: 'threads',
|
||||
platformLabel: 'Threads',
|
||||
originalUrl,
|
||||
author: { handle },
|
||||
...(text ? { text } : {}),
|
||||
textPosition: 'above',
|
||||
media,
|
||||
};
|
||||
}
|
||||
|
||||
export const threads: PlatformSpec = {
|
||||
id: 'threads',
|
||||
label: 'Threads',
|
||||
prefix: 'threads',
|
||||
canonicalHost: 'www.threads.com',
|
||||
textPosition: 'above',
|
||||
matchesHost: hostMatcher('threads.net', 'threads.com'),
|
||||
resolve,
|
||||
};
|
||||
Reference in New Issue
Block a user