英文:
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: 'microphone'});
return permission.state === 'granted';
}
function checkMicrophonePermission() {
var permission = navigator.permissions.query({name: 'microphone'});
if(permission.state == "prompt"){
// Do something
}
}
// Check if microphone permission is blocked by an extension
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论