-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
155 lines (118 loc) · 3.91 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
const modal = document.getElementById('modal');
const modalShow = document.getElementById('show-modal');
const modalClose = document.getElementById('close-modal');
const bookmarkForm = document.getElementById('bookmark-form');
const websiteNameEl = document.getElementById('website-name');
const websiteUrlEl = document.getElementById('website-url');
const bookmarksContainer = document.getElementById('bookmarks-container');
let bookmarks = [];
// Show Modal
function showModal() {
modal.classList.add('show-modal');
websiteNameEl.focus();
}
modalShow.addEventListener('click', showModal);
modalClose.addEventListener('click', () => {
modal.classList.remove('show-modal');
})
window.addEventListener('click', (e) => {
e.target === modal && modal.classList.remove('show-modal');
})
// Validate Form
function validate(nameValue, urlValue) {
if (!nameValue || !urlValue) {
alert('Informe o nome e a URL do site');
return false;
}
const expression = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/g;
var regex = new RegExp(expression);
if (!urlValue.match(regex)) {
alert("Informe um endereço web válido");
return false;
}
return true;
}
function fetchBookmarks() {
if (localStorage.getItem('book-keeper')) {
bookmarks = JSON.parse(localStorage.getItem('book-keeper'));
} else {
bookmarks = [
{
name: 'Udemy',
url: 'https://udemy.com'
}
];
localStorage.setItem('book-keeper', JSON.stringify(bookmarks));
}
buildBookmarks();
}
function buildBookmarks() {
bookmarksContainer.textContent = '';
bookmarks.forEach((bookmark) => {
const { name, url } = bookmark;
const item = document.createElement('div');
item.classList.add('item');
const closeIcon = document.createElement('i');
closeIcon.classList.add('fas', 'fa-times');
closeIcon.id = 'delete-bookmark';
closeIcon.title = "Excluir";
closeIcon.setAttribute('onclick', `deleteBookmark('${ url }')`);
const linkInfo = document.createElement('div');
linkInfo.classList.add('name');
const favicon = document.createElement('img');
favicon.setAttribute('src', `https://s2.googleusercontent.com/s2/favicons?domain=${url}`);
favicon.setAttribute('alt', name);
const link = document.createElement('a');
link.setAttribute('href', url);
link.setAttribute('target', '_blank');
link.textContent = name;
linkInfo.append(favicon, link);
item.append(closeIcon, linkInfo);
bookmarksContainer.appendChild(item);
})
}
function buildBookmarks_V2() {
bookmarks.forEach((bookmark) => {
const { name, url } = bookmark;
const item = document.createElement('div');
item.innerHTML = `
<div class="item">
<i class="fas fa-times" id="delete-bookmark" title="Excluir"></i>
<div class="name">
<img src="https://s2.googleusercontent.com/s2/favicons?domain=${url}" alt=${name}>
<a href=${ url } target="_blank">
${ name }
</a>
</div>
</div>
`;
bookmarksContainer.appendChild(item);
})
}
function deleteBookmark(url) {
bookmarks = bookmarks.filter((bookmark) => bookmark.url !== url);
localStorage.setItem('book-keeper', JSON.stringify(bookmarks));
fetchBookmarks();
}
function storeBookmark(e) {
e.preventDefault();
const nameValue = websiteNameEl.value;
let urlValue = websiteUrlEl.value;
if (!urlValue.includes('https://', 'http://')) {
urlValue = `https://${urlValue}`;
}
if (!validate(nameValue, urlValue)) return false;
const bookmark = {
name: nameValue,
url: urlValue,
}
bookmarks.push(bookmark);
localStorage.setItem('book-keeper', JSON.stringify(bookmarks));
fetchBookmarks();
bookmarkForm.reset();
websiteNameEl.focus();
}
// Event Listeners
bookmarkForm.addEventListener('submit', storeBookmark);
// On Load
fetchBookmarks();