-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
60 lines (49 loc) · 1.52 KB
/
script.js
File metadata and controls
60 lines (49 loc) · 1.52 KB
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
const quoteContainer = document.getElementById('quote-container')
const loader = document.getElementById('loader')
function loading()
{
loader.hidden = false;
quoteContainer.hidden = true;
}
function unloading()
{
loader.hidden = true;
quoteContainer.hidden = false;
}
function newQuote(arr)
{
loading()
const quote= arr[Math.floor(Math.random()*arr.length)]
console.log(quote)
const authorText = document.getElementById('author')
!quote.author ? authorText.textContent = 'Unknown' : authorText.textContent = quote.author;
const quoteText = document.getElementById('quote')
quote.text.length > 100 ? quoteText.classList.add('quote-long') : quoteText.classList.remove('quote-long')
quoteText.textContent = quote.text
unloading()
const tweetBtn = document.getElementById('twitter')
tweetBtn.addEventListener('click', ()=>{tweetQuote(quote.text, quote.author)})
}
function tweetQuote(text,author)
{
const twitterUrl = `https://twitter.com/intent/tweet?text=${text} - ${author}`;
window.open(twitterUrl,'_blank')
}
async function getQuote()
{
loading()
let quoteBank=[];
try{
const response =await fetch('https://type.fit/api/quotes');
quoteBank = await response.json()
// console.log(quoteBank[21])
newQuote(quoteBank)
}
catch(err)
{
console.log(err)
}
const quoteBtn = document.getElementById('new-quote')
quoteBtn.addEventListener('click', ()=>{newQuote(quoteBank)});
}
getQuote()