-
Notifications
You must be signed in to change notification settings - Fork 5
/
script.js
51 lines (42 loc) · 1.31 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
const apiUrl = "https://api.quotable.io/random";
const quote = document.getElementById("quote");
const author = document.getElementById("author");
const twitterBtn = document.getElementById("twitter");
const newQuoteBtn = document.getElementById("new-quote");
const quoteContainer = document.getElementById("quote-container");
const loader = document.getElementById("loader");
function loadingInProgress() {
quoteContainer.hidden = true;
loader.hidden = false;
}
function loadingFinished() {
quoteContainer.hidden = false;
loader.hidden = true;
}
async function newQuote() {
loadingInProgress();
let q = await fetch(apiUrl)
.then(response => response.json())
.then(data => data)
.catch(error => {
newQuote();
alert(error);
});
if (q.content.length > 100) {
quote.classList.add('long-quote');
} else {
quote.classList.remove('long-quote');
}
quote.textContent = q.content;
author.textContent = q.author;
loadingFinished();
};
newQuoteBtn.addEventListener('click', () => {
newQuote();
});
twitterBtn.addEventListener('click', () => {
const twitterUrl = `https://twitter.com/intent/tweet?text=${quote.textContent} - ${author.textContent}`;
window.open(twitterUrl, '_blank');
});
// onload
newQuote();