In js is it possible to check if mic permission is given or not in browser or in which state it is or it is blocked by some extension?

huangapple go评论62阅读模式
英文:

In js is it possible to check if mic permission is given or not in browser or in which state it is or it is blocked by some extension?

问题

目前我正在进行一个项目,在这里,当权限处于提示状态时,我无法执行它,是否可以在JavaScript代码中获取权限状态?

实际上,如果我能获取到权限,那么我可以借助它向客户显示适当的消息。

英文:

Currently I am working on a project and here I can't it cause problem when permission is in prompt state . Is it possible to get the state of permission in JavaScript code ?

Actually If i get the acess of this then with the help of this I can show proper messages to the customer regarding that.

答案1

得分: 1

这可以很容易地这样做:

// 检查麦克风权限是否已授予
function checkMicrophonePermission() {
  var permission = navigator.permissions.query({name: 'microphone'});
  return permission.state === 'granted';
}

function checkMicrophonePermission() {
  var permission = navigator.permissions.query({name: 'microphone'});
  if(permission.state == "prompt"){
    // 做一些事情
  }
}

// 检查是否由扩展程序阻止了麦克风权限
function checkMicrophonePermissionBlockedByExtension() {
  var extensions = chrome.extensions.getInstalledExtensions();
  for (var i = 0; i < extensions.length; i++) {
    if (extensions[i].permissions && extensions[i].permissions.indexOf('microphone') !== -1) {
      return true;
    }
  }
  return false;
}

注意:这里的代码示例中存在两个相同名称的函数checkMicrophonePermission(),你可能需要修改其中一个函数的名称以避免冲突。

英文:

This can be easily this as :

// Check if microphone permission is granted
function checkMicrophonePermission() {
  var permission = navigator.permissions.query({name: &#39;microphone&#39;});
  return permission.state === &#39;granted&#39;;
}

function checkMicrophonePermission() {
  var permission = navigator.permissions.query({name: &#39;microphone&#39;});
  if(permission.state == &quot;prompt&quot;){
    // Do something
  }
}

// Check if microphone permission is blocked by an extension
function checkMicrophonePermissionBlockedByExtension() {
  var extensions = chrome.extensions.getInstalledExtensions();
  for (var i = 0; i &lt; extensions.length; i++) {
    if (extensions[i].permissions &amp;&amp; extensions[i].permissions.indexOf(&#39;microphone&#39;) !== -1) {
      return true;
    }
  }
  return false;
}

huangapple
  • 本文由 发表于 2023年6月12日 22:41:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定