我们的志愿者还没有将这篇文章翻译为 中文 (简体)。加入我们帮助完成翻译!
Type | Array |
---|---|
Mandatory | No |
Example |
"web_accessible_resources": [ "images/my-image.png" ] |
Sometimes you will want to package some resources - for example, images, HTML, CSS, or JavaScript - with your extension, and make them available to web pages.
For example, the "beastify" example extension used in the walkthrough tutorial replaces images in a web page with images of some beasts, by setting the src
attribute of any <img>
elements. The images are packaged with the extension, and for the web page to be able to load them, they must be made web accessible.
The web_accessible_resources
key lists all packaged resources that you want to make available to web pages in this way. You specify them as paths relative to the manifest.json file.
The files will then be available using a URL like: "moz-extension://<extension-id>/<path/to/resource>".
For example, consider an entry like this:
"web_accessible_resources": ["images/my-image.png"]
If your extension's ID is 944cfddf-7a95-3c47-bd9a-663b3ce8d699
, then this resource will be available at the following URL:
moz-extension://944cfddf-7a95-3c47-bd9a-663b3ce8d699/images/my-image.png
Entries can contain wildcards, for example:
"web_accessible_resources": ["images/*.png"]
Will also work.
The easiest way to get this URL, though, is to use the chrome.extension.getURL
API, and give it the path relative to manifest.json:
chrome.extension.getURL("images/my-image.png");
Note that content scripts don't need to be listed as web accessible resources.
Example
"web_accessible_resources": ["images/my-image.png"]
Make the file at "images/my-image.png" web accessible.