Created
March 30, 2022 10:18
-
-
Save larscwallin/b11c10b203301a38b8e3a05d6a5f9cd0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition | |
var SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList | |
var SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEvent | |
var actions = [ 'next' , 'previous' , 'toc']; | |
var grammar = '#JSGF V1.0; grammar actions; public <action> = ' + actions.join(' | ') + ' ;' | |
var recognition = new SpeechRecognition(); | |
var speechRecognitionList = new SpeechGrammarList(); | |
speechRecognitionList.addFromString(grammar, 1); | |
recognition.grammars = speechRecognitionList; | |
recognition.continuous = false; | |
recognition.lang = 'en-GB'; | |
recognition.interimResults = false; | |
recognition.maxAlternatives = 1; | |
recognition.onresult = function(event) { | |
var action = event.results[0][0].transcript; | |
console.log('Result received: ' + action + '.'); | |
switch(action) { | |
case 'next': | |
VanillaReaderApi.next(); | |
break; | |
case 'previous': | |
VanillaReaderApi.previous(); | |
break; | |
case 'toc': | |
VanillaReaderApi.openTocDialog(); | |
break; | |
} | |
} | |
recognition.onspeechend = function() { | |
recognition.stop(); | |
} | |
recognition.onnomatch = function(event) { | |
console.log('I didnt recognise that action.'); | |
} | |
recognition.onerror = function(event) { | |
console.log('Error occurred in recognition: ' + event.error); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment