-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
82 lines (71 loc) · 2.3 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
window.addEventListener('load',
function() {
renderShortcuts();
});
function openDialog() {
document.getElementById('dialogContainer').style.display = 'block';
}
function closeDialog() {
document.getElementById('dialogContainer').style.display = 'none';
}
function addShortcut() {
var label = document.getElementById('label').value;
var url = document.getElementById('url').value;
if (label && url) {
var shortcut = { label: label, url: url };
var shortcuts = localStorage.getItem('shortcuts');
if (!shortcuts) {
shortcuts = [];
} else {
shortcuts = JSON.parse(shortcuts);
}
if (shortcuts.length > 4) {
shortcuts.shift();
alert("Error: You cannot add more than 5 shortcuts")
}
else {
shortcuts.push(shortcut);
localStorage.setItem('shortcuts', JSON.stringify(shortcuts));
renderShortcuts();
}
closeDialog();
document.getElementById('label').value = '';
document.getElementById('url').value = '';
}
}
function deleteShortcut(index) {
var shortcuts = localStorage.getItem('shortcuts');
if (shortcuts) {
shortcuts = JSON.parse(shortcuts);
shortcuts.splice(index, 1);
localStorage.setItem('shortcuts', JSON.stringify(shortcuts));
renderShortcuts();
}
}
function renderShortcuts() {
var shortcuts = localStorage.getItem('shortcuts');
if (shortcuts) {
shortcuts = JSON.parse(shortcuts);
var container = document.getElementById('shortcuts-container');
container.innerHTML = '';
shortcuts.forEach(function (shortcut, index) {
var li = document.createElement('li');
li.className = 'shortcut';
var faviconimg = document.createElement('img');
faviconimg.src = "https://t1.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=" + shortcut.url + "&size=32";
var label = document.createElement('a');
label.innerText = shortcut.label;
label.href = shortcut.url;
var deleteButton = document.createElement('button');
deleteButton.innerHTML = "";
deleteButton.className = 'delete-button';
deleteButton.onclick = function () {
deleteShortcut(index);
};
li.appendChild(faviconimg);
li.appendChild(label);
li.appendChild(deleteButton);
container.appendChild(li);
});
}
}