Created
September 4, 2022 12:52
-
-
Save wibawasuyadnya/43606cc23c1fa2026a3db74039bb20ac to your computer and use it in GitHub Desktop.
Text Speech with JavaScript
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
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