const synth = window.speechSynthesis;
const inputForm = document.querySelector("form");
const inputTxt = document.querySelector("input");
const voiceSelect = document.querySelector("select");
let voices;
function loadVoices() {
voices = synth.getVoices();
for (const [i, voice] of voices.entries()) {
const option = document.createElement("option");
option.textContent = `${voice.name} (${voice.lang})`;
option.value = i;
voiceSelect.appendChild(option);
}
}
// in Google Chrome the voices are not ready on page load
if ("onvoiceschanged" in synth) {
synth.onvoiceschanged = loadVoices;
} else {
loadVoices();
}
inputForm.onsubmit = (event) => {
event.preventDefault();
const utterThis = new SpeechSynthesisUtterance(inputTxt.value);
utterThis.voice = voices[voiceSelect.value];
synth.speak(utterThis);
inputTxt.blur();
};SpeechSynthesisUtterance / notebook
SpeechSynthesisUtterance
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.