1 <h2 id=
"howto"> Implementing optional permissions
</h2>
4 Step
1: Decide which permissions are required and which are optional
8 An extension can declare both required and optional permissions. In general, you should:
10 <li>Use required permissions when they are needed for your extension’s basic functionality.
</li>
11 <li>Use optional permissions when they are needed for optional features in your extension.
</li>
16 Advantages of
<em>required
</em> permissions:
18 <li><strong>Fewer prompts:
</strong>
19 An extension can prompt the user once to accept all permissions.
</li>
20 <li><strong>Simpler development:
</strong>
21 Required permissions are guaranteed to be present.
</li>
26 Advantages of
<em>optional
</em> permissions:
28 <li><strong>Better security:
</strong>
29 Extensions run with fewer permissions since users only enable permissions that are needed.
</li>
30 <li><strong>Better information for users:
</strong>
31 An extension can explain why it needs a particular permission when the user enables the relevant feature.
</li>
32 <li><strong>Easier upgrades:
</strong>
33 When you upgrade your extension, Chrome will not disable it for your users if the upgrade adds optional rather than required permissions.
</li>
37 <h3 id=
"manifest"> Step
2: Declare optional permissions in the manifest
</h3>
39 Declare optional permissions in your
<a href=
"manifest.html">extension
40 manifest
</a> with the
<code>optional_permissions
</code> key, using the
41 same format as the
<a href=
"declare_permissions.html#manifest">permissions
</a>
45 <pre data-filename=
"manifest.json">
47 "name":
"My extension",
49 <b>"optional_permissions": [
"tabs",
"http://www.google.com/" ],
</b>
55 You can specify any of the following as optional
56 <a href=
"declare_permissions.html">permissions
</a>:
58 <li><i>host permissions
</i></li>
61 <li>clipboardRead
</li>
62 <li>clipboardWrite
</li>
63 <li>contentSettings
</li>
70 <li>notifications
</li>
74 <li>webNavigation
</li>
76 <li>webRequestBlocking
</li>
80 <p>If you want to request hosts that you only discover at runtime, include
81 <code>"http://*/"</code> and/or
<code>"https://*/"</code> as an
82 <code>optional_permission
</code>. This lets you specify any origin in
83 $ref:Permissions.origins as long as it has a matching scheme.
86 <h3 id=
"request"> Step
3: Request optional permissions
</h3>
88 Request the permissions from within a user gesture using
89 <code>permissions.request()
</code>:
91 document.querySelector('#my-button').addEventListener('click', function(event) {
92 // Permissions must be requested from inside a user gesture, like a button's
94 chrome.permissions.request({
95 permissions: ['tabs'],
96 origins: ['http://www.google.com/']
97 }, function(granted) {
98 // The callback argument will be true if the user granted the permissions.
110 Chrome prompts the user if adding the permissions results in different
111 <a href=
"permission_warnings.html">warning messages
</a> than the user has
112 already seen and accepted. For example, the previous code might result in
116 <p style=
"text-align: center">
117 <img src=
"{{static}}/images/perms-optional.png"
118 alt=
"example permission confirmation prompt"
119 width=
"490" height=
"193">
122 <h3 id=
"contains"> Step
4: Check the extension's current permissions
</h3>
124 To check whether your extension has a specific permission or set of
125 permissions, use
<code>permission.contains()
</code>:
129 chrome.permissions.contains({
130 permissions: ['tabs'],
131 origins: ['http://www.google.com/']
132 }, function(result) {
134 // The extension has the permissions.
136 // The extension doesn't have the permissions.
141 <h3 id=
"remove"> Step
5: Remove the permissions
</h3>
143 You should remove permissions when you no longer need them.
144 After a permission has been removed, calling
145 <code>permissions.request()
</code> usually adds the permission back without
150 chrome.permissions.remove({
151 permissions: ['tabs'],
152 origins: ['http://www.google.com/']
153 }, function(removed) {
155 // The permissions have been removed.
157 // The permissions have not been removed (e.g., you tried to remove
158 // required permissions).