0

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 don't 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>
Leonardo
  • 2,439
  • 33
  • 17
  • 31
milind
  • 1
  • 1

0 Answers0