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!
我们从手机到网络的许多功能都需要得到用户的许可。 考虑一下地理位置,音频/视频访问( 对于摄像头访问 ,请考虑使用getUserMedia ),以及类似的API。 我们可能都同意,要求获得对这些API的访问权限是一件好事,但我看到一个问题:有时,在不触发用户获取该信息的请求的情况下,无法访问每个API的权限级别。 至少可以这么说!
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:
最近,我发现了Permissions API ,它提供了一种查询API权限级别的方法,而无需触发用户访问请求。 让我们看一个简单的例子:
// 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.
我喜欢这个新的API,这是获取权限级别的清晰路径,而无需从用户那里请求任何信息。 例如:如果权限级别为负,则不要要求用户权限做某事,也不要以其他方式提示用户允许给定权限。
Do you see another advantage to this API? Share!
您看到此API的另一个优势了吗? 分享!
api权限