javascript - 如何从您的网站使用语音识别搜索谷歌?

标签 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();
            });

最佳答案

好吧,由于您没有指定之后要对结果做任何事情,您可以打开弹出窗口/使用 iframe 并打开带有搜索查询的 google url。例如:

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

将打开 google 查询“这是一个测试”

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

现在,如果您想以某种方式使用页面上的结果,一个选项可能是 googles custome search api:https://developers.google.com/custom-search/v1/overview

关于javascript - 如何从您的网站使用语音识别搜索谷歌?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55485277/

相关文章:

javascript - 如何在主窗口中加载 URL 内容

mfc - 使用谷歌API : Speech To Text on PC Version

python - 带唤醒词的 Google Python 语音识别

android - 安卓语音识别

javascript - 发送数千个获取请求会导致浏览器崩溃。内存不足

javascript - 根据缩放事件更改 amChart Serial 中的 dataProvider

javascript - 如何将文本和 Div 放置在彼此的顶部并将它们居中对齐?

javascript - 使用 Javascript 从外部 CSS 读取顶部和左侧位置值

javascript - 可排序新订单错误获取值 tkEmail

html - 如何使 div 填充视口(viewport)的宽度而不是其父级?