runtime.getURL()
Given a relative path from the manifest.json to a resource packaged with the extension, return a fully-qualified URL.
This function does not check that the resource actually exists at that URL.
Note:
The absence of the resource check impacts the use of fetch(). Because moz-extension: URLs are served through a custom (non-HTTP) channel, Firefox builds the Response object with ok: true and status: 200 before it has checked whether the resource exists. So, for a missing JSON file, fetch(browser.runtime.getURL("missing.json")).then(r => r.ok) resolves to true. There is no 404 to check for.
If your extension attempts to read the body, e.g., with response.json() or response.text(), it is rejected with DOMException: The operation was aborted. To detect a missing resource, wrap the body read in a try/catch rather than relying on response.ok:
try {
const response = await fetch(browser.runtime.getURL("beasts/frog.json"));
const data = await response.json(); // throws if frog.json doesn't exist
} catch (e) {
console.error("Resource missing or unreadable:", e);
}
Syntax
browser.runtime.getURL(
path // string
)
Parameters
path-
string. A relative path from the manifest.json to a resource packaged with the extension.
Return value
string. The fully-qualified URL to the resource within the extension.
Examples
Given a file packaged with the extension at "beasts/frog.html", get the full URL like this:
let fullURL = browser.runtime.getURL("beasts/frog.html");
console.log(fullURL);
// Returns something like:
// moz-extension://2c127fa4-62c7-7e4f-90e5-472b45eecfdc/beasts/frog.html
Example extensions
Browser compatibility
Note:
This API is based on Chromium's chrome.runtime API. This documentation is derived from runtime.json in the Chromium code.