-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
68 lines (56 loc) · 2.07 KB
/
Copy pathscript.js
File metadata and controls
68 lines (56 loc) · 2.07 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
const addBookmarkBtn = document.getElementById("add-bookmark");
const bookmarkList = document.getElementById("bookmark-list");
const bookmarkNameInput = document.getElementById("bookmark-name");
const bookmarkUrlInput = document.getElementById("bookmark-url");
document.addEventListener("DOMContentLoaded", loadBookmarks);
addBookmarkBtn.addEventListener("click", function () {
const name = bookmarkNameInput.value.trim();
const url = bookmarkUrlInput.value.trim();
if (!name || !url) {
alert("Please enter both name and URL.");
return;
} else {
if (!url.startsWith("http://") && !url.startsWith("https://")) {
alert("Please enter a valid URL starting with http:// or https://");
return;
}
addBookmark(name, url);
saveBookmark(name, url);
bookmarkNameInput.value = "";
bookmarkUrlInput.value = "";
}
});
function addBookmark(name, url) {
const li = document.createElement("li");
const link = document.createElement("a");
link.href = url;
link.textContent = name;
link.target = "_blank";
const removeButton = document.createElement("button");
removeButton.textContent = "Remove";
removeButton.addEventListener("click", function () {
bookmarkList.removeChild(li);
removeBookmarkFromStorage(name, url);
});
li.appendChild(link);
li.appendChild(removeButton);
bookmarkList.appendChild(li);
}
function getBookmarksFromStorage() {
const bookmarks = localStorage.getItem("bookmarks");
return bookmarks ? JSON.parse(bookmarks) : [];
}
function saveBookmark(name, url) {
const bookmarks = getBookmarksFromStorage();
bookmarks.push({ name, url });
localStorage.setItem("bookmarks", JSON.stringify(bookmarks));
}
function loadBookmarks() {
const bookmarks = getBookmarksFromStorage();
bookmarks.forEach((bookmark) => addBookmark(bookmark.name, bookmark.url));
}
function removeBookmarkFromStorage(name, url) {
let bookmarks = getBookmarksFromStorage();
bookmarks = bookmarks.filter((bookmark) => bookmark.name !== name || bookmark.url !== url);
localStorage.setItem("bookmarks", JSON.stringify(bookmarks));
}