-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
42 lines (37 loc) · 1.38 KB
/
Copy pathscript.js
File metadata and controls
42 lines (37 loc) · 1.38 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
const generateQuoteBtn = document.getElementById("generate-btn");
const quoteText = document.getElementById("quote");
const quoteAuthor = document.getElementById("author");
const copyQuoteBtn = document.getElementById("copy-btn");
const newQuoteBtn = document.getElementById("new-quote-btn");
copyQuoteBtn.style.display = "none";
quoteText.textContent = "Click 'Generate Quote' to begin!";
async function fetchQuote() {
quoteText.textContent = "Loading...";
quoteAuthor.textContent = "";
copyQuoteBtn.style.display = "none";
try {
const response = await fetch('https://dummyjson.com/quotes/random');
const data = await response.json();
quote.textContent = `"${data.quote}"`;
quoteAuthor.textContent = `- ${data.author}`;
copyQuoteBtn.style.display = "inline-block";
} catch (error) {
quoteText.textContent = "Failed to fetch quote. Please try again.";
quoteAuthor.textContent = "";
copyQuoteBtn.style.display = "none";
}
}
async function copyQuote() {
const quote = quoteText.textContent;
try {
await navigator.clipboard.writeText(quote);
copyQuoteBtn.textContent = "Copied!";
setTimeout(() => {
copyQuoteBtn.textContent = "Copy Quote";
}, 2000);
} catch (error) {
alert("Failed to copy quote. Please try again.");
}
}
generateQuoteBtn.addEventListener("click", fetchQuote);
copyQuoteBtn.addEventListener("click", copyQuote);