我在网络浏览器中有一个“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/