i’m working as a software developer trainee in an organisation and
i’m trying to achieve a webspeechApi code which can access browser..
suppose if i say ‘open facebook’ the browser should open facebook.
i’ve tried using annyang but i dont wanna add any external libraries because of
security reasons
<script>
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 commands = {
'open facebook' : open,
}
function openfacebook(){
alert('opening facebook')
console.log('open facebook');
location.href = 'https://www.facebook.com/';
}
};
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();
});
</script>