Web-accessible resources are files within an extension that can be accessed by web pages or other extensions. These resources are often used for images, scripts, or stylesheets that are required by content scripts or pages embedded within an extension's context.
Extension.js performs the following tasks for web-accessible resources:
manifest.json
file with paths for web-accessible resources
based on the content scripts and assets being used.The following manifest field is used to declare web-accessible resources:
Manifest Field | File Type Expected | HMR Support |
---|---|---|
web_accessible_resources |
Any file type | No |
manifest.json
FileIf the resources you need to declare are already imported by the content scripts, you don't need to declare
them in the web_accessible_resources
field. For all other cases, you can declare the resources in the manifest.json
file.
Here's an example of how to declare web-accessible resources in the manifest.json
file for manifest V3.
Here's an example of how to declare web-accessible resources in the manifest.json
file for manifest V2.
In the examples above, the web_accessible_resources
field exposes specific resources (images, scripts, styles)
for web pages that match the pattern defined in matches
.
For a resource to be web-accessible, it must be emitted to the output directory. The easiest way to achieve this
is to place the resources in the /public
folder. Assets declared in there will be automatically copied to the
output directory during the build process and will be accessible from the root of the extension.
In the example above, the icon.png
, content.js
, and content.css
files are web-accessible resources that can
be accessed by web pages or other extensions, so the way they should be declared in the web_accessible_resources
field would be:
For resources not declared in the web_accessible_resources
field, you can predict their path by examining the
/dist
folder after building the extension. The resources will be placed in the root of the output directory.
To reference them in your code, you can use the chrome.runtime.getURL
method.
dist/
folderIn the example above, the my-page.html
and page-0.html
files are web-accessible resources that can be accessed
by web pages or other extensions, so the way they should be declared in the web_accessible_resources
field would be:
web_accessible_resources
if they are intended to be accessible by web pages or
other external scripts./public
folder to keep your manifest.json
clean and organized.