-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainScript.js
49 lines (44 loc) · 1.25 KB
/
mainScript.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
const button = document.getElementById('button');
const audioElement = document.getElementById('audio');
//Disable/enable button
function toggleButton () {
button.disabled = !button.disabled;
}
//passing joke to voicerss API
function tellMe(joke){
VoiceRSS.speech({
key: 'a23efc9a46be49a99a28f6001faae549',
src: joke,
hl: 'en-us',
v: 'Linda',
r: 0,
c: 'mp3',
f: '44khz_16bit_stereo',
ssml: false
});
}
//Get jokes from API
async function getJokes(){
let joke = '';
const apiURL = 'https://sv443.net/jokeapi/v2/joke/Programming?blacklistFlags=nsfw,religious,political,racist,sexist'
try {
const response = await fetch(apiURL);
const data = await response.json();
//checks if one or two part and formats joke
if (data.setup){
joke = `${data.setup}...${data.delivery}`;
}else {
joke = `${data.joke}`;
}
//Text-to-Speech
tellMe(joke);
//Disable Button
toggleButton();
} catch (error) {
//catch error
console.log("Whoops, error: ", error);
}
}
//Event listeners
button.addEventListener('click',getJokes);
audioElement.addEventListener('ended',toggleButton);