本文介绍了Firefox权限:PermissionDescriptor'camera'的'name'成员不是枚举PermissionName的有效值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个Web应用程序,该应用程序需要使用权限查询来检查是否已授予用户相机访问权限.

I'm making a web app that needs to check whether user camera access permission has been granted or not using permission query.

我尝试了以下代码:

navigator.permissions.query({name:'camera'}).then(function(result) {
 console.log(result);
});

它在 Google Chrome 70 上运行良好,但在firefox上却给我一个错误:

It ran fine on Google Chrome 70 but gave me an error on firefox:

TypeError:PermissionDescriptor'camera'的'name'成员不是枚举PermissionName的有效值.

我一直在寻找这个问题,但没有任何帮助.

I have been searching for this issue but nothing helped.

有人可以帮我吗?

谢谢

推荐答案

Permissions API被标记为一项实验性技术.

The Permissions API is marked as an experimental technology.

问题在于Firefox确实具有navigator.permissions并支持query方法,但是,它不支持 MDN的权限API"页面.

The issue is that Firefox does have navigator.permissions and support the query method on it, too, however, it does not support all the permission names that are listed on MDN's Permissions API page.

您可以自己尝试:转到Firefox上的控制台并执行

You can try this yourself: go to the console on Firefox and execute

// geolocation is working fine
navigator.permissions.query({ name: 'geolocation' }).then(console.log)

// camera, microphone is not supported, throws
navigator.permissions.query({ name: 'camera' })
// TypeError: 'name' member of PermissionDescriptor 'camera' is not a valid value for enumeration PermissionName.
navigator.permissions.query({ name: 'microphone' })
// TypeError: 'name' member of PermissionDescriptor 'microphone' is not a valid value for enumeration PermissionName.

mozilla/standards-positions 中有关Github的公开讨论在Permissions API上的位置.老实说,正如我所见,他们尚未得出任何结论.

There is an open discussion on Github in the mozilla/standards-positions about their position on the Permissions API. To be honest, as I see, they did not reach any conclusion yet.

您可以做的是创建一个基本功能,该功能可以在所有没有权限信息的浏览器上使用,并且可以在Chrome上使用Permissions.query逐步确定用户对摄像头和麦克风的权限,从而逐步提高用户体验.

What you can do is you create a basic functionality that works on all browsers without the permissions information and on Chrome, you progressively enhance the user experience by using the Permissions.query for figuring out the permissions for camera and microphone.

或者,您可以使用 MediaDevices.getUserMedia :例如,如果您唯一要确保您的应用具有麦克风和摄像头的权限,则可以调用getUserMedia并立即停止播放曲目.不过要小心,这有多个问题:

Alternatively, you can come up with some logic to handle this using MediaDevices.getUserMedia: for example, you can call getUserMedia and immediately stop the tracks, if the only thing you want is to ensure you app has permissions granted for microphone and camera. Be careful though, there are multiple problems with this:

  1. 相机灯将打开一秒钟
  2. 如果用户拒绝许可,您将很难再次请求它,因此在请求许可之前,您应该向用户说明为什么需要这些许可

这篇关于Firefox权限:PermissionDescriptor'camera'的'name'成员不是枚举PermissionName的有效值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 01:57