chrome.offscreen.
Setup
Request the permission inmanifest.json:
manifest.json
pages/ special folder and Extension.js compiles it like any other HTML entry:
pages/offscreen.html
Create, reuse, close
Chrome allows one offscreen document per extension, and callingcreateDocument while one exists throws. The reliable pattern is an ensure function that checks for an existing document first:
background.ts
creating lock matters: two events can race into ensureOffscreen before the document exists, and the second createDocument call would throw.
Close it when the work is done to free memory:
Choosing a reason
Thereasons array tells Chrome why the document exists. Common values:
| Reason | Use case |
|---|---|
DOM_PARSER | Parse or sanitize HTML strings |
AUDIO_PLAYBACK | Play sound from the background |
CLIPBOARD | Read or write the clipboard |
DOM_SCRAPING | Extract data from rendered markup |
BLOBS | Create and manage blob URLs |
USER_MEDIA | Access getUserMedia for recording |
AUDIO_PLAYBACK, Chrome closes the document automatically about 30 seconds after audio stops playing. For other reasons the document lives until you close it or the extension unloads.
Talking to the document
Offscreen documents use standard runtime messaging. Scope your messages so other surfaces ignore them:pages/offscreen.ts
Firefox
Firefox does not implementchrome.offscreen. Its Manifest V3 background runs as an event page that already has DOM access, so the same DOM work runs directly in the background script there. Use browser-specific manifest fields and a capability check (typeof chrome.offscreen !== "undefined") to branch.

