-
Notifications
You must be signed in to change notification settings - Fork 0
/
task2.js
104 lines (93 loc) · 3.27 KB
/
task2.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
setInterval(function() {
let myDate = new Date();
let newDate = ` ${myDate.getDate()} - ${myDate.getMonth() + 1} - ${myDate.getFullYear()} , ( ${convert24To12(myDate.getHours())}:${myDate.getMinutes()+ " min "}:${myDate.getSeconds()+" sec"})`
document.getElementById('date').innerHTML = newDate;
}, 1000)
function convert24To12(hours) {
if (hours > 12) {
return `${hours-12} PM `
} else {
return `${hours} AM `
}
}
document.getElementById('myForm').addEventListener('submit', saveBookmark);
function saveBookmark(e){
var siteName =document.getElementById('siteName').value;
var siteUrl =document.getElementById('siteUrl').value;
if(!validateForm(siteName, siteUrl)){
alert("This Website already exist");
}
var bookmark = {
name: siteName,
url: siteUrl
}
if(localStorage.getItem('bookmarks') === null){
var bookmarks = [];
bookmarks.push(bookmark);
localStorage.setItem('bookmarks', JSON.stringify(bookmarks));
} else {
var bookmarks = JSON.parse(localStorage.getItem('bookmarks'));
bookmarks.push(bookmark);
localStorage.setItem('bookmarks', JSON.stringify(bookmarks));
}
document.getElementById('myForm').reset();
fetchBookmarks();
e.preventDefault();
}
function deleteBookmark(url){
var bookmarks = JSON.parse(localStorage.getItem('bookmarks'));
for(var i =0;i < bookmarks.length;i++){
if(bookmarks[i].url == url){
bookmarks.splice(i, 1);
}
}
localStorage.setItem('bookmarks', JSON.stringify(bookmarks));
fetchBookmarks();
}
function fetchBookmarks(){
var bookmarks = JSON.parse(localStorage.getItem('bookmarks'));
var bookmarksResults = document.getElementById('bookmarksResults');
bookmarksResults.innerHTML = '';
for(var i = 0; i < bookmarks.length; i++){
var name = bookmarks[i].name;
var url = bookmarks[i].url;
bookmarksResults.innerHTML += '<div class="well">'+
'<h3>'+name+
' <a class="btn btn-default" target="_blank" href="'+addhttp(url)+'">Visit</a> ' +
' <a onclick="deleteBookmark(\''+url+'\')" class="btn btn-danger" href="#">Delete</a> ' +
'</h3>'+
'</div>';
}
}
function validateForm(siteName, siteUrl){
if(siteName ===""|| siteName=== null ){
alert('Please Enter a valid data');
for(var i=0;i<bookmarks.length;i++)
{
if(bookmarks[i].name===siteName)
return false;
}
return true
}else if(siteUrl===""|| siteUrl===null){
alert('Please Enter a valid URL');
for(var i=0;i<bookmarks.length;i++)
{
if(bookmarks[i].url===siteUrl)
return false;
}
return true
}
var expression = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
var 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;
}