-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathscript.js
More file actions
49 lines (45 loc) · 1.23 KB
/
script.js
File metadata and controls
49 lines (45 loc) · 1.23 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
37
38
39
40
41
42
43
44
45
46
47
48
49
const button = document.getElementById('button');
const audioElement = document.getElementById('audio');
// Passing Joke to VoiceRSS API
function tellMe(joke) {
VoiceRSS.speech({
// Don't write out API Keys like this, but an exception made here because it's free
key: 'e985f868e96c46d9b0789c3855350152',
src: joke,
hl: 'en-us',
v: 'Linda',
r: 0,
c: 'mp3',
f: '44khz_16bit_stereo',
ssml: false
});
}
// Disable/Enable Button
function toggleButton() {
button.disabled = !button.disabled;
}
// Get Jokes from Joke API
async function getJokes() {
let joke = '';
const apiUrl = 'https://v2.jokeapi.dev/joke/Programming?blacklistFlags=nsfw,religious,political,racist,sexist,explicit';
try {
const response = await fetch(apiUrl);
const data = await response.json();
if (data.setup) {
// If joke's type is "twopart"
joke = `${data.setup} ... ${data.delivery}`;
} else {
// If joke's type is "single"
joke = data.joke;
}
// Text-to-Speech
tellMe(joke);
// Disable Button
toggleButton();
} catch (error) {
// Catch Errors Here
}
}
// Event Listeners
button.addEventListener('click', getJokes);
audioElement.addEventListener('ended', toggleButton);