-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
60 lines (50 loc) · 1.32 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const button = document.getElementById('button');
const audioElement = document.getElementById('audio');
const bubble = document.getElementById('speech');
// Enable/Disable Button
function toggleButton() {
button.disabled = !button.disabled;
}
// Render joke in speech bubble
function renderJoke(joke) {
bubble.textContent = joke;
}
// Get Jokes from Joke API
async function getJokes() {
let joke = '';
const apiUrl = 'https://sv443.net/jokeapi/v2/joke/Programming';
try {
const response = await fetch(apiUrl);
const data = await response.json();
//Check if the joke is a single or two parts joke
if (data.setup) {
joke = `${data.setup} ... ${data.delivery}`;
} else {
joke = `${data.joke}`;
}
// Display text
renderJoke(joke);
// Text-To-Speech API
tellMeAJoke(joke);
// Disable Button While Audio Playing
toggleButton();
} catch (error) {
console.log('Ooops, there was an error: ' + error);
}
}
// Passing Joke to VoiceRSS API
function tellMeAJoke(joke) {
VoiceRSS.speech({
key: '5ff4b1b6ab8143f488a977bc3a2f2a9c',
src: joke,
hl: 'en-us',
v: 'Linda',
r: 0,
c: 'mp3',
f: '44khz_16bit_stereo',
ssml: false,
});
}
// Event Listeners
button.addEventListener('click', getJokes);
audioElement.addEventListener('ended', toggleButton);