-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
79 lines (69 loc) · 2.14 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const button = document.getElementById('button');
const audioElement = document.getElementById('audio');
const programmingButton = document.getElementById('programming');
const darkButton = document.getElementById('dark');
const nsfwButton = document.getElementById('nsfw');
let currentAPIUrl =
'https://sv443.net/jokeapi/v2/joke/Programming,Dark?blacklistFlags=nsfw,religious,political,racist,sexist';
// Disable / Enable button
function toggleButton() {
button.disabled = !button.disabled;
}
// Pass the joke to the voiceRSS API
function tellMe(joke) {
VoiceRSS.speech({
key: config.MY_KEY,
src: joke,
hl: 'en-us',
r: 0,
c: 'mp3',
f: '44khz_16bit_stereo',
ssml: false,
});
}
// Get Jokes from Joke API
async function getJokes() {
let joke = '';
try {
const response = await fetch(currentAPIUrl);
const data = await response.json();
if (data.setup) {
joke = `${data.setup} ... ${data.delivery}`;
} else {
joke = data.joke;
}
// Tell the joke
tellMe(joke);
// Disable the button
toggleButton();
} catch (error) {
console.log('An error occured in getJokes(). ', error);
}
}
// Event Listeners
button.addEventListener('click', () => {
getJokes();
});
audioElement.addEventListener('ended', toggleButton);
// These are to let the user switch between different joke types.
programmingButton.addEventListener('click', () => {
darkButton.disabled = false;
nsfwButton.disabled = false;
programmingButton.disabled = true;
currentAPIUrl =
'https://sv443.net/jokeapi/v2/joke/Programming,Dark?blacklistFlags=nsfw,religious,political,racist,sexist';
});
darkButton.addEventListener('click', () => {
darkButton.disabled = true;
nsfwButton.disabled = false;
programmingButton.disabled = false;
currentAPIUrl =
'https://sv443.net/jokeapi/v2/joke/Dark?blacklistFlags=nsfw,religious,political,racist,sexist';
});
nsfwButton.addEventListener('click', () => {
darkButton.disabled = false;
nsfwButton.disabled = true;
programmingButton.disabled = false;
currentAPIUrl =
'https://sv443.net/jokeapi/v2/joke/Any?blacklistFlags=religious,political,racist,sexist';
});