wxt-dev / wxt-dev/wxt

Add support for `world: "MAIN"` content scripts for MV2

Open
#796 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
10.5k
Forks
564
PR merge metrics
No merged PRs in 30d

Description

Feature Request

Main world content scripts are a feature of MV3. Currently, to use them in firefox MV2, you have to write a separate "loader" content script that sets up a <script> block:

// entrypoints/some-script-loader.content.ts
export default defineContentScript({
  include: ["firefox"],
  // ...
  main() {
    const script = document.createElement("script");
    script.innerHtml = await fetch(browser.runtime.getURL("content-scripts/some-script.js"))
      .then(res => res.text());
    document.head.append(script);
  },
});

You also need to add the content script to the web_accessible_resources.

It would be nice if WXT did this automatically for you.

Is your feature request related to a bug?

Kinda related to https://discord.com/channels/1212416027611365476/1258093235809685655. I suggested monkey-patching the fetch API from a main world content script and realized that would only work for MV3.

What are the alternatives?

Might be able to use the MV2 scripting APIs to execute the script instead, but that would probably require the tabs permission, which is not ideal.

Additional context

Add any other context or screenshots about the feature request here.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how WXT generates MV2 and MV3 content scripts and web_accessible_resources from entrypoints. Compare the existing MV3 main-world behavior with the loader pattern shown in the issue, then verify that an MV2 content script using world: "MAIN" is generated automatically without requiring a separate loader or manual resource declaration.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
build-system, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.