Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 测试navigator.getUserMedia是否需要https而不需要浏览器用户代理嗅探_Javascript_Google Chrome_Firefox_Https_Getusermedia - Fatal编程技术网

Javascript 测试navigator.getUserMedia是否需要https而不需要浏览器用户代理嗅探

Javascript 测试navigator.getUserMedia是否需要https而不需要浏览器用户代理嗅探,javascript,google-chrome,firefox,https,getusermedia,Javascript,Google Chrome,Firefox,Https,Getusermedia,我想使用Javascript来确定浏览器是否支持通过http访问网络摄像头(navigator.getUserMedia),以防我不想将用户重定向到https。但是,如果浏览器支持网络摄像头访问而不需要https(例如,firefox),我根本不想将用户重定向到https。我想专门使用http为站点提供服务。我目前的解决方案是将浏览器的用户代理移动到la: var isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') &

我想使用Javascript来确定浏览器是否支持通过http访问网络摄像头(navigator.getUserMedia),以防我不想将用户重定向到https。但是,如果浏览器支持网络摄像头访问而不需要https(例如,firefox),我根本不想将用户重定向到https。我想专门使用http为站点提供服务。我目前的解决方案是将浏览器的用户代理移动到la:

var isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
我目前的解决方案在理论上会崩溃,因为Firefox可能会在未来几年的某个时候修补对网络摄像头访问的http支持

这是我不愿意在所有浏览器上推广https的一个原因,但我不想进入这个阶段,但我意识到浏览器世界正在朝着一个总有一天到处都需要https的方向发展

!!navigator.getUserMedia
这将返回true无论我是在http还是https中运行chrome,我希望在chrome的当前版本(而不是firefox)上运行http时生成一个假值,我之所以想这样做而不是用户代理嗅探,是因为浏览器在http上修补对网络摄像头访问的支持,我希望我的代码能够适应,而不需要修补


简而言之,如何在不查找特定浏览器用户代理的情况下检测支持http视频功能的浏览器?

我想我找到了一个解决方案,我已经在safari、firefox和chrome上进行了测试:

if(navigator.mediaDevices){
  navigator.mediaDevices.getUserMedia({video:true}).then(function(){
    //firefox, chrome in https
    alert('success!'); 
  }, function(){
    //chrome in http
    alert('failure!'); 
  });
} else {
  //safari, ie
  alert('not supported');
}

然而,这在运行时会弹出一个权限气泡,这是不可取的,我想找到一个解决方案,在检测过程中不会打扰用户。

以下内容将检测Chrome的https行为,而不会出现提示:

(在Chrome中使用,因为getUserMedia在Chrome中不起作用,所以代码片段在Chrome中不起作用)

if('https:'==document.location.protocol){
log(“页面是https”);
}否则{
navigator.mediaDevices.getUserMedia({video:{width:{min:2,max:1}})
。然后(流=>{
日志(“检测失败!”);
stream.getTracks().forEach(t=>t.stop());
})
.catch(e=>{
开关(例如名称){
案例“NotSupportedError”:
案例“NotAllowedError”:
案例“SecurityError”:
log(“不允许http中的getUserMedia”);
打破
案例“过度约束错误”:
违约:
log(“允许http中的getUserMedia”);
打破
}
});

}
如果这是在ES5@denodster将
foo=>bar
替换为
function(foo){returnbar;}
应该可以。