-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
86 lines (75 loc) · 2.48 KB
/
Copy pathapp.js
File metadata and controls
86 lines (75 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
let currentAdvice = "";
let currentAuthor = "";
let favorites = JSON.parse(localStorage.getItem("favorites")) || [];
function getAdvice() {
const randomno = Math.floor(Math.random() * 15);
fetch("https://type.fit/api/quotes")
.then((response) => response.json())
.then((data) => {
currentAdvice = data[randomno].text;
currentAuthor = data[randomno].author.replace(", type.fit", "");
document.getElementById("advice-text").textContent = currentAdvice;
document.getElementById(
"advice-author",
).textContent = `- ${currentAuthor}`;
})
.catch((error) => console.error("Error:", error));
}
function favoriteAdvice() {
if (currentAdvice && !favorites.includes(currentAdvice)) {
favorites.push(currentAdvice);
localStorage.setItem("favorites", JSON.stringify(favorites));
alert("Advice added to favorites!");
}
}
function copyAdvice() {
if (currentAdvice) {
navigator.clipboard
.writeText(`${currentAdvice} - ${currentAuthor}`)
.then(() => {
alert("Advice copied to clipboard!");
})
.catch((error) => console.error("Error copying text:", error));
}
}
function shareAdvice() {
if (currentAdvice) {
const shareText = `${currentAdvice} - ${currentAuthor}`;
if (navigator.share) {
navigator
.share({
title: "Wisdom Whisper",
text: shareText,
url: window.location.href,
})
.catch((error) => console.error("Error sharing:", error));
} else {
alert("Share API not supported in this browser.");
}
}
}
function filterAdvice(category) {
fetch(`https://api.quotable.io/quotes?tags=${category}`)
.then((response) => response.json())
.then((data) => {
const randomIndex = Math.floor(Math.random() * data.results.length);
currentAdvice = data.results[randomIndex].content;
currentAuthor = data.results[randomIndex].author;
document.getElementById("advice-text").textContent = currentAdvice;
document.getElementById(
"advice-author",
).textContent = `- ${currentAuthor}`;
})
.catch((error) => console.error("Error:", error));
}
document.addEventListener("DOMContentLoaded", () => {
const mobileMenuButton = document.getElementById("mobile-menu-button");
const menu = document.getElementById("menu");
mobileMenuButton.addEventListener("click", () => {
menu.classList.toggle("hidden");
});
const currentTheme = localStorage.getItem("theme");
if (currentTheme === "dark") {
document.documentElement.classList.add("dark");
}
});