5

Is there a way to find out if the user has rejected or allowed permission to the media devices (Eg: Microphone, Camera) in Firefox?. In Chrome, I can check that with navigator.permissions.query but this fails in Firefox with a "TypeError".

navigator.permissions.query({name:"microphone"}).then(function(promise) {
   if ( promise && promise.state ) {
      console.log(promise.state); //"granted", "prompt" or "rejected"
    }
});
//in Firefox, It throws the error "TypeError: 'name' member of PermissionDescriptor '' is not a valid value for enumeration PermissionName"

I could not catch the above error with a try catch block for some reason. So I would like to know why I can't catch this error in a try catch block and If there is an alternative approach.

Use Case

My application has speech recognition feature. I need to show a "Pre permission pop up" before user encounters the actual "System dialog" seeking access to microphone. The idea behind this "pre permission pop up" is to give a context to the user why the application needs the access. If a user has already given/rejected the access then pre permission pop up would not be needed. So I need to check the microphone's permission state and show the pop up if needed.

Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
Murali Nepalli
  • 1,458
  • 4
  • 16
  • Hi, could it be a security issue? getUserMedia only works for secure contexts. It will not work in http unless you are in your localhost. Also try to run the code from this article when the window.onload https://blog.addpipe.com/common-getusermedia-errors/ – louie kim Dec 02 '19 at 01:13
  • @louiekim I see this problem over https too. It seems the navigator.permissions.query has limitations in firefox and edge. – Murali Nepalli Dec 02 '19 at 01:15
  • https://stackoverflow.com/questions/53147944/ Though for your case there might be hack-arounds, e.g `(await navigator.mediaDevices.enumerateDevices()).filter( ({kind}) => "audioinput" )[0].label !== ""` could tell us if a device's permission has been "granted", though it doesn't seem too reliable from my tests... Also note that depending on user's settings, UA may always ask for permissions, so maybe explaining your use case could be beneficial for the question. – Kaiido Dec 02 '19 at 02:47
  • @Kaiido I included the use case. – Murali Nepalli Dec 02 '19 at 05:32
  • ... then as I said, browsers let the user set rules where they'll get a prompt everytime, even though they already marked this website as being allowed to use their devices. I for one use this because I've got multiple audio input sources from which I like to always be able to choose. Even the Permissions API would give you false positives in that case. – Kaiido Dec 02 '19 at 05:36
  • @Kaiido Okay. Do you suggest any alternative approach? – Murali Nepalli Dec 02 '19 at 05:38
  • 1
    Always show the message? Or if you really think it's too cumbersome, store a cookie/pref in localStorage so it's shown only once per user. – Kaiido Dec 02 '19 at 05:40

1 Answers1

4

It's not possible

The Permissions API is an experimental technology currently under development:

Mozilla believes that the ability to work with user permissions is critical for user agency. There are certain aspects of the API that are not suitable for the permissions model used in Firefox and so we would like to work on improving several aspects of the API. In particular, we think that the way that status of permissions needs to more accurately reflect the different states that exist or could exist. We also think that the interactions with Feature Policy need to be better clarified. We're committed to fixing this, because permissions has become critical in making the web a more capable platform and it is important to ensure that we preserve user control over their online experience

Mozilla's position about Permissions API

Temo Tchanukvadze
  • 1,429
  • 4
  • 14
  • Yes, I understand that it's not possible with permission API. So, I am trying to figure out how can I catch this error with a try catch block or if is there any alternative approach. – Murali Nepalli Dec 11 '19 at 22:49
  • To catch exception just append `.catch(() => console.log('not suppported')). With 'try/catch` you can't catch exception if it's not an async function. – Temo Tchanukvadze Dec 12 '19 at 05:07
  • With `.catch`: https://jsfiddle.net/ek9mcx4f async function handling exception with `try/catch`: https://jsfiddle.net/d6huca1f/ – Temo Tchanukvadze Dec 12 '19 at 05:18
  • Also, check out this answer for an alternative approach. https://stackoverflow.com/questions/53147944/firefox-permission-name-member-of-permissiondescriptor-camera-is-not-a-vali – Temo Tchanukvadze Dec 12 '19 at 05:21
  • **it IS very possible** in chrome – DannyMoshe Dec 10 '20 at 17:39