使用 js 启动 Google 语音识别会出错 - 不允许

Starting Google voice recognition with js gives error - not allowed

我正在尝试通过单击按钮开始语音识别以进行搜索,为此我使用了此代码 -

<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>

我从 recognition.onerror - SpeechRecognitionError {isTrusted: true, error: "not-allowed", message: "", type: "error", target: SpeechRecognition, …}

得到这个 e 值

我正在使用来自 labnol

的代码

我无法弄清楚问题所在,为什么会出现错误 - not allowed 谁能帮忙。

这是因为来源不受信任。

来源应至少匹配以下(方案、主机、端口)模式之一:

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

欲了解更多信息,请访问 - https://www.chromium.org/Home/chromium-security/prefer-secure-origins-for-powerful-new-features