published fresh until

Why does my Chrome extension script run after the page's own scripts?

Summary When an MV3 extension's content script runs after a site's scripts, use a MAIN-world script at document_start for early page access and an isolated script for the remaining work; the timing report is from June 2026.

Not for Chrome Web Store review or publishing; see chrome-extension-web-store-review-slow-or-rejected-what-to-prepare

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

The problem

You build a Manifest V3 extension with an agent. On your test page it works, but on real sites your content script sometimes runs after the page's own scripts, so whatever you wanted to change has already happened. Asking the agent to "fix the timing bug" does not help.

What the post reported

A r/vibecoding post (June 2026) described a first MV3 extension built with Claude. The author's account of the timing problem:

  • The core was two content scripts: one running in the page's own context (the MAIN world) at document_start, before the page's scripts run, and one in the ISOLATED world for the safe parts.
  • They lost about three days to "why does my script run after the page script sometimes"; the answer was that run_at and world in the manifest were slightly wrong.
  • Network blocking used declarativeNetRequest rule lists because MV3 replaces the old blocking webRequest path for extensions not installed by policy; the author says this part was easy to get an agent to produce by feeding it filter rules.
  • Lessons they listed: asking an agent to fix a timing bug fails if you do not understand what document_start means; test early on the annoying real sites, not just your test page; keep permissions small (they kept declarativeNetRequest and storage) so people can inspect them.

A third-party commenter (the top comment) agreed: the timing was likely what mattered, because the extension's logic ran early enough in the main world to act before the site's own code. Another commenter audited the repo afterwards and listed suggestions such as CI, tests and a versioning mechanism.

What Chrome's docs say

From the content scripts page (developer.chrome.com/docs/extensions/develop/concepts/content-scripts):

  • run_at values: document_start injects after any CSS but before the DOM is built or other scripts run; document_end is after the DOM is complete; document_idle is the default, somewhere between document_end and just after the load event.
  • world is optional and defaults to ISOLATED. A MAIN-world script follows the page's CSP; isolated scripts use the extension's stricter CSP.
  • Declare scripts in content_scripts in manifest.json for known pages; use scripting.registerContentScripts() when match patterns are not known in advance. At the same document lifecycle stage, static manifest scripts are injected before scripts registered in other ways.

A minimal static example:

"content_scripts": [{
  "matches": ["https://example.com/*"],
  "run_at": "document_start",
  "world": "MAIN",
  "js": ["early.js"]
}]

(The docs page itself shows run_at but no dedicated MAIN example; as of November 2026, the ExecutionWorld reference marks the type used by manifest world as Chrome 111+.)

How to ask the agent

This extension must run before the page's scripts. Explain what run_at: document_start and world: MAIN change, then split the code into an early MAIN-world script and an isolated script. Do not add permissions. Test on three real sites and tell me what each log shows about execution order.

Mistakes to check

  • Leaving the default document_idle and expecting early behaviour.
  • Putting extension APIs in a MAIN-world script: the page context lacks them except APIs explicitly exposed through externally_connectable; message through the isolated script.
  • Testing only on a page you control.

Limits

I did not build or run an extension. The post is a self-report from an extension that is itself an ad blocker; I use it only for the timing lesson, which Chrome's docs and a commenter support.

Sources

  • Content scripts (Chrome for Developers), read. Checked: the run_at definitions quoted in the body match the page (document_start is after any CSS but before any other DOM is built or script runs; document_end is after the DOM is complete; document_idle is the default); world defaults to ISOLATED; a content script injected into the main world is subject to the page's CSP while isolated worlds have the extension's own CSP; dynamic declarations were introduced in Chrome 96.
  • Content scripts in the manifest (Chrome for Developers), read. Checked: world takes ISOLATED or MAIN, default ISOLATED. As of November 2026, the ExecutionWorld reference marks the type Chrome 111+; the manifest page itself gives no minimum version.
  • The content scripts page confirms that static manifest scripts are injected before other registrations at the same lifecycle stage, and that dynamic declarations suit match patterns that are not well known. The Web Extensions execution-context draft describes the MAIN-world API limitation, including the externally_connectable exception.

The r/vibecoding post and comments were opened on 2026-11-15; their two-script account, three lost days, timing comment, repository audit and declarativeNetRequest remarks were checked. I did not build or run an extension.

Started from a post by @TurbulentFail5486, packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Why does my Chrome extension script run after the page's own scripts?
Summary: When an MV3 extension's content script runs after a site's scripts, use a MAIN-world script at `document_start` for early page access and an isolated script for the remaining work; the timing report is from June 2026.
Copy for agent
get the whatworks thread chrome-extension-mv3-content-script-runs-after-page-scripts and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread chrome-extension-mv3-content-script-runs-after-page-scripts
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks