Extension internals
How the browser extension is built: its modules, how a page is read and sent, how videos are found and uploaded, how big videos are sent, and how to add a reader for a new site.
On this page
The extension is plain TypeScript with no framework and no runtime dependencies. It targets Chrome (Manifest V3, minimum version 111). The code is in apps/extension/src. yarn workspace @social-crawl/extension build compiles it with tsc and copies static/ (the manifest and the HTML pages) into dist/.
The pieces#
| Piece | Files | Job |
|---|---|---|
| Service worker | background.ts | Keeps, per tab, the video files and playlists the page loaded. Saves a post when the panel asks. Registers the web app bridge. |
| The panel ("island") | island.ts | The floating panel and its pill, drawn on the page. Shows what was read, the video list, tags, progress and errors. |
| Reading and sending | capture.ts | Picks the reader for the page (readerFor), runs it in the tab with chrome.scripting.executeScript, and posts the result to POST /capture. |
| Readers | extract.ts (Instagram), extract-facebook.ts, extract-x.ts, extract-pinterest.ts, extract-linkedin.ts, extract-youtube.ts, extract-playlist.ts, extract-title.ts (IMDb, Rotten Tomatoes), extract-github.ts, extract-article.ts (everything else) | Each is a self-contained function that runs in the page and returns a Post or a reason. |
| Site rules | templates.ts, pick-area.ts, options.ts | The rule data format, the pointing editor, and the rules list in the settings. |
| Settings | settings.ts, options.html | API address, token, video choices, YouTube choices, rules. Stored in chrome.storage. |
| Video finding | video-watch.ts, video-detect.ts, find-streams.ts, m3u8.ts, video-buttons.ts, video-wake.ts, probe-download.ts | A list of what the page loads, like a download manager. Opt-in. |
| Downloading | video-download.ts, server-download.ts, youtube.ts, tube.ts, quality.ts | Saving a picked video: in the browser, then uploaded in 16 MB pieces, or asked of the server. |
| Big videos | big-route.ts, big-transfer.ts, big-job.ts, big-list.ts, transfer.ts, transfer.html | Sending a video over the server's limit to WebDAV in parts, from a window of its own. |
| Web app bridge | webapp-bridge.ts | Lets the web app's Download video button ask the extension. Registered only for the web app's site, which the person allowed. |
| Lab | lab*.ts, lab.html | Testing only. Off unless the server sets LAB_ENABLED. |
How a page is read#
- The panel or the toolbar button asks
capture.tsto read the current tab. readerFor(url)chooses a reader. YouTube videos and playlists, X, Facebook, Pinterest, LinkedIn, IMDb and Rotten Tomatoes titles and GitHub repositories each have their own. Instagram usesextract.ts. Everything else is an article.- The reader function is serialised and run in the page. It must be self-contained: no imports (types only) and no names from outside, because
chrome.scriptingsends its source text. - For an article, a matching site rule (see Site rules) is looked up first and passed in.
- The reader returns
{ ok: true, post, notes }or{ ok: false, reason, hint }. Ahintofsmallorpickmakes the panel offer Save anyway and Choose the article area.
How a post and its videos are sent#
- Small videos. The browser downloads the files and sends them to
POST /capture/uploadsin 16 MB pieces, thendone. The post names the upload byuploadId(andaudioUploadIdfor a sound kept apart), and the server joins them into one file. - Server downloads. For sites the server can fetch, only the address goes:
POST /capture/fetches. - Streams. The extension reads the playlist address the page itself requested (HLS), or collects the numbered pieces (LinkedIn), and the server joins them with ffmpeg. DASH is not handled.
- Big videos.
big-route.tsdecides which way a picked video goes. A video over the server's limit goes the big way: the post is saved first, then a window of the extension (transfer.html) sends the tracks in parts, with Resume. - YouTube. The formats come from YouTube's player service the way download managers get them, not from the page's own coded player. This is the part most likely to break when YouTube changes.
Permissions#
activeTab, scripting, storage, webRequest, declarativeNetRequestWithHostAccess and power, plus optional access to https://*/* and localhost. See Security notes.
Add a reader for a new site#
- Add
extract-<site>.tswith one exported async function that returns anExtractResult. Keep it self-contained. - Add a branch for the host in
readerForincapture.ts, and a matching branch inextract(). - If the site needs a new kind of data, extend the schema first: see The Post schema.
- Write a test in
apps/extension/test/. The extractor tests build a small page and run the reader on it: copyextract-github.test.mjsas a pattern. - To make the web app show it, add a viewer component and teach
packages/renderto draw it for the saved page.
For a site that only needs a better article reader, write a site rule instead: no code change.
Tests#
The extension tests run in real Chromium (playwright-core):
CHROMIUM_PATH=<chromium binary> yarn workspace @social-crawl/extension testCI installs Playwright's own Chromium, because Google Chrome no longer loads unpacked extensions from the command line.