Chrome Extension Offscreen Documents — Manifest V3 Guide

4 min read

Offscreen Documents in Manifest V3

Introduction

manifest.json

{
  "permissions": ["offscreen"]
}

Creating an Offscreen Document

await chrome.offscreen.createDocument({
  url: "offscreen.html",
  reasons: ["DOM_SCRAPING"],  // Why you need DOM access
  justification: "Parse HTML content using DOMParser"
});

Reasons (enum values)

Offscreen HTML Page

<!-- offscreen.html -->
<!DOCTYPE html>
<html><body><script src="offscreen.js"></script></body></html>
// offscreen.js — has full DOM access
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  if (message.type === "parseHTML") {
    const parser = new DOMParser();
    const doc = parser.parseFromString(message.html, "text/html");
    const title = doc.querySelector("title")?.textContent;
    sendResponse({ title });
    return true;
  }
});

Communication: Background <-> Offscreen

Lifecycle Management

// Check if offscreen document already exists
const existingContexts = await chrome.runtime.getContexts({
  contextTypes: ["OFFSCREEN_DOCUMENT"]
});
if (existingContexts.length === 0) {
  await chrome.offscreen.createDocument({ ... });
}

Common Patterns

HTML/DOM Parsing

Audio Playback

Canvas/Image Processing

Clipboard Access

Web Workers

Best Practices

Common Mistakes

Migration from MV2

Part of the Chrome Extension Guide by theluckystrike. Built at zovo.one.