javascripthtmlgoogle-apigoogle-voice

Starting Google voice recognition with js gives error - not allowed


I am trying to start voice recognition with a button click for search, for that I use this code -

<style>
  .speech {border: 1px solid #DDD; width: 300px; padding: 0; margin: 0}
  .speech input {border: 0; width: 240px; display: inline-block; height: 30px;}
  .speech img {float: right; width: 40px }
</style>

<!-- Search Form -->
<form id="labnol" method="get" action="https://www.google.com/search">
  <div class="speech">
    <input type="text" name="q" id="transcript" placeholder="Speak" />
    <img onclick="startDictation()" src="//i.imgur.com/cHidSVu.gif" />
  </div>
</form>

<!-- HTML5 Speech Recognition API -->
<script>
  function startDictation() {

    if (window.hasOwnProperty('webkitSpeechRecognition')) {

      var recognition = new webkitSpeechRecognition();

      recognition.continuous = false;
      recognition.interimResults = false;

      recognition.lang = "en-US";
      recognition.start();

      recognition.onresult = function(e) {
        document.getElementById('transcript').value
                                 = e.results[0][0].transcript;
        recognition.stop();
        document.getElementById('labnol').submit();
      };

      recognition.onerror = function(e) {
        console.log(e);
        recognition.stop();
      }

    }
  }
</script>

I am getting this value of e from recognition.onerror - SpeechRecognitionError {isTrusted: true, error: "not-allowed", message: "", type: "error", target: SpeechRecognition, …}

I am using this code from labnol

I am unable to figure out the problem, why I am getting error - not allowed can anybody help.


Solution

  • This is because origin is not trusted.

    origins should match at least one of the following (scheme, host, port) patterns:

    (https, *, *)
    (wss, *, *)
    (*, localhost, *)
    (*, 127/8, *)
    (*, ::1/128, *)
    (file, *, —)
    (chrome-extension, *, —) 
    

    for more clearification visit - https://www.chromium.org/Home/chromium-security/prefer-secure-origins-for-powerful-new-features