Created
July 21, 2018 20:52
-
-
Save brianpeiris/a2046177226c9a31edc401afa6aec3bd to your computer and use it in GitHub Desktop.
Speech recognition for dev tools
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
(() => { | |
const actionMap = { | |
space: () => console.log("---------------------------"), | |
dispatch: () => window.dispatchEvent(new CustomEvent("printstate")) | |
}; | |
const actions = Object.keys(actionMap); | |
let lastActionTime = Date.now(); | |
if (window.recognition) window.recognition.stop(); | |
window.recognition = new webkitSpeechRecognition(); | |
recognition.continuous = true; | |
recognition.interimResults = true; | |
recognition.onerror = console.error; | |
recognition.onresult = event => { | |
const lastResult = event.results[event.results.length - 1]; | |
if (lastResult.isFinal) return; | |
const transcript = lastResult[0].transcript.trim(); | |
const action = actions.find(key => transcript === key); | |
if (action && Date.now() - lastActionTime > 1000) { | |
actionMap[action](); | |
lastActionTime = Date.now(); | |
} | |
}; | |
recognition.start(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment