forked from ruchikakengal/WebDevIn100_Days
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtts.js
More file actions
36 lines (33 loc) · 1.25 KB
/
tts.js
File metadata and controls
36 lines (33 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Check if the browser supports the Web Speech API for compatibility.
if ('speechSynthesis' in window) {
// --- Initialize References ---
const synth = window.speechSynthesis;
const textInput = document.getElementById('textToSpeak');
const speakButton = document.getElementById('speakButton');
const cancelButton = document.getElementById('cancelButton');
// --- Speak Button Functionality ---
speakButton.addEventListener('click', () => {
const text = textInput.value;
if (text.trim() !== '') {
if (synth.speaking) {
synth.cancel();
}
const utterance = new SpeechSynthesisUtterance(text);
synth.speak(utterance);
} else {
alert('Please enter some text first.');
}
});
// --- Redirect to Home Button Functionality ---
cancelButton.addEventListener('click', () => {
// Stop any speech that is currently in progress.
if (synth.speaking) {
synth.cancel();
}
// Redirect the user to the home page.
window.location.href = 'index.html';
});
} else {
// If the browser doesn't support the API, alert the user.
alert("Sorry, your browser does not support Text-to-Speech.");
}