-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (81 loc) · 2.64 KB
/
index.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Init Speech Synth
const message = new SpeechSynthesisUtterance()
let voices = [];
// Get available voices
speechSynthesis.addEventListener("voiceschanged", () => {
voices = speechSynthesis.getVoices()
loadVoices(voices);
})
// DOM elements
const quotesElement = document.querySelector(".phrases");
const selectElement = document.querySelector(".select");
const form = document.querySelector(".form")
const textareaElement = document.querySelector(".textarea");
//Load select with voices
function loadVoices(voices){
voices.forEach(voice => {
const { name, lang } = voice
const optionElement = document.createElement("option");
optionElement.innerText = `${ name } - ${ lang }`
optionElement.value = name
selectElement.appendChild(optionElement)
})
}
//Listen for change events on select
selectElement.addEventListener("change", (event) =>{
const name = event.target.value;
const voice = voices.find(voice => voice.name === name)
message.voice = voice;
})
// Listen for form submission
form.addEventListener("submit", (event) => {
event.preventDefault()
const customText = textareaElement.value
message.text = customText;
speechSynthesis.speak(message)
})
// App data
const phrases = [
{
text: "Te conviertes en lo que le das a tu atención»",
author: "Epictecto",
},
{
text: "El único modo de hacer un gran trabajo es amar lo que haces ",
author: "Steve Jobs",
},
{
text: "«Es esencial que recuerdes que la atención que le des a cualquier acción debe ser proporcional a su valor.»",
author: "Marco Aurelio",
},
{
text: "El dinero no es la clave del éxito; la libertad para poder crear lo es ",
author: "Nelson Mandela",
},
{
text: "Cuanto más duramente trabajo, más suerte tengo",
author: "Gary Player",
},
{
text: "El trabajo duro hace que desaparezcan las arrugas de la mente y el espíritu",
author: "Helena Rubinstein ",
},
];
// Load quotes in the DOM
phrases.forEach(phrase => {
const { text, author } = phrase
const quoteTemplate = `
<section class="phrase">
<h2 class="phrase-text">${ text }</h2>
<h5 class="phrase-author">${ author }</h5>
</section> `
quotesElement.innerHTML += quoteTemplate
})
//Listen for clicks on quotes
const quotesCollection = document.querySelectorAll(".phrase");
quotesCollection.forEach(quoteElement => {
quoteElement.addEventListener("click", (event) => {
message.text = event.target.innerText
speechSynthesis.speak(message)
});
});