Files
thatguygriffandClaude Opus 5 b5f9483615
CI / Typecheck, test, build (push) Successful in 28s
Publish / Build and push (push) Failing after 2m24s
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
2026-08-26 11:32:25 -03:00

46 lines
2.3 KiB
TypeScript

import assert from 'node:assert/strict';
import { test } from 'node:test';
import { extractJsonObjectAfterKey } from '../src/platforms/scan.ts';
test('a plain payload is read straight out', () => {
const source = `window.x = {"a":1,"shortcode_media":{"id":"7","is_video":false},"b":2};`;
assert.deepEqual(extractJsonObjectAfterKey(source, 'shortcode_media'), { id: '7', is_video: false });
});
test('braces inside strings do not end the object', () => {
const source = `{"shortcode_media":{"caption":"a } b { c","id":"7"}}`;
assert.deepEqual(extractJsonObjectAfterKey(source, 'shortcode_media'), { caption: 'a } b { c', id: '7' });
});
test('an escaped quote inside a string does not end the string', () => {
const source = String.raw`{"shortcode_media":{"caption":"she said \"hi\" }","id":"7"}}`;
const found = extractJsonObjectAfterKey<{ caption: string }>(source, 'shortcode_media');
assert.equal(found?.caption, 'she said "hi" }');
});
test('a null value is skipped in favour of a later real one', () => {
const source = `{"shortcode_media":null,"other":1,"wrap":{"shortcode_media":{"id":"7"}}}`;
assert.deepEqual(extractJsonObjectAfterKey(source, 'shortcode_media'), { id: '7' });
});
test('a payload escaped inside a JS string is unescaped and decoded', () => {
// This is the real shape: Instagram nests the post JSON inside a bootstrap
// call, so the quotes and slashes arrive escaped a level deeper.
const source =
String.raw`requireLazy(["ServerJS"],function(s){s.handle({"gql_data":{\"shortcode_media\":{\"id\":\"7\",\"display_url\":\"https:\\\/\\\/cdn\\\/a.jpg\",\"caption\":\"line one\\nline two\"}}})})`;
const found = extractJsonObjectAfterKey<{ id: string; display_url: string; caption: string }>(
source,
'shortcode_media',
);
assert.equal(found?.id, '7');
// Both the slash escaping and the newline have to survive intact.
assert.equal(found?.display_url, 'https://cdn/a.jpg');
assert.equal(found?.caption, 'line one\nline two');
});
test('a missing key is undefined rather than a throw', () => {
assert.equal(extractJsonObjectAfterKey('{"a":1}', 'shortcode_media'), undefined);
assert.equal(extractJsonObjectAfterKey('', 'shortcode_media'), undefined);
assert.equal(extractJsonObjectAfterKey('{"shortcode_media":{"a":', 'shortcode_media'), undefined);
});