-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
78 lines (70 loc) · 2.37 KB
/
index.js
File metadata and controls
78 lines (70 loc) · 2.37 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Adding all the elements
let quote = document.querySelector(".quote");
let author = document.querySelector(".author");
let newQuoteBtn = document.getElementById("new-quote");
let copyBtn = document.getElementById("copy");
let tweetBtn = document.getElementById("tweet");
let saveBtn = document.getElementById("save-image");
//function to fetch data from api
async function fetchData() {
const url = "https://api.freeapi.app/api/v1/public/quotes/quote/random";
const options = { method: "GET", headers: { accept: "application/json" } };
try {
const response = await fetch(url, options);
const data = await response.json();
// console.log(data);
return data;
} catch (error) {
console.error(error);
}
}
//saving the api respone in a function
async function displayQuote() {
let apiResponse = await fetchData();
quote.innerText = `"${apiResponse.data.content}"`;
author.innerText = `- ${apiResponse.data.author}`;
}
displayQuote(); //running the function for first time
//EventListners for NewQuote
newQuoteBtn.addEventListener("click", () => {
displayQuote();
});
//EventListners for CopyQuote
copyBtn.addEventListener("click", function () {
navigator.clipboard
.writeText(quote.innerText + " " + author.innerText)
.then(() => {
this.innerText = "Copied!";
setTimeout(() => {
this.innerText = "Copy to Clipboard";
}, 1000);
})
.catch((error) => {
console.error(error);
});
});
//EventListner for Tweet
tweetBtn.addEventListener("click", function () {
let tweetContent = `${quote.innerText} ${author.innerText}`;
let finalContent = encodeURIComponent(tweetContent);
let tweetUrl = `https://x.com/intent/post?text=${finalContent}`;
window.open(tweetUrl, "_blank");
this.innerText = "Tweeted!";
setTimeout(() => {
this.innerText = "Share on Twitter";
}, 1000);
});
//EventListner for SaveImage
saveBtn.addEventListener("click", function () {
// Use html2canvas to take a screenshot of the quote container (its a third party cdn js)
// URL : https://html2canvas.hertzen.com/
html2canvas(document.querySelector(".quote-container")).then((canvas) => {
let image = canvas.toDataURL("image/png");
let link = document.createElement("a");
link.href = image;
link.download = "quote.png";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
});
});