wxt-dev / wxt-dev/wxt

Register a defined Content Script at runtime

Open
#2,040 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

contribution welcome good first issue
Dominant language
TypeScript
Stars
10.5k
Forks
564
PR merge metrics
No merged PRs in 30d

Description

Feature Request

I use defineContentScript() for some Logic behind Feature-Toggles. To dynamically inject them I have set registration: "runtime", as well.
When I now try to inject these at runtime using browser.scripting.registerContentScripts() I have to specify paths to css and js artifacts generated from my content script directory (e.g. bundled css files from scss code). This pretty much defeats the purpose of the easy to use wxt build system.

I would like to be able to reference my defined Content Script with variable/import References, for example like this:

// entrypoints/my.content/index.ts
export default defineContentScript({ registration: "runtime", ... })
// entrypoints/background.ts
import myContent from "./entrypoints/my.content";
export default defineBackground(() => {
	browser.scripting.registerContentScripts([myContent]); // <- not possible because types do not match
	// post-bundle information on the script (and its parts) is not exposed

	// ... some time later
	browser.scripting.unregisterContentScripts([myContent]);
});
Is your feature request related to a bug?

"N/A"

What are the alternatives?

Working around this by looking into the built artifacts and specifying every file for every content script manually.

Additional context

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 the defineContentScript declaration in entrypoints/my.content/index.ts and its use from entrypoints/background.ts. Read how runtime content scripts are represented after bundling, then verify that the resulting reference can be passed to browser.scripting.registerContentScripts() and unregisterContentScripts() without manually locating generated artifacts.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
build-system, developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.