# 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.

Source: https://crawl-docs.pdhq.win/advanced/extension-internals/
Last updated: 2026-12-02

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

1. The panel or the toolbar button asks `capture.ts` to read the current tab.
2. `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 uses `extract.ts`. Everything else is an **article**.
3. 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.scripting` sends its source text.
4. For an article, a matching site rule (see [Site rules](https://crawl-docs.pdhq.win/advanced/site-rules.md)) is looked up first and passed in.
5. The reader returns `{ ok: true, post, notes }` or `{ ok: false, reason, hint }`. A `hint` of `small` or `pick` makes 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/uploads` in 16 MB pieces, then `done`. The post names the upload by `uploadId` (and `audioUploadId` for 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.ts` decides 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](https://crawl-docs.pdhq.win/advanced/security.md).

## Add a reader for a new site

1. Add `extract-<site>.ts` with one exported async function that returns an `ExtractResult`. Keep it self-contained.
2. Add a branch for the host in `readerFor` in `capture.ts`, and a matching branch in `extract()`.
3. If the site needs a new kind of data, extend the schema first: see [The Post schema](https://crawl-docs.pdhq.win/advanced/post-schema.md).
4. Write a test in `apps/extension/test/`. The extractor tests build a small page and run the reader on it: copy `extract-github.test.mjs` as a pattern.
5. To make the web app show it, add a viewer component and teach `packages/render` to 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`):

```bash
CHROMIUM_PATH=<chromium binary> yarn workspace @social-crawl/extension test
```

CI installs Playwright's own Chromium, because Google Chrome no longer loads unpacked extensions from the command line.
