0

how to change the permissionStatus state to "granted" if it is "denied" or "prompt" in javascript? can anyone answer this question please? thanks in advance

navigator.permissions.query({name: 'microphone'}).then(permissionStatus => { // code });

parthi
  • 1
  • 1
  • 4
    That would go against the point of "permissions" – evolutionxbox Jan 09 '21 at 22:50
  • I think it will be the same code https://developer.mozilla.org/en-US/docs/Web/API/Navigator/permissions – Daniil Loban Jan 09 '21 at 22:53
  • yeah but it is against of permissions but is there any possibility to change the state of the permission ? thanks in advance @evolutionxbox – parthi Jan 09 '21 at 22:58
  • 1
    @parthipartha if you could there’d be no point of asking for permission – evolutionxbox Jan 09 '21 at 22:59
  • @DaniilLoban actually i tried to see the docs but actually the state of the permissionStatus is readonly property so we cannot change directly but we can possibly do that by creating a new instance of PermissionStatus but i didnot know how to create the new instance for that – parthi Jan 09 '21 at 23:11
  • @evolutionxbox you are right but would like to access the microphone but not reprompt the popup. By implementing a button in my interface and change the permission directly to 'denied' or 'granted' – parthi Jan 09 '21 at 23:17
  • this is example for enable and revoke: https://github.com/chrisdavidmills/location-finder-permissions-api/tree/gh-pages – Daniil Loban Jan 09 '21 at 23:17
  • What do you mean by changing the state of the permission @parthi? Do you want to use the microphone even when a user denied to give you permission? If so that is not possible. The way change that status is by telling the browser to ask the user for permission. – Ivar Jan 09 '21 at 23:17
  • I checked example above it doesn't work, I'm sorry... This feature is obsolete. – Daniil Loban Jan 09 '21 at 23:20
  • @Ivar no actually if the user denied the microphone i would like to set up a button in my interface and that button need to change the permissionstatus directly to "granted" if it is "denied" instead of going to the browser settings -> exeception -> microphone -> change to allow etc.. – parthi Jan 09 '21 at 23:23
  • If it were possible to change this setting directly from JavaScript, it would mean that _every_ website could start listening to your microphone without the user's consent. Browsers want to protect their users so this is not possible. If you ask properly for permission and the users denies this, then the only way to undo this is indeed via the settings of your browser. See [reprompt for permissions with getUserMedia() after initial denial](https://stackoverflow.com/questions/15993581/reprompt-for-permissions-with-getusermedia-after-initial-denial) – Ivar Jan 10 '21 at 00:12
  • @Ivar yes true. Anyway thank you, i tried something but i did not achieve. – parthi Jan 10 '21 at 11:10

1 Answers1

0

Try it, this doesn't work in this snippet but works in a file.

examples

<!DOCTYPE html>
 <html>
 <head>
   <title></title>
 </head>
 <body>
 <script type="text/javascript">
    let havePermissions = false
    const permissions = navigator
       .mediaDevices
       .getUserMedia({audio: true, video: false})
    permissions.then((stream) => {
      alert('accepted the permissions');
      havePermissions: !havePermissions
    })
    .catch((err) => {
        havePermissions: false
      console.log(`${err.name} : ${err.message}`)
    });
 </script>
 </body>
 </html> 
Daniil Loban
  • 4,165
  • 1
  • 14
  • 20
  • thanks @Danniil but it will give a popup of microphone and i need to click the allow button to activate the microphone but what i want is instead of the popup window once the user is denied or blocked the prompt there is a custom button in my interface and the button clicks by the user the microphone should activate from the browser by default – parthi Jan 09 '21 at 23:40
  • I think that is impossible, but if you find way, please share it) and user can save permissions for every site at once. – Daniil Loban Jan 09 '21 at 23:42
  • yeah for sure, i am trying to change the state of the permission but i haven't achieved yet, but i think its possible. thanks – parthi Jan 09 '21 at 23:50
  • I think if it were possible to install them programmatically without the popup, then any site could turn on the user's microphone and camera without prompting. But good luck! – Daniil Loban Jan 09 '21 at 23:58
  • yeah you have a point but lets give a try to access – parthi Jan 10 '21 at 00:01
  • @DaniilLoban if it is possible then, imo, it needs to be made impossible. – evolutionxbox Jan 10 '21 at 12:26
  • @evolutionxbox I absolutely agree with you – Daniil Loban Jan 10 '21 at 12:33
  • @DaniilLoban I was a little worried when I saw this question had an answer. Good on you for sharing your effort though. – evolutionxbox Jan 10 '21 at 12:34