Strategies for injecting code with registerContentScripts
and CRXJS
#643
-
Hello, and thank you so much for this amazing project! It's really been indispensable, and the further I get with extension development, the more appreciation I gain for your efforts here 🙏🙏 I have a use case that I initially thought would be kinda common in MV3, but I haven't found a solid way to make it work yet and was hoping to get some ideas or guidance from more experienced folks. What I'm trying to doBroadly, I want to have users grant permission for content scripts to register themselves against a matching domain, instead of me having to list/update a set of domains in my manifest.json. So the goals are:
So far I've got everything working except successful execution of the script passed to Reproducing the issueHere's a repo that illustrates the issue: https://github.com/jonlong/crx-registercontentscripts.
Why do this?Really I just want to have a chance to explain to users what the permissions prompt their about to see means, and what we're planning to do (and not do) with their data. I could do this in the extension's description in the Chrome Web Store, but I doubt it would have the same read rate as something that is right next to the action itself. If I avoid listing any matching urls in my manifest.json, and instead specify Where I'm looking for helpI guess the content script import error is the most obvious place to start.
If there's not a CRXJS solution to this, how would you approach it?
I'd appreciate any thoughts or direction anyone's willing to share! Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 13 replies
-
This looks like an ideal way to register optional content scripts. 👍
I'd suggest removing the module param from the query string:
Content scripts use a wrapper script to import the actual script module and to support HMR in dev mode. The module param removes the wrapper, which is useful in some scenarios, but doesn't work for a registered content script. |
Beta Was this translation helpful? Give feedback.
-
My suggestion for this use case is to use two build processes:
CRXJS will copy the IIFE script file as-is, and you can develop the Chrome Extension using CRXJS with HMR, etc. |
Beta Was this translation helpful? Give feedback.
-
It's been a year and I'm wondering if there is a solution for HMR support for injected mainworld js-files - that doesn't require me to run a seperate --watch task? If someone can think of a better solution please share with the rest of us :) |
Beta Was this translation helpful? Give feedback.
-
I did some research and figured a few things out. Let me summarize this to anyone finding this issue:
You can follow solution 1/2 to have a setup that enables HMR and main world injecting without any hacks or extra stuff needed. Solution 1 - define loader in manifest:
Solution 2 - we use a registerContentScripts in the background
Followed by:
Hope this helps |
Beta Was this translation helpful? Give feedback.
My suggestion for this use case is to use two build processes:
main-world.ts -> main-world.iife.js
) in a separate process usingvite build --watch
in library mode without CRXJS.main-world.iife.js
) inweb_accessible_resources
in the manifest.background.ts
, register the content script asmain-world.iife.js
(don't import the script):