Skip to content

Instantly share code, notes, and snippets.

@wibawasuyadnya
Created September 4, 2022 12:52
Show Gist options
  • Save wibawasuyadnya/43606cc23c1fa2026a3db74039bb20ac to your computer and use it in GitHub Desktop.
Save wibawasuyadnya/43606cc23c1fa2026a3db74039bb20ac to your computer and use it in GitHub Desktop.
Text Speech with JavaScript
let msg = new SpeechSynthesisUtterance();
let voices = speechSynthesis.getVoices();
msg.voice = voices[0];
let tags = document.querySelectorAll('p,a,h1,h2,h3'); // add more tags for you project
tags.forEach((tag) => {
tag.addEventListener('click', (e) => {
msg.text = e.target.innerText;
tag.style.backgroundColor = "yellow";
speechSynthesis.speak(msg);
let interval = setInterval(() => {
if(!speechSynthesis.speaking){
tag.style.removeProperty('background-color');
clearInterval(interval);
}
}, 100);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment