This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookmark.js
129 lines (110 loc) · 3.36 KB
/
bookmark.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
// Listen for form submit
document.getElementById('myForm').addEventListener('submit', saveBookmark);
// Save Bookmark
function saveBookmark(e) {
// Get form values
const siteName = document.getElementById('siteName').value;
const siteUrl = document.getElementById('siteUrl').value;
if (!validateForm(siteName, siteUrl)) {
return false;
}
const bookmark = {
name: siteName,
url: siteUrl,
};
/*
// Local Storage Test
localStorage.setItem('test', 'Hello World');
console.log(localStorage.getItem('test'));
localStorage.removeItem('test');
console.log(localStorage.getItem('test'));
*/
// Test if bookmarks is null
let bookmarks;
if (localStorage.getItem('bookmarks') === null) {
// Init array
bookmarks = [];
// Add to array
bookmarks.push(bookmark);
// Set to localStorage
localStorage.setItem('bookmarks', JSON.stringify(bookmarks));
} else {
// Get bookmarks from localStorage
bookmarks = JSON.parse(localStorage.getItem('bookmarks'));
// Add bookmark to array
bookmarks.push(bookmark);
// Re-set back to localStorage
localStorage.setItem('bookmarks', JSON.stringify(bookmarks));
}
// Clear form
document.getElementById('myForm').reset();
// Re-fetch bookmarks
fetchBookmarks();
// Prevent form from submitting
e.preventDefault();
}
// Delete bookmark
function deleteBookmark(url) {
// Get bookmarks from localStorage
const bookmarks = JSON.parse(localStorage.getItem('bookmarks'));
// Loop through the bookmarks
for (let i = 0; i < bookmarks.length; i++) {
if (bookmarks[i].url === url) {
// Remove from array
bookmarks.splice(i, 1);
}
}
// Re-set back to localStorage
localStorage.setItem('bookmarks', JSON.stringify(bookmarks));
// Re-fetch bookmarks
fetchBookmarks();
}
// Fetch bookmarks
function fetchBookmarks() {
// Get bookmarks from localStorage
const bookmarks = JSON.parse(localStorage.getItem('bookmarks'));
// Get output id
const bookmarksResults = document.getElementById('bookmarksResults');
// Build output
bookmarksResults.innerHTML = '';
for (let i = 0; i < bookmarks.length; i++) {
const name = bookmarks[i].name;
const url = bookmarks[i].url;
bookmarksResults.innerHTML +=
`${'<div class="card jumbotron text-left">' +
'<h3>'}${name} <a class="btn btn-light" target="_blank" href="${addhttp(
url
)}"> Visit </a> ` +
` <a onclick="deconsteBookmark('${url}')" class="btn btn-danger" href="#"> Deconste </a> ` +
`</h3>` +
`</div>`;
}
}
// Validate Form
function validateForm(siteName, siteUrl) {
if (!siteName || !siteUrl) {
alert('Please fill in the form');
return false;
}
const expression = /[-a-zA-Z0-9@:%_+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_+.~#?&//=]*)?/gi;
const regex = new RegExp(expression);
if (!siteUrl.match(regex)) {
alert('Please use a valid URL');
return false;
}
return true;
}
function addhttp(url) {
if (!/^(?:f|ht)tps?:\/\//.test(url)) {
url = `http://${url}`;
}
return url;
}
// bookmarksResults.innerHTML +=
// '<div class="card jumbotron text-left"><h3>' +
// name +
// '<a class="btn btn-light" target="_blank" href="' +
// url +
// '"> Visit </a> <a class="btn btn-danger" href="#" onclick="deconsteBookmark(\'' +
// url +
// '\')"> Deconste </a> </h3> </div>'; }