Permissions.query() - Web APIs 编辑

Experimental

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The Permissions.query() method of the Permissions interface returns the state of a user permission on the global scope.

Syntax

navigator.permissions.query(PermissionDescriptor).then(function(permissionStatus) { ... })

Parameters

PermissionDescriptor
An object that sets options for the query operation consisting of a comma-separated list of name-value pairs. The available options are:
  • name: The name of the API whose permissions you want to query. An up-to-date list of permission names can be found in the spec under the PermissionName enum, but bear in mind that the actual permissions supported by browsers is currently much smaller than this. Firefox for example currently supports geolocation, notifications, push, and persistent-storage (see our Permissions.webidl file).
  • userVisibleOnly: (Push only, not supported in Firefox — see the Browser Support section below) Indicates whether you want to show a notification for every message or be able to send silent push notifications. The default is false.
  • sysex: (Midi only) Indicates whether you need and/or receive system exclusive messages. The default is false.

Note: As of Firefox 44, the permissions for Notifications and Push have been merged. If permission is granted (e.g. by the user, in the relevant permissions dialog), navigator.permissions.query() will return true for both notifications and push.

Note: The persistent-storage permission allows an origin to use a persistent box (i.e persistent storage) for its storage, as per the Storage API.

Returns

A Promise that resolves to a PermissionStatus object.

Exceptions

ExceptionExplanation
TypeErrorRetrieving the PermissionDescriptor information failed in some way, or the permission doesn't exist or is currently unsupported (e.g. midi, or push with userVisibleOnly).

Example

navigator.permissions.query({name:'geolocation'}).then(function(result) {
 if (result.state == 'granted') {
   showLocalNewsWithGeolocation();
 } else if (result.state == 'prompt') {
   showButtonToEnableLocalNews();
 }
 // Don't do anything if the permission was denied.
});

Specifications

SpecificationStatusComment
Permissions
The definition of 'query()' in that specification.
Working DraftInitial definition.

Browser compatibility

BCD tables only load in the browser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:148 次

字数:4854

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文