You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Register a defined Content Script at runtime #2040
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/background.tsimportmyContentfrom"./entrypoints/my.content";exportdefaultdefineBackground(()=>{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 laterbrowser.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.
Until something is changed here what you can do is use a wxt module to do this for you. We are currently doing this right now.
Here some guides:
entrypoints:resolved: use entrypoints:resolved to hook into when entrypoint's are loaded from here you should be able to access registration and then save these to some object in the module.
Once you do all this you can then import the above in the background and then pass that along to registerContentScripts. Im using something like the following to do this but still early on testing soo may need some updates. Mine is strictly for optional content scripts tho soo just change some of the naming 😃
import{optionalContentScripts}from'#optional-content-scripts';constID_PREFIX='optional:';functiongetScriptId(js: string[]): string{return`${ID_PREFIX}${js[0]}`;}exportasyncfunctionregisterOptionalContentScripts(): Promise<void>{constregistered=awaitbrowser.scripting.getRegisteredContentScripts();// Unregister any stale optional scripts from previous versionsconststaleIds=registered.filter((s)=>s.id.startsWith(ID_PREFIX)).map((s)=>s.id);if(staleIds.length>0){awaitbrowser.scripting.unregisterContentScripts({ids: staleIds});}if(optionalContentScripts.length===0)return;// Register current optional content scriptsawaitbrowser.scripting.registerContentScripts(optionalContentScripts.map((script)=>({
...script,id: getScriptId(script.js),}))asBrowser.scripting.RegisteredContentScript[]);}
I'd be down to create an official WXT module for this, similar to @wxt-dev/auto-icons. I don't think every WXT project needs this, so it shouldn't go in the core wxt package.
Generating a module in the .wxt dir like @marcellino-ornelas mentioned is the "correct" way to do this today. This is what you should do anytime you need to create and reference generated variables.
@aklinker1 — I can take this on as a separate @wxt-dev/* module. Before I open a PR, can you sanity-check the shape?
Read your comments across #2239 and #2288. As I understand it: the build-system side stays in core (PR #2288, with optionalMatches
always going to optional_host_permissions per your May 12 call), and the runtime orchestration sits outside core. This module would be
the runtime orchestration piece — purely surfacing the metadata WXT already has so the user can call browser.scripting.registerContentScripts() themselves. Dev reload already only updates existing user-registered scripts
(reloadRuntimeContentScriptMv3 calls updateContentScripts, never registerContentScripts), so the module just emits a typed array
the user passes to their own registration call.
// wxt.config.tsexportdefaultdefineConfig({modules: ['@wxt-dev/runtime-content-scripts'],});// entrypoints/background.tsimport{runtimeContentScripts,registerRuntimeContentScripts,}from'#runtime-content-scripts';browser.runtime.onInstalled.addListener(async()=>{awaitregisterRuntimeContentScripts();// or directly: await browser.scripting.registerContentScripts(runtimeContentScripts);});
Open questions:
Name.@wxt-dev/runtime-content-scripts is most literal but overlaps the keyword; @wxt-dev/dynamic-content-scripts reads
cleaner. Preference?
Helper or just the array for v0? Helper would do unregister-stale + register with a prefixed ID scheme (default wxt-rcs:<name>), so calling it from onInstalled is idempotent. Otherwise just the array.
optionalMatches (per your feat: add content script registration optional #2288 pivot). Once that lands, the module would surface those separately — e.g. runtimeContentScripts for required matches, plus optionalContentScripts (or a sibling field on the same descriptor) for optionalMatches so the user can register those after the permission grant. Sound right, or did you have a different shape in mind?
getEntrypointBundlePath is internal. Happy to copy the small bit I need locally — only worth exposing from wxt/modules if
you'd prefer to keep that logic in one place.
@silverfish2525 let's circle back to the original problem: The OP doesn't want to pass the bundled paths as a string literal since it may change over time as the file is moved/renamed. Registering content scripts is just one example of a place where this would be useful, but it would also be useful for any functions that require a file path, like browser.runtime.getURL.
A simple solution to this would be to add a ref option (or something else) to entrypoints that you can set and doesn't change when the file is renamed:
Any you import it like import { ENTRYPOINT_CUSTOM_NAME } from '#entrypoints'
I would probably name this something like @wxt-dev/entrypoint-refs.
I'm not opposed to a module that does what you describe, but it seems quite niche - do all extensions that use dynamically registered content scripts need the behavior you described? Could you explain your use-case more so I have more context?
It just seems like there was a logical jump here I'm missing.
CSS: content scripts need both paths — { js, css } object or plain string?
You decide - whatever feels better once you have something up and running works for me.
Opt-in or auto: explicit ref: 'NAME' only, or every entrypoint by default?
If the separate module is installed and setup, I assume people want references for each entrypoint. So let's make it auto, using the entrypoint name by default, but respect custom refs.
HTML entrypoints (ENTRYPOINT_POPUP = '/popup.html') in scope?
Yes, I would say all entrypoints should be given a variable in the generated module.
Also, to respond to some of your other question that's relevant:
getEntrypointBundlePath is internal. Happy to copy the small bit I need locally — only worth exposing from wxt/modules if
you'd prefer to keep that logic in one place.
We can export it from wxt. That's be better than wxt/modules in this case, it can be used by other code, not just modules.
Activity
marcellino-ornelas commentedon Apr 10, 2026
Until something is changed here what you can do is use a wxt module to do this for you. We are currently doing this right now.
Here some guides:
entrypoints:resolved: useentrypoints:resolvedto hook into when entrypoint's are loaded from here you should be able to accessregistrationand then save these to some object in the module.registerContentScripts. Im using something like the following to do this but still early on testing soo may need some updates. Mine is strictly for optional content scripts tho soo just change some of the naming 😃marcellino-ornelas commentedon Apr 10, 2026
Still would like to see this as a first class wxt feature tho 🙌🏽
aklinker1 commentedon Apr 11, 2026
I'd be down to create an official WXT module for this, similar to
@wxt-dev/auto-icons. I don't think every WXT project needs this, so it shouldn't go in the corewxtpackage.Generating a module in the
.wxtdir like @marcellino-ornelas mentioned is the "correct" way to do this today. This is what you should do anytime you need to create and reference generated variables.andoan16 commentedon Apr 13, 2026
Auto-closing: linked PR #2258 was closed (CI checks failed). Sorry for the inconvenience.
silverfish2525 commentedon Jun 25, 2026
@aklinker1 — I can take this on as a separate
@wxt-dev/*module. Before I open a PR, can you sanity-check the shape?Read your comments across #2239 and #2288. As I understand it: the build-system side stays in core (PR #2288, with
optionalMatchesalways going to
optional_host_permissionsper your May 12 call), and the runtime orchestration sits outside core. This module would bethe runtime orchestration piece — purely surfacing the metadata WXT already has so the user can call
browser.scripting.registerContentScripts()themselves. Dev reload already only updates existing user-registered scripts(
reloadRuntimeContentScriptMv3callsupdateContentScripts, neverregisterContentScripts), so the module just emits a typed arraythe user passes to their own registration call.
Open questions:
Name.
@wxt-dev/runtime-content-scriptsis most literal but overlaps the keyword;@wxt-dev/dynamic-content-scriptsreadscleaner. Preference?
Helper or just the array for v0? Helper would do unregister-stale + register with a prefixed ID scheme (default
wxt-rcs:<name>), so calling it fromonInstalledis idempotent. Otherwise just the array.optionalMatches(per your feat: add content script registrationoptional#2288 pivot). Once that lands, the module would surface those separately — e.g.runtimeContentScriptsfor requiredmatches, plusoptionalContentScripts(or a sibling field on the same descriptor) foroptionalMatchesso the user can register those after the permission grant. Sound right, or did you have a different shape in mind?getEntrypointBundlePathis internal. Happy to copy the small bit I need locally — only worth exposing fromwxt/modulesifyou'd prefer to keep that logic in one place.
Timing. Wait for feat: add content script registration
optional#2288 to actually land before I open a PR, or fine to start scaffolding now against your stated direction?I'll hold off on the PR until you've weighed in.
aklinker1 commentedon Jun 28, 2026
@silverfish2525 let's circle back to the original problem: The OP doesn't want to pass the bundled paths as a string literal since it may change over time as the file is moved/renamed. Registering content scripts is just one example of a place where this would be useful, but it would also be useful for any functions that require a file path, like
browser.runtime.getURL.A simple solution to this would be to add a
refoption (or something else) to entrypoints that you can set and doesn't change when the file is renamed:Then the module would generate a file like this:
Any you import it like
import { ENTRYPOINT_CUSTOM_NAME } from '#entrypoints'I would probably name this something like
@wxt-dev/entrypoint-refs.I'm not opposed to a module that does what you describe, but it seems quite niche - do all extensions that use dynamically registered content scripts need the behavior you described? Could you explain your use-case more so I have more context?
It just seems like there was a logical jump here I'm missing.
silverfish2525 commentedon Jun 29, 2026
@aklinker1 Not all — only extensions using
registration: 'runtime'.Before starting on
packages/entrypoint-refs/:{ js, css }object or plain string?ref: 'NAME'only, or every entrypoint by default?ENTRYPOINT_POPUP = '/popup.html') in scope?aklinker1 commentedon Jun 29, 2026
You decide - whatever feels better once you have something up and running works for me.
If the separate module is installed and setup, I assume people want references for each entrypoint. So let's make it auto, using the entrypoint name by default, but respect custom refs.
Yes, I would say all entrypoints should be given a variable in the generated module.
aklinker1 commentedon Jun 29, 2026
Also, to respond to some of your other question that's relevant:
We can export it from
wxt. That's be better thanwxt/modulesin this case, it can be used by other code, not just modules.Please do this in a separate PR.