Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code samples are licensed under the BSD License.
©2012 Google
Use the chrome.permissions
module to implement
optional permissions. You can request optional permissions during your
extension's regular application flow rather than at install time, so users
understand why the permissions are needed and use only those that are
necessary.
For general information about permissions and details about each permission, see the permissions section of the manifest documentation.
Extensions should generally require permissions when they are needed for the extension's basic functionality and employ optional permissions for optional features.
Advantages of optional permissions:
Advantages of required permissions:
Declare optional permissions in your extension
manifest with the optional_permissions
key, using the
same format as the permissions
field:
{ "name": "My extension", ... "optional_permissions": [ "tabs", "http://www.google.com/" ], ... }
You can specify any of the following as optional permissions:
Request the permissions from within a user gesture using
permissions.request()
:
document.querySelector('#my-button').addEventListener('click', function(event) { // Permissions must be requested from inside a user gesture, like a button's // click handler. chrome.permissions.request({ permissions: ['tabs'], origins: ['http://www.google.com/'] }, function(granted) { // The callback argument will be true if the user granted the permissions. if (granted) { doSomething(); } else { doSomethingElse(); } }); });
Chrome prompts the user if adding the permissions results in different warning messages than the user has already seen and accepted. For example, the previous code might result in a prompt like this:
To check whether your extension has a specific permission or set of
permissions, use permission.contains()
:
chrome.permissions.contains({ permissions: ['tabs'], origins: ['http://www.google.com/'] }, function(result) { if (result) { // The extension has the permissions. } else { // The extension doesn't have the permissions. } });
You should remove permissions when you no longer need them.
After a permission has been removed, calling
permissions.request()
usually adds the permission back without
prompting the user.
chrome.permissions.remove({ permissions: ['tabs'], origins: ['http://www.google.com/'] }, function(removed) { if (removed) { // The permissions have been removed. } else { // The permissions have not been removed (e.g., you tried to remove // required permissions). } });
Gets the extension's current set of permissions.
Checks if the extension has the specified permissions.
The callback parameter should specify a function that looks like this:
function(boolean result) {...};
Requests access to the specified permissions. These permissions must be defined in the optional_permissions field of the manifest. If there are any problems requesting the permissions, chrome.extension.lastError will be set.
If you specify the callback parameter, it should specify a function that looks like this:
function(boolean granted) {...};
Removes access to the specified permissions. If there are any problems removing the permissions, chrome.extension.lastError will be set.
If you specify the callback parameter, it should specify a function that looks like this:
function(boolean removed) {...};
Fired when the extension acquires new permissions.
Fired when access to permissions has been removed from the extension.