Last active
February 24, 2018 07:40
-
-
Save vinaydotblog/a695f28b74077ad7f4328a941f01731c to your computer and use it in GitHub Desktop.
A bookmarklet to input text using speech to document's active element.
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
(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