Skip to content

Instantly share code, notes, and snippets.

@fernanDOTdo
Created February 1, 2020 04:00
Show Gist options
  • Select an option

  • Save fernanDOTdo/e5a08af6f1a1ac26cfee3961cffd2bc2 to your computer and use it in GitHub Desktop.

Select an option

Save fernanDOTdo/e5a08af6f1a1ac26cfee3961cffd2bc2 to your computer and use it in GitHub Desktop.
Web Speech API - Falando o Elemento Clicado
// ⌨️ πŸ’
// 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