Content Scripts
CRXJS provides content scripts with Vite HMR.
Static Assets
Feel free to import static assets! CRXJS automatically declares imported content
script dependencies as web_accessible_resources
in the manifest.
Use the extension URL
Content scripts share the origin of the host page, so convert imported static assets to the extension origin using the Chrome API.
HTML in content scripts
It is possible to inject an extension page into a host page using an iframe. The
host page CSP does not affect the injected iframe even if the host page
specifies the frame-src
policy.
An injected extension page loads inside a cross-origin iframe, so it does not have access to the host page DOM like a content script.
Injected extension pages do have access to the full Chrome API, however.
If you load an HTML file from a content script, you need to declare the file as a web-accessible resource.
You will also need to add the HTML file to your Vite config under
build.rollupOptions.input
.
Imported HTML
If you need to render complex HTML in a content script without a framework, an
HTML file can serve as a static fragment by importing it as text using the
?raw
query. This technique does not require the file to be web-accessible, and
you don't need to declare it in the Vite config.
Importing an HTML file as text lets you take advantage of IDE language services
for HTML files. Depending on your HTML, this technique may be more concise than
using document.createElement()
.