Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition
- var SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList
- var recognition = new SpeechRecognition();
- var speechRecognitionList = new SpeechGrammarList();
- speechRecognitionList.addFromString('#JSGF V1.0;', 1);
- recognition.grammars = speechRecognitionList;
- recognition.continuous = false;
- recognition.lang = 'ru-RU';
- recognition.interimResults = false;
- recognition.maxAlternatives = 1;
- function log(msg) {
- var time = new Date();
- time = ("0" + time.getHours()).slice(-2) + ":" +
- ("0" + time.getMinutes()).slice(-2) + ":" +
- ("0" + time.getSeconds()).slice(-2);
- chrome.tabs.executeScript({
- code: `console.log("[${time}] ${msg}");`
- });
- }
- recognition.onresult = function(event) {
- // The SpeechRecognitionEvent results property returns a SpeechRecognitionResultList object
- // The SpeechRecognitionResultList object contains SpeechRecognitionResult objects.
- // It has a getter so it can be accessed like an array
- // The first [0] returns the SpeechRecognitionResult at the last position.
- // Each SpeechRecognitionResult object contains SpeechRecognitionAlternative objects that contain individual results.
- // These also have getters so they can be accessed like arrays.
- // The second [0] returns the SpeechRecognitionAlternative at position 0.
- // We then return the transcript property of the SpeechRecognitionAlternative object
- var transcript = event.results[0][0].transcript;
- var confidence = event.results[0][0].confidence;
- log(`Пользователь сказал: '${transcript}', точность определения: ${confidence}`);
- }
- recognition.onspeechend = function() {
- log('Запись команды остановлена');
- recognition.stop();
- }
- recognition.onerror = function(event) {
- log(JSON.stringify(event).replace(/\"/g, "'"));
- log('Произошла ошибка во время записи команды');
- }