-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
54 lines (43 loc) · 1.31 KB
/
Copy pathscript.js
File metadata and controls
54 lines (43 loc) · 1.31 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
const btn = document.querySelector(".getQuote");
const insertInto = document.querySelector(".quotes");
const twitterBtn = document.getElementById("twitter-btn");
let res, result;
//getting a random quotes from the fetched quotes
const getQuote = () => {
let quote = result[Math.floor(Math.random() * result.length)];
while (quote.text.length > 50) {
quote = result[Math.floor(Math.random() * result.length)];
}
let template = `
<h3 class='quote-text'>${quote.text}</h3>
<p class='author'>By ~ ${quote.author}</p>
`;
insertInto.innerHTML = template;
};
//getting quotes from the api
async function fetchQuotes() {
const api = "https://type.fit/api/quotes";
try {
res = await fetch(api);
result = await res.json();
getQuote();
} catch (err) {
alert(err);
}
}
//tweet button handling
const tweet = () => {
const tweetBody = document.querySelector(".quote-text").innerText;
//twitter web intent url 'https://twitter.com/intent/tweet'
let api = "https://twitter.com/intent/tweet";
window.open(
api + `?text=${tweetBody}&hashtags=motivation,workHard`,
"",
"width=400,height=200,top=center,left=center"
);
};
btn.addEventListener("click", getQuote);
twitterBtn.addEventListener("click", tweet);
window.addEventListener("load", () => {
fetchQuotes();
});