-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
64 lines (51 loc) · 1.84 KB
/
popup.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
function saveBookmark(e) {
e.preventDefault();
const title = document.querySelector('#bookmark-form_title').value.trim();
const url = document.querySelector('#bookmark-form_url').value.trim();
const bookmark = { title, url };
browser.storage.local.get('bookmarks').then(result => {
const bookmarks = result.bookmarks || [];
bookmarks.push(bookmark);
browser.storage.local.set({ bookmarks }).then(() => {
showBookmarks(bookmarks);
document.querySelector('#bookmark-form').reset();
});
});
}
function deleteBookmark(index) {
browser.storage.local.get('bookmarks').then(result => {
const bookmarks = result.bookmarks || [];
bookmarks.splice(index, 1);
browser.storage.local.set({ bookmarks }).then(() => {
showBookmarks(bookmarks);
});
});
}
function showBookmarks(bookmarks) {
const list = document.querySelector('#bookmark-list');
list.innerHTML = '';
if (bookmarks.length !== 0) {
const header = document.querySelector('#bookmark-list_header');
header.textContent = 'Saved Bookmarks!';
}
bookmarks.forEach((bookmark, index) => {
const item = document.createElement('li');
const title = document.createElement('a');
const deleteBtn = document.createElement('button');
item.className = 'bookmark-item';
title.className = 'bookmark-item_title';
deleteBtn.className = 'bookmark-item_delete';
title.href = bookmark.url;
title.textContent = bookmark.title;
title.target = '_blank';
deleteBtn.textContent = 'Delete';
deleteBtn.onclick = () => deleteBookmark(index);
item.appendChild(title);
item.appendChild(deleteBtn);
list.appendChild(item);
});
}
document.querySelector('#bookmark-form').addEventListener('submit', saveBookmark);
browser.storage.local.get('bookmarks').then(result => {
showBookmarks(result.bookmarks || []);
});