如何在您的网站上通过语音识别搜索Google?

时间:2019-04-03 00:19:11

标签: javascript html speech-recognition google-speech-api

我在网络浏览器中有一个“ Jarvis”或个人助理。它在某些语音识别代码上运行。我将放在底部。我想告诉它谷歌的东西,然后说我想要它的谷歌,然后让它自动为我谷歌。

你能帮忙吗?  只是为了让您知道语音识别效果很好,但是说明中并未显示激活语音识别的按钮。

<script>

        var x = document.getElementById("reconition");
        var message = document.querySelector('#message');
        var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
        var SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList;
        var grammar = '#JSGF V1.0;'
        var recognition = new SpeechRecognition();
        var speechRecognitionList = new SpeechGrammarList();
                speechRecognitionList.addFromString(grammar, 1);
                recognition.grammars = speechRecognitionList;
                recognition.lang = 'en-US';
                recognition.interimResults = false;
                recognition.onresult = function(event) {
                        var last = event.results.length - 1;
                        var command = event.results[last][0].transcript;
                        message.textContent = 'Voice Input: ' + command + '.';



                        if(command.toLowerCase() === 'google something'){
                                var msg = new SpeechSynthesisUtterance('what would you like for me to google');
                window.speechSynthesis.speak(msg);
                        }
  }




                    };
            recognition.onspeechend = function() {
                recognition.stop();
            };
            recognition.onerror = function(event) {
                message.textContent = 'Error occurred in recognition: ' + event.error;
            }
            document.querySelector('#btnGiveCommand').addEventListener('click', function(){
                recognition.start();
            });

2 个答案:

答案 0 :(得分:1)

好吧,由于您之后没有指定要对结果做任何事情,因此可以打开一个弹出窗口/使用iframe,然后使用搜索查询打开一个Google网址。例如:

https://www.google.com/search?q=this+is+a+test

将使用查询“这是一个测试”打开Goog​​le

q =参数实际上负责查询。

现在,如果您想以某种方式使用页面上的结果,则可以选择使用Google的自定义搜索API:https://developers.google.com/custom-search/v1/overview

答案 1 :(得分:0)

如果有人想要这个答案,我前一阵子已经弄清楚了,但是忘了发布了。

else if (command.includes("Google")){
    var search = command.substring(command.indexOf("+google")+7);
    window.open('https://www.google.com/search?q=' + search, "mywindow", "width=420, height=420, top=0,");
}

这会打开一个新窗口,其中会用google搜索您在google一词后所说的内容。