-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
199 lines (179 loc) · 5.41 KB
/
script.js
File metadata and controls
199 lines (179 loc) · 5.41 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const modal = document.getElementById("modal");
const modalShow = document.getElementById("show-modal");
const modalClose = document.getElementById("close-modal");
const bookmarkForm = document.getElementById("bookmark-form");
const websiteName = document.getElementById("website-name");
const websiteUrl = document.getElementById("website-url");
const bookmarkContainer = document.getElementById("bookmarks-container");
let bookmarks = [];
let defaultBookmarks = [
{
name: "Custom Countdown",
url: "https://luisluft.github.io/luftCustomCountdown/",
},
{
name: "Music Player",
url: "https://luisluft.github.io/luftMusicPlayer/",
},
{
name: "Navigation Nation",
url: "https://luisluft.github.io/luftNavigationNation/",
},
{
name: "Animated Template",
url: "https://luisluft.github.io/luftAnimatedTemplate/",
},
{
name: "Light and Dark Mode",
url: "https://luisluft.github.io/luftLightDarkMode/",
},
{
name: "Joke Teller",
url: "https://luisluft.github.io/luftJokeTeller/",
},
{
name: "Quote Generator",
url: "https://luisluft.github.io/luftQuoteGenerator/",
},
{
name: "Picture in Picture",
url: "https://luisluft.github.io/luftPictureInPicture/",
computer: "yes",
},
{
name: "Infinity Scroll",
url: "https://luisluft.github.io/luftInfinityScroll/",
},
{
name: "Video Player",
url: "https://luisluft.github.io/luftVideoPlayer/",
},
{
name: "Form Validator",
url: "https://luisluft.github.io/luftFormValidator/",
},
{
name: "Rock Paper Scissors Lizard Spock",
url: "https://luisluft.github.io/luftRockPaperScissorsLizardSpock/",
},
{
name: "NASA APOD",
url: "https://luisluft.github.io/luftNasaApod/",
},
{
name: "Math Sprint Game",
url: "https://luisluft.github.io/luftMathSprintGame/",
},
{
name: "Kanban Board",
url: "https://luisluft.github.io/luftKanbanBoard/",
},
{
name: "Calculator",
url: "https://luisluft.github.io/luftCalculator",
},
{
name: "Splash Page",
url: "https://luisluft.github.io/luftSplashPage",
},
{
name: "Paint",
url: "https://luisluft.github.io/luftPaint",
computer: "yes",
},
{
name: "Pong Game",
url: "https://luisluft.github.io/luftPongGame",
computer: "yes",
},
{
name: "Monsters Rolodex",
url: "https://luisluft.github.io/my-monsters-rolodex/",
computer: "yes",
},
];
function showModal() {
modal.classList.add("show-modal");
websiteName.focus();
}
function buildBookmarks() {
bookmarkContainer.textContent = "";
bookmarks.forEach((bookmark) => {
const { name, url, computer } = bookmark;
const item = document.createElement("div");
item.classList.add("item");
const closeIcon = document.createElement("i");
closeIcon.classList.add("fas", "fa-window-close");
closeIcon.setAttribute("title", "Delete Bookmark");
closeIcon.setAttribute("id", "delete-bookmark");
closeIcon.setAttribute("onclick", `deleteBookmark('${url}')`);
const linkInfo = document.createElement("div");
linkInfo.classList.add("name");
const favicon = document.createElement("img");
favicon.setAttribute("src", `https://www.google.com/s2/u/0/favicons?domain=${url}`);
favicon.setAttribute("alt", "Favicon");
const link = document.createElement("a");
link.setAttribute("href", `${url}`);
link.setAttribute("target", "_blank");
link.textContent = computer === "yes" ? `${name} (computer only)` : name;
// Append all the different previously created html elements
linkInfo.append(favicon, link);
item.append(closeIcon, linkInfo);
bookmarkContainer.appendChild(item);
});
}
function deleteBookmark(url) {
bookmarks.forEach((bookmark, i) => {
if (bookmark.url === url) {
bookmarks.splice(i, 1);
}
});
localStorage.setItem("bookmarks", JSON.stringify(bookmarks));
fetchBookmarks();
}
function fetchBookmarks() {
if (localStorage.getItem("bookmarks")) {
bookmarks = JSON.parse(localStorage.getItem("bookmarks"));
} else {
bookmarks = defaultBookmarks;
localStorage.setItem("bookmarks", JSON.stringify(bookmarks));
}
buildBookmarks();
}
function storeBookmark(event) {
event.preventDefault();
const nameValue = websiteName.value;
let urlValue = websiteUrl.value;
if (!urlValue.includes("http://") && !urlValue.includes("https://")) urlvalue = `https://${urlValue}`;
if (!validateForm(nameValue, urlValue)) return false;
const bookmark = {
name: nameValue,
url: urlValue,
};
bookmarks.push(bookmark);
localStorage.setItem("bookmarks", JSON.stringify(bookmarks));
fetchBookmarks();
bookmarkForm.reset();
websiteName.focus();
}
function validateForm(nameValue, urlValue) {
const expression = /[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)?/gi;
const regex = new RegExp(expression);
if (!nameValue || !urlValue) {
alert("Please submit values for both fields.");
return false;
}
if (urlValue.match(regex)) {
alert("Successful match");
} else {
alert("Please provide a valid address");
return false;
}
return true;
}
modalShow.addEventListener("click", showModal);
modalClose.addEventListener("click", () => modal.classList.remove("show-modal"));
window.addEventListener("click", (e) => (e.target === modal ? modal.classList.remove("show-modal") : false));
bookmarkForm.addEventListener("submit", storeBookmark);
// On load
fetchBookmarks();