-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookmark.js
More file actions
82 lines (69 loc) · 2.67 KB
/
bookmark.js
File metadata and controls
82 lines (69 loc) · 2.67 KB
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
document.addEventListener("DOMContentLoaded", () => {
const bookmarkList = document.getElementById("bookmarkList");
chrome.storage.local.get({ bookmarks: [] }, (data) => {
const bookmarks = data.bookmarks;
if (bookmarks.length === 0) {
bookmarkList.innerHTML = "<p>No bookmarks saved yet.</p>";
return;
}
bookmarks.forEach((bookmark) => {
const li = document.createElement("li");
// link
const link = document.createElement("a");
link.href = bookmark.url;
link.textContent = bookmark.title || bookmark.url;
link.target = "_blank";
// date
const date = document.createElement("span");
date.textContent = ` (${new Date(bookmark.time).toLocaleString()})`;
date.style.fontSize = "0.9em";
date.style.color = "#555";
// note
const noteInput = document.createElement("input");
noteInput.type = "text";
noteInput.placeholder = "Add a note...";
noteInput.value = bookmark.note || "";
noteInput.style.marginLeft = "10px";
noteInput.style.width = "200px";
// save button
const saveButton = document.createElement("button");
saveButton.textContent = "Save";
saveButton.title = "Save note";
saveButton.style.marginLeft = "5px";
saveButton.addEventListener("click", () => {
chrome.storage.local.get({ bookmarks: [] }, (data) => {
const bookmarks = data.bookmarks;
const targetIndex = bookmarks.findIndex(b => b.url === bookmark.url);
if (targetIndex !== -1) {
bookmarks[targetIndex].note = noteInput.value;
chrome.storage.local.set({ bookmarks }, () => {
console.log("Note saved!");
});
}
});
});
// remove button
const removeButton = document.createElement("button");
removeButton.textContent = "Remove";
removeButton.title = "Remove bookmark";
removeButton.style.marginLeft = "5px";
removeButton.addEventListener("click", () => {
if (confirm("Are you sure you want to delete this bookmark?")) {
chrome.storage.local.get({ bookmarks: [] }, (data) => {
let bookmarks = data.bookmarks;
bookmarks = bookmarks.filter(b => b.url !== bookmark.url);
chrome.storage.local.set({ bookmarks }, () => {
location.reload();
});
});
}
});
li.appendChild(link);
li.appendChild(date);
li.appendChild(noteInput);
li.appendChild(saveButton);
li.appendChild(removeButton);
bookmarkList.appendChild(li);
});
});
});