permissions.request()
Asks the user for the permissions listed in the permissions.Permissions
object.
The Permissions
argument can contain an origins
property, an array of host permissions, a permissions
property, an array of API permissions, or both.
Requested permissions must be defined in the optional_permissions
manifest.json key. The origins
property can include permissions matching a subset of the hosts matched by an optional permission. For example, if optional_permissions
include "*://mozilla.org/"
, then permissions.origins
can include "https://developer.mozilla.org/"
.
Requests for optional-only permissions can't include any other optional permissions.
The request can only be made inside the handler for a user action. Unless all the permissions requested are ones granted silently, the browser asks the user whether to grant the requested permissions. One request is made for all requested permissions: either all permissions are granted or none are.
The extension retains any permissions granted, even over upgrade and disable and enable cycling.
This is an asynchronous function that returns a Promise
.
Syntax
let requesting = browser.permissions.request(
permissions // Permissions object
)
Parameters
permissions
-
A
permissions.Permissions
object.
Return value
A Promise
that is fulfilled with true
if the extension is granted the permissions listed in the permissions
argument, or false
otherwise.
Browser compatibility
BCD tables only load in the browser
Examples
This code adds a click handler that asks for various permissions, then logs the result of the request and the extension's permissions after the request completes.
const permissionsToRequest = {
permissions: ["bookmarks", "history"],
origins: ["https://developer.mozilla.org/"],
};
async function requestPermissions() {
function onResponse(response) {
if (response) {
console.log("Permission was granted");
} else {
console.log("Permission was refused");
}
return browser.permissions.getAll();
}
const response = await browser.permissions.request(permissionsToRequest);
const currentPermissions = await onResponse(response);
console.log(`Current permissions:`, currentPermissions);
}
document
.querySelector("#request")
.addEventListener("click", requestPermissions);
Example extensions
Note:
This API is based on Chromium's chrome.permissions
API.