权限 API

权限 API provides a consistent programmatic way to query the status of API permissions attributed to the current context. For example, the Permissions API can be used to determine if permission to access a particular API has been granted or denied.

概念和用法

Historically different APIs handle their own permissions inconsistently — for example the Notifications API allows for explicit checking of permission status and requesting permission, whereas the 地理位置 API doesn't (which causes problems if the user denied the initial permission request). The Permissions API provides the tools to allow developers to implement a better user experience as far as permissions are concerned.

permissions property has been made available on the Navigator object, both in the standard browsing context and the worker context ( WorkerNavigator — so permission checks are available inside workers), and returns a Permissions object that provides access to the Permissions API functionality.

Once you have this object you can then perform permission-related tasks, for example querying a permission using the Permissions.query() method to return a promise that resolves with the PermissionStatus for a specific API.

Not all APIs' permission statuses can be queried using the Permissions API. Notable APIs that are Permissions-aware include:

More APIs will gain Permissions API support over time.

范例

We have made a simple example available called Location Finder. You can run the example live ,或 view the source code on Github .

Read more about how it works in our article Using the Permissions API .

接口

Navigator.permissions and WorkerNavigator.permissions 只读
Provides access to the Permissions object from the main context and worker context respectively.
Permissions
Provides the core Permission API functionality, such as methods for querying and revoking permissions.
PermissionStatus
Provides access to the current status of a permission, and an event handler to respond to changes in permission status.

规范

规范 状态 Comment
Permissions 工作草案 初始定义。

浏览器兼容性

Permissions interface

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
Permissions Chrome 43 Edge 79 Firefox 46 IE 不支持 No Opera Yes Safari 不支持 No WebView Android 43 Chrome Android 43 Firefox Android 46 Opera Android Yes Safari iOS 不支持 No Samsung Internet Android 4.0
accelerometer permission Chrome 62 Edge 79 Firefox ? IE 不支持 No Opera ? Safari 不支持 No WebView Android 62 Chrome Android 62 Firefox Android ? Opera Android ? Safari iOS 不支持 No Samsung Internet Android 8.0
accessibility-events permission Chrome 62 Edge 79 Firefox ? IE 不支持 No Opera ? Safari 不支持 No WebView Android 62 Chrome Android 62 Firefox Android ? Opera Android ? Safari iOS 不支持 No Samsung Internet Android 8.0
ambient-light-sensor permission Chrome 62 Edge 79 Firefox ? IE 不支持 No Opera ? Safari 不支持 No WebView Android 62 Chrome Android 62 Firefox Android ? Opera Android ? Safari iOS 不支持 No Samsung Internet Android 8.0
background-sync permission Chrome 62 Edge 79 Firefox ? IE 不支持 No Opera ? Safari 不支持 No WebView Android 62 Chrome Android 62 Firefox Android ? Opera Android ? Safari iOS 不支持 No Samsung Internet Android 8.0
camera permission Chrome 64 Edge 79 Firefox ? IE 不支持 No Opera ? Safari 不支持 No WebView Android 64 Chrome Android 64 Firefox Android ? Opera Android ? Safari iOS 不支持 No Samsung Internet Android 9.0
clipboard-read permission Chrome 64 Edge 79 Firefox 不支持 No IE 不支持 No Opera ? Safari 不支持 No WebView Android 64 Chrome Android 64 Firefox Android 不支持 No Opera Android ? Safari iOS 不支持 No Samsung Internet Android 9.0
clipboard-write permission Chrome 64 Edge 79 Firefox 不支持 No IE 不支持 No Opera ? Safari 不支持 No WebView Android 64 Chrome Android 64 Firefox Android 不支持 No Opera Android ? Safari iOS 不支持 No Samsung Internet Android 9.0
geolocation permission Chrome 43 Edge 79 Firefox ? IE 不支持 No Opera 30 Safari 不支持 No WebView Android 不支持 No Chrome Android 43 Firefox Android ? Opera Android 30 Safari iOS 不支持 No Samsung Internet Android 4.0
gyroscope permission Chrome 51 Edge 79 Firefox ? IE 不支持 No Opera ? Safari 不支持 No WebView Android 51 Chrome Android 51 Firefox Android ? Opera Android ? Safari iOS 不支持 No Samsung Internet Android 5.0
magnetometer permission Chrome 62 Edge 79 Firefox ? IE 不支持 No Opera ? Safari 不支持 No WebView Android 62 Chrome Android 62 Firefox Android ? Opera Android ? Safari iOS 不支持 No Samsung Internet Android 8.0
microphone permission Chrome 64 Edge 79 Firefox ? IE 不支持 No Opera ? Safari 不支持 No WebView Android 64 Chrome Android 64 Firefox Android ? Opera Android ? Safari iOS 不支持 No Samsung Internet Android 9.0
midi permission Chrome 43 Edge 79 Firefox ? IE 不支持 No Opera 30 Safari 不支持 No WebView Android 不支持 No Chrome Android 43 Firefox Android ? Opera Android 30 Safari iOS 不支持 No Samsung Internet Android 4.0
notifications permission Chrome 43 Edge 79 Firefox ? IE 不支持 No Opera 30 Safari 不支持 No WebView Android 不支持 No Chrome Android 43 Firefox Android ? Opera Android 30 Safari iOS 不支持 No Samsung Internet Android 4.0
payment-handler permission Chrome 66 Edge 79 Firefox ? IE 不支持 No Opera ? Safari 不支持 No WebView Android 66 Chrome Android 66 Firefox Android ? Opera Android ? Safari iOS 不支持 No Samsung Internet Android 9.0
persistent-storage permission Chrome 71 Edge 79 Firefox 53 IE 不支持 No Opera 58 Safari 不支持 No WebView Android 71 Chrome Android 71 Firefox Android 53 Opera Android 50 Safari iOS 不支持 No Samsung Internet Android 10.0
push permission Chrome 43 Edge 79 Firefox ? IE 不支持 No Opera 30 Safari 不支持 No WebView Android 不支持 No Chrome Android 43 Firefox Android ? Opera Android 30 Safari iOS 不支持 No Samsung Internet Android 4.0
query Chrome 43 Edge 79 Firefox 46 IE 不支持 No Opera Yes Safari 不支持 No WebView Android 43 Chrome Android 43 Firefox Android 46 Opera Android Yes Safari iOS 不支持 No Samsung Internet Android 4.0
request
非标
Chrome 46 Edge 79 Firefox 不支持 No IE 不支持 No Opera ? Safari 不支持 No WebView Android 46 Chrome Android 46 Firefox Android 不支持 No Opera Android ? Safari iOS 不支持 No Samsung Internet Android 5.0
requestAll
非标
Chrome 48 Edge 79 Firefox 不支持 No IE 不支持 No Opera ? Safari 不支持 No WebView Android 48 Chrome Android 48 Firefox Android 不支持 No Opera Android ? Safari iOS 不支持 No Samsung Internet Android 5.0
revoke
非标
Chrome 46 Edge 79 Firefox 51
Disabled
51
Disabled
Disabled From version 51: this feature is behind the dom.permissions.revoke.enable preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
不支持 47 — 51
IE 不支持 No Opera ? Safari 不支持 No WebView Android 46 Chrome Android 46 Firefox Android 51
Disabled
51
Disabled
Disabled From version 51: this feature is behind the dom.permissions.revoke.enable preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
不支持 47 — 51
Opera Android ? Safari iOS 不支持 No Samsung Internet Android 5.0

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
非标。预期跨浏览器支持较差。
非标。预期跨浏览器支持较差。
用户必须明确启用此特征。
用户必须明确启用此特征。

另请参阅