Permissions API - patrickcole/learning GitHub Wiki

Permissions API

Provides a single resource to check what permissions have been enabled/disabled on user's device

For example, checking the permissions for the camera:

navigator.permissions
  .query({ name: 'camera' })
  .then( status => console.log(status) );

// => { status: "prompt", onchange: null }

Can also plug into when permissions are updated:

navigator.permissions
  .query({name:'camera'})
  .then(res => {
    res.onchange = ( (e) => {
      // detecting if the event is a change
      if (e.type === 'change'){
        // checking what the new permissionStatus state is
        const newState = e.target.state
        if (newState === 'denied') {
          console.log('why did you decide to block us?')
        } else if (newState === 'granted') {
          console.log('We will be together forever!')
        } else {
          console.log('Thanks for reverting things back to normal')
        }
      }
    })
  });

Sources