-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
42 lines (38 loc) · 1014 Bytes
/
script.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
const button = document.getElementById('button');
const audioElement = document.getElementById('audio');
// Passin Jokes to VoiceRSS
const tellMe = joke => {
console.log(`Tell me: ${joke}`);
VoiceRSS.speech({
key: 'b8bbdfdca46e484a93af4cdbe1251d47',
src: joke,
hl: 'en-us',
v: 'Linda',
r: 0,
c: 'mp3',
f: '44khz_16bit_stereo',
ssml: false
});
};
const toggleButton = () => {
button.disabled = !button.disabled;
};
// Get Jokes From Jokes API
const getJokes = async () => {
const apiUrl = "https://v2.jokeapi.dev/joke/Programming";
let joke;
try{
const response = await fetch(apiUrl);
const data = await response.json();
joke = data.joke ? data.joke : data.setup + "\n" + data.delivery;
// Text-to-Speech
tellMe(joke);
// Disable Button
toggleButton();
} catch (error) {
console.log("Fetch failed", error);
}
};
// Event Listeners
button.addEventListener("click", getJokes);
audioElement.addEventListener('ended', toggleButton);