-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
61 lines (56 loc) · 1.58 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
const button = document.getElementById('button');
const audioElement = document.getElementById('audio');
const textElement = document.getElementById('text');
// Disable/Enable Button
function toggleButton() {
button.disabled = !button.disabled;
if(!button.disabled) {
textElement.innerText = '';
textElement.style.padding = '';
textElement.style.background = '';
} else {
textElement.style.background = 'black';
textElement.style.padding = '10px 10px';
}
};
// Putting text
function textJoke(joke) {
textElement.innerText = joke;
};
// Passsing Joke to VoiceRSS API
function tellMe(joke) {
VoiceRSS.speech({
key: '9b06d9a7e80f4778b0fdd09b855a0288',
src: joke,
hl: 'en-us',
v: 'Linda',
r: 0,
c: 'mp3',
f: '44khz_16bit_stereo',
ssml: false
});
}
// Get Jokes from Joke API
let joke = '';
async function getJokes() {
const apiUrl = 'https://v2.jokeapi.dev/joke/Programming?blacklistFlags=nsfw,religious,political,racist,sexist';
try {
const response = await fetch(apiUrl);
const data = await response.json();
if (data.setup) {
joke = `${data.setup} ... ${data.delivery}`;
} else {
joke = data.joke;
}
// Text-to-Speech
tellMe(joke);
// Disable Button
toggleButton();
textJoke(joke);
} catch (error) {
console.log('Whoops', error);
}
}
// Event Listeners
button.addEventListener('click', getJokes);
audioElement.addEventListener('ended', toggleButton);