Permissions API

By  on  

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!

Recent Features

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

  • By
    Animated 3D Flipping Menu with CSS

    CSS animations aren't just for basic fades or sliding elements anymore -- CSS animations are capable of much more.  I've showed you how you can create an exploding logo (applied with JavaScript, but all animation is CSS), an animated Photo Stack, a sweet...

Incredible Demos

  • By
    Face Detection with jQuery

    I've always been intrigued by recognition software because I cannot imagine the logic that goes into all of the algorithms. Whether it's voice, face, or other types of detection, people look and sound so different, pictures are shot differently, and from different angles, I...

  • By
    MooTools Text Flipping

    There are lots and lots of useless but fun JavaScript techniques out there. This is another one of them. One popular April Fools joke I quickly got tired of was websites transforming their text upside down. I found a jQuery Plugin by Paul...

Discussion

  1. Adam

    Like for some apps, we can show a nice information that we are going to ask you permission for ex. geolocation and explain why would we need this, if you tell me what are you going to do with this, then I am more about accepting…

  2. Vladimir Georgiev

    It seems in Chrome you need to look for result.state, and not result.status

  3. Thank you for sharing :)

    I’ve had some issues with one name though: microphone. If I query many of the other names possible, I get the correct status. But ‘microphone’ throws an error.

    Is there any way to make javascript show a list of all supported names in the current browser?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!