Created
February 1, 2020 04:00
-
-
Save fernanDOTdo/e5a08af6f1a1ac26cfee3961cffd2bc2 to your computer and use it in GitHub Desktop.
Web Speech API - Falando o Elemento Clicado
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
| // β¨οΈ π | |
| // Carrega todas as vozes (ΓΊtil para colocar em PT-BR) | |
| var vozes; | |
| window.speechSynthesis.onvoiceschanged = function() { | |
| vozes = window.speechSynthesis.getVoices(); | |
| }; | |
| // π Observa todos os elementos clicados no Body | |
| document.querySelector("body").addEventListener("click", e => { | |
| // π Evita Redirecionamento | |
| e.preventDefault(); | |
| // π£ Pega a API do web speech | |
| const api = window.speechSynthesis; | |
| // π Pega elemento clicado | |
| const target = e.target; | |
| // π Prepara a fala | |
| const fala = new SpeechSynthesisUtterance(target.innerText); | |
| // π§π· Fala em Portugues | |
| fala.voice = vozes.filter(v => v.lang == "pt-BR")[0]; | |
| // π€ Solta a voz | |
| api.speak(fala); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment