-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
87 lines (71 loc) · 2.48 KB
/
Copy pathscript.js
File metadata and controls
87 lines (71 loc) · 2.48 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
79
80
81
82
83
84
85
86
87
const quoteText = document.getElementById("quote");
const authorText = document.getElementById("author");
const newQuoteBtn = document.getElementById("new-quote");
const copyBtn = document.getElementById("copy-quote");
const tweetBtn = document.getElementById("tweet-quote");
const loader = document.getElementById("loader");
const quoteHistory = [];
// Fetch a new quote from DummyJSON API
async function getQuote() {
showLoading(true);
try {
const response = await fetch("https://dummyjson.com/quotes/random");
const data = await response.json();
const quote = data.quote;
const author = data.author;
// Update DOM
quoteText.innerHTML = `"${quote}"`;
authorText.innerHTML = `— ${author}`;
// Store quote in history
quoteHistory.push({ quote, author });
animateQuote();
} catch (error) {
quoteText.innerHTML = "Failed to load quote.";
authorText.innerHTML = "";
console.error("Error:", error);
} finally {
showLoading(false);
}
}
// Animate quote on change
function animateQuote() {
quoteText.classList.add("fade-in");
authorText.classList.add("fade-in");
setTimeout(() => {
quoteText.classList.remove("fade-in");
authorText.classList.remove("fade-in");
}, 500);
}
// Show/hide loader
function showLoading(show) {
loader.classList.toggle("hidden", !show);
newQuoteBtn.disabled = show;
}
// Copy quote to clipboard
function copyQuote() {
const textToCopy = `${quoteText.innerText} ${authorText.innerText}`;
navigator.clipboard.writeText(textToCopy)
.then(() => alert("✅ Quote copied to clipboard!"))
.catch(err => console.error("❌ Copy failed:", err));
}
// Tweet quote
function tweetQuote() {
const tweetUrl = `https://twitter.com/intent/tweet?text=${encodeURIComponent(quoteText.innerText + " " + authorText.innerText)}`;
window.open(tweetUrl, "_blank");
}
// Event Listeners
newQuoteBtn.addEventListener("click", getQuote);
copyBtn.addEventListener("click", copyQuote);
tweetBtn.addEventListener("click", tweetQuote);
const toggleThemeBtn = document.getElementById("toggle-theme");
function toggleTheme() {
document.body.classList.toggle("dark-mode");
if (document.body.classList.contains("dark-mode")) {
toggleThemeBtn.textContent = "☀️ Light Mode";
} else {
toggleThemeBtn.textContent = "🌙 Dark Mode";
}
}
toggleThemeBtn.addEventListener("click", toggleTheme);
// Initial quote load
getQuote();