-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
73 lines (65 loc) · 2.06 KB
/
main.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
const generate = document.getElementById("generate");
const joke = document.getElementById("joke");
const reset = document.getElementById("reset");
const delivery = document.getElementById("delivery");
const copyBtn = document.querySelector("#copy_btn");
/* API Base URL*/
const baseURL = "https://sv443.net/jokeapi/v2/joke/Any?";
/* Color Array: to change the backgroundColor of the JOKE*/
const color = [
"lightgreen",
"lightblue",
"gray",
"lightgrey",
"skyblue",
"wheat",
"white",
"lightcyan",
"antiquewhite",
"azure",
"lightyellow",
"mintcream",
"springgreen",
"snow",
"seashell",
];
/* Generate Buttton click event */
generate.addEventListener("click", () => {
/* Setting Joke Color to Black*/
joke.style.color = "black";
fetch(baseURL)
.then((res) => res.json())
.then((randomJoke) => {
console.log(randomJoke);
if (randomJoke.type === "single") {
// If type == "single", the joke only has the "joke" property
joke.innerHTML = randomJoke.joke;
delivery.innerHTML = "";
delivery.style.display = "none";
} else {
// If type == "single", the joke only has the "joke" property
joke.innerHTML = randomJoke.setup;
delivery.style.display = "block";
delivery.innerHTML = randomJoke.delivery;
delivery.style.backgroundColor = "yellow";
}
console.log(copyBtn);
copyBtn.style.display = "flex";
joke.style.backgroundColor =
color[Math.floor(Math.random() * color.length)];
});
});
/* Resetting the Page onClick */
reset.addEventListener("click", () => {
joke.innerHTML = "Click Generate Button";
joke.style.backgroundColor = "black";
joke.style.color = "white";
delivery.style.backgroundColor = "black";
copyBtn.style.display = "none";
elivery.style.display = "none";
});
copyBtn.onclick = () => {
var exportJoke = joke.textContent + "\n\n" + delivery.textContent;
// Copy the text inside the text field
navigator.clipboard.writeText(exportJoke);
};