Sign inTry it

Content script

A content script is JavaScript that a browser extension injects into a web page. It can read and change the page's DOM. It runs in an isolated world, separate from the page's own JavaScript. Extensions use content scripts when they need to see or change what a user sees on a site.

Updated

How a content script works

An extension declares content scripts in its manifest, with match patterns for the sites they apply to. An extension can also inject a script at runtime with the scripting API. When a matching page loads, the browser runs the script in that page.

The script shares the page's DOM. It has its own JavaScript variables, so it cannot read variables that the page defines. To talk to the rest of the extension, it sends messages, usually to the background service worker.

Why it matters for AI agents

Content scripts let an extension see the page as the user sees it, including content that loads after the page starts. That makes them useful for reading results or filling in forms.

The same access carries a risk. The script runs inside every page it matches. Keep match rules narrow, and ask only for the permissions the extension needs.

In practice with AI agents

An agent-facing extension might use a content script to read the visible text of a results page and send it to the background worker. The page text was written by the site, so treat it as content, not as instructions for the agent.

FAQ

Can a content script read the page's JavaScript variables?

No. A content script runs in an isolated world. It shares the DOM with the page, but not the page's JavaScript objects or variables.

How is a content script different from a service worker?

A content script runs inside a web page, with access to that page's DOM. An extension service worker is the extension's background script. It has no page DOM, starts on events and runs apart from any one page.

Sources

  1. Content scriptsdeveloper.chrome.com