Skip to content

Instantly share code, notes, and snippets.

@vinaydotblog
Last active February 24, 2018 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinaydotblog/a695f28b74077ad7f4328a941f01731c to your computer and use it in GitHub Desktop.
Save vinaydotblog/a695f28b74077ad7f4328a941f01731c to your computer and use it in GitHub Desktop.
A bookmarklet to input text using speech to document's active element.
(function(Recognition,c){
var recognition = new Recognition;
recognition.lang = 'en-US';
recognition.interimResults = false;
recognition.maxAlternatives = 5;
recognition.start();
c.log('listening now');
function triggerEvent(elem, Event, type) {
var event = new Event(type, {'bubbles': true, 'cancelable': true });
elem.dispatchEvent(event);
}
recognition.onresult = function(event) {
(function(activeElement, event){
var text = event.results[0][0].transcript;
c.log('saying: ', text);
if( activeElement && activeElement.tagName === 'INPUT' ) {
activeElement.value = text;
triggerEvent(activeElement, KeyboardEvent, 'keydown');
triggerEvent(activeElement, KeyboardEvent, 'keypress');
triggerEvent(activeElement, InputEvent, 'input');
triggerEvent(activeElement, KeyboardEvent, 'keyup');
}
})(document.activeElement, event);
};
recognition.onend = function(){
console.log('stopped listening');
}
})(window.SpeechRecognition || window.webkitSpeechRecognition, console);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment