-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
54 lines (49 loc) · 1.43 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
// Selecting Content
const newQuoteButton = document.querySelector("#new-quote");
const twitterButton = document.querySelector(".twitter-button");
const quotes = document.querySelector("#quote");
const author = document.querySelector("#author");
const loader = document.querySelector(".loader");
const quoteContainer = document.querySelector("#quote-container");
// Function to get the quote
async function getQuotes() {
loading();
const apiUrl = "https://programming-quotesapi.vercel.app/api/random";
try {
const response = await fetch(apiUrl);
const data = await response.json();
complete();
const quote = data;
quotes.textContent = quote.quote;
author.textContent = quote.author;
if (!quote.author) {
author.textContent = "Unknown";
} else {
author.textContent = quote.author;
}
} catch (error) {
console.error(error);
}
}
getQuotes();
// Function to Tweet the quote
function tweetQuote() {
const twitterUrl = `https://twitter.com/intent/tweet?text =${quotes.textContent} - ${author.textContent} `;
window.open(twitterUrl, "_blank");
}
// Function for loader
function loading() {
loader.hidden = false;
quoteContainer.hidden = true;
}
// Function when the content is ready
function complete() {
loader.hidden = true;
quoteContainer.hidden = false;
}
newQuoteButton.addEventListener("click", () => {
getQuotes();
});
twitterButton.addEventListener("click", () => {
tweetQuote();
});