-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
179 lines (150 loc) · 5.58 KB
/
script.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
let links = JSON.parse(localStorage.getItem("links")) || [];
let favorites = JSON.parse(localStorage.getItem("favorites")) || [];
function renderLinks() {
const educationalContainer = document.getElementById("educational-links");
const professionalContainer = document.getElementById("professional-links");
const favoriteContainer = document.getElementById("favorite-links");
const favheadContainer = document.getElementById("fav-head");
educationalContainer.innerHTML = "";
professionalContainer.innerHTML = "";
favoriteContainer.innerHTML = "";
let hasEducationalLinks = false;
let hasProfessionalLinks = false;
let hasFavoriteLinks = false;
if (links.length === 0) {
educationalContainer.innerHTML =
"<p>No educational links added. Please add some.</p>";
professionalContainer.innerHTML =
"<p>No professional links added. Please add some.</p>";
favoriteContainer.style.display = "none";
favheadContainer.style.display = "none";
return;
}
links.forEach((link) => {
const linkItem = createLinkHTML(link);
if (favorites.some((fav) => fav.url === link.url)) {
favoriteContainer.appendChild(linkItem);
hasFavoriteLinks = true;
} else {
if (link.category === "educational") {
educationalContainer.appendChild(linkItem);
hasEducationalLinks = true;
} else if (link.category === "professional") {
professionalContainer.appendChild(linkItem);
hasProfessionalLinks = true;
}
}
});
if (!hasEducationalLinks) {
educationalContainer.innerHTML =
"<p>No educational links added. Please add some.</p>";
}
if (!hasProfessionalLinks) {
professionalContainer.innerHTML =
"<p>No professional links added. Please add some.</p>";
}
if (!hasFavoriteLinks) {
favoriteContainer.style.display = "none";
favheadContainer.style.display = "none";
} else {
favoriteContainer.style.display = "block";
favheadContainer.style.display = "block";
}
}
function createLinkHTML(link) {
const linkItem = document.createElement("div");
linkItem.classList.add("link-item");
const icon = document.createElement("img");
icon.src = `https://www.google.com/s2/favicons?sz=64&domain_url=${link.url}`;
icon.alt = link.title;
const title = document.createElement("a");
title.href = link.url;
title.textContent = link.title;
title.target = "_blank";
const favoriteButton = document.createElement("button");
const removeLinkButton = document.createElement("button");
favoriteButton.classList.add("favorite-button");
favoriteButton.style.marginLeft = "auto";
removeLinkButton.style.padding = "8px";
removeLinkButton.style.backgroundColor = "transparent";
removeLinkButton.style.border = "none";
removeLinkButton.style.color = "#ff0000";
removeLinkButton.style.fontSize = "18px";
removeLinkButton.style.cursor = "pointer";
removeLinkButton.style.transition = "color 0.3s ease";
removeLinkButton.addEventListener("mouseenter", function () {
removeLinkButton.style.color = "#ff6666";
});
removeLinkButton.addEventListener("mouseleave", function () {
removeLinkButton.style.color = "#ff0000";
});
if (favorites.some((fav) => fav.url === link.url)) {
favoriteButton.style.display = "none";
favoriteButton.classList.add("active");
removeLinkButton.style.marginLeft = "auto";
} else {
favoriteButton.textContent = "Add to Favorites";
removeLinkButton.style.position = "default";
}
favoriteButton.onclick = () => toggleFavorite(link);
removeLinkButton.textContent = "X";
removeLinkButton.onclick = () => removeLink(link);
linkItem.appendChild(icon);
linkItem.appendChild(title);
linkItem.appendChild(favoriteButton);
linkItem.appendChild(removeLinkButton);
return linkItem;
}
function addLink() {
const newLinkInput = document.getElementById("new-link");
const newNameInput = document.getElementById("new-name");
const newCategoryInput = document.getElementById("new-category");
const url = newLinkInput.value.trim();
const name = newNameInput.value.trim();
const category = newCategoryInput.value.trim();
if (url && name && category) {
const newLink = { url, title: name, category };
links.push(newLink);
localStorage.setItem("links", JSON.stringify(links));
newLinkInput.value = "";
newNameInput.value = "";
newCategoryInput.value = "";
renderLinks();
} else {
alert("Please fill all fields: URL, Name, and Category.");
}
}
function toggleFavorite(link) {
if (confirm(`Are you sure you want to add "${link.title}" to favorites?`)) {
const index = favorites.findIndex((fav) => fav.url === link.url);
if (index === -1) {
favorites.push(link);
} else {
favorites.splice(index, 1);
}
localStorage.setItem("favorites", JSON.stringify(favorites));
renderLinks();
}
}
function removeLink(link) {
if (confirm(`Are you sure you want to remove "${link.title}"?`)) {
links = links.filter((item) => item.url !== link.url);
favorites = favorites.filter((item) => item.url !== link.url);
localStorage.setItem("links", JSON.stringify(links));
localStorage.setItem("favorites", JSON.stringify(favorites));
renderLinks();
}
}
document.getElementById("search").addEventListener("input", function () {
const filter = this.value.trim().toLowerCase();
const links = document.querySelectorAll(".link-item");
links.forEach((link) => {
const title = link.querySelector("a").textContent.toLowerCase();
if (title.includes(filter)) {
link.style.display = "";
} else {
link.style.display = "none";
}
});
});
renderLinks();