Userscript that requires microphone access prompts for permissions in Chrome multiple times

元气小坏坏 提交于 2019-12-06 15:17:56

Chrome's speech recognition implementation simply stops from time-to-time. To handle this, annyang restarts it (unless it is stopped manually by you or the user). Since your page doesn't use HTTPS, the permission you gave Chrome to use speech recognition on that page doesn't persist, and it asks the user for permission again and again. This is why it is recommended to use HTTPS whenever using speech recognition on the page.

This was a funny one.

So I unminified annyang.js to find the onend and onerror functions. In the onerror call I console logged the error to get:

Note error: "no-speech".

You see where this is going...

Looking up the W3 spec: https://dvcs.w3.org/hg/speech-api/raw-file/tip/speechapi.html#dfn-sre.nospeech

It reads:

"no-speech" No speech was detected.

Long story short, you're being too quiet.

As for PREVENTING it - dig into annyang.js, particularly the onerror event. Or sing the ABCs repeatedly while you need it on.

Brock Adams

You are probably getting the extra prompts due to AJAX loading in iframes for various purposes. Wrap your code in a frame check:

if (window.top == window.self) {
    //-- Only run on the master (top) page...
    (function() {
        // https://github.com/TalAter/annyang
        // I couldn't figure out how to load it dynamically, so I just copypasted
        // the minified version.
        // @require works in Firefox, but not Chrome, and this is way easier than
        // any alternative I found.
        (function(a){"use strict";var b=this,c=b.SpeechRecognition||b.webkitSpeechRecognition|| ...

    // etc...
}


To eliminate the prompt completely, you have to use a full-blown extension. Then you can use techniques like in this other answer. (But then you also have to hassle with the Chrome store if you want to share the extension easily.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!