-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
63 lines (48 loc) · 1.53 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
55
56
57
58
59
60
61
62
63
const minute = 1000 * 60;
const hour = minute * 60;
const day = hour * 24;
const year = day * 365;
const d = new Date();
var time = d.toLocaleTimeString();
var date = d.toLocaleDateString();
document.getElementById("time").innerHTML = time;
document.getElementById("date").innerHTML = date;
// Fetch quote from API
async function fetchQuote() {
const url = 'https://type.fit/api/quotes';
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error('Request not found');
}
const quotes = await response.json();
return quotes;
} catch (e) {
alert(e);
}
}
function tweetQuote() {
const twitterQuoteUrl = `https://twitter.com/intent/tweet?text=${quoteEl.textContent} - ${quoteAuthorEl.textContent}`;
window.open(twitterQuoteUrl, '_blank');
}
const quotesPromise = fetchQuote();
const quoteEl = document.getElementById('quote');
const quoteAuthorEl = document.getElementById('author');
document.getElementById('new-quote').addEventListener('click', function (e) {
quotesPromise.then(quotes => {
const randomQuoteIndex = Math.floor(Math.random() * quotes.length);
const quoteText = quotes[randomQuoteIndex].text;
let author = quotes[randomQuoteIndex].author;
if (!author) {
author = 'Unknown';
}
if (quoteText.length > 120) {
quoteEl.classList.add('long-quote');
} else {
quoteEl.classList.remove('long-quote');
}
quoteEl.textContent = quoteText;
quoteAuthorEl.textContent = author;
});
});
document.getElementById('twitter').addEventListener('click', tweetQuote);