神刀安全网

Permissions API

Many of the functionalities that we’re translated from mobile to the web require permission from the user.  Think about geolocation, audio/video access (think getUserMedia for camera access ), and likewise APIs.  We can probably all agree that requiring permission for access to these APIs is a good thing, but I see a problem:  there’s sometimes no way to access each APIs permission level without triggering a request to the user to get that information.  Obtrusive to say the least!

I recently discovered the Permissions API which provides a method to query the permission level for an API without trigger a request to the user for access.  Let’s look at a simple example:

// Get the geolocation status (starts out as "prompt") // ... meaning the user will be shown an access request if we want it navigator.permissions.query({ name: 'geolocation' }).then(function(result) {     /* result.status = "prompt" */ });  // Request geolocation access if we really want it navigator.geolocation.getCurrentPosition(function(result) { /* ... */  })  // Assuming the user requested access, the permission is now "granted" navigator.permissions.query({ name: 'geolocation' }).then(function(result) {     /* result.status = "granted" */ });  // Push notifications require options: navigator.permissions.query({ name: 'push', userVisibleOnly:true }).then(function(result) { /* ... */ });

I love this new API — a clear path for getting a permission level without needing to request any information from the user. For example: if the permission level is negative, don’t ask the user permission to do something, or prompt the user in another fashion to allow a given permission.

Do you see another advantage to this API? Share!

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Permissions API

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址