-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
120 lines (101 loc) · 3.94 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
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
document.addEventListener('DOMContentLoaded', function() {
const bookmarkBtn = document.getElementById('bookmarkBtn');
const bookmarkText = document.getElementById('bookmarkText');
bookmarkBtn.addEventListener('click', () => {
const customText = bookmarkText.value.trim();
if (!customText) {
alert("Please enter a custom note for the bookmark.");
return;
}
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const activeTab = tabs[0];
chrome.tabs.sendMessage(activeTab.id, { type: 'getCurrentTimestamp' }, (response) => {
if (response) {
chrome.storage.local.get('bookmarks', (data) => {
const bookmarks = data.bookmarks || [];
const newBookmark = {
url: activeTab.url,
title: activeTab.title,
timestamp: response.currentTime,
text: customText
};
bookmarks.push(newBookmark);
chrome.storage.local.set({ bookmarks }, () => {
displayBookmarks();
});
});
}
});
});
bookmarkText.value = ''; // Clear the input after saving
});
displayBookmarks(); // Ensure bookmarks are displayed on load
});
function displayBookmarks() {
chrome.storage.local.get('bookmarks', (data) => {
const bookmarkList = document.getElementById('bookmarkList');
bookmarkList.innerHTML = '';
(data.bookmarks || []).forEach((bookmark, index) => {
const listItem = document.createElement('li');
listItem.className = 'bookmark-item';
const textContainer = document.createElement('div');
textContainer.className = 'text-container';
const title = document.createElement('span');
title.className = 'bookmark-title';
title.textContent = bookmark.text;
const time = document.createElement('span');
time.className = 'bookmark-time';
time.textContent = formatTime(bookmark.timestamp);
const editBtn = document.createElement('button');
editBtn.textContent = 'Edit';
editBtn.className = 'edit-btn';
editBtn.addEventListener('click', (event) => {
event.stopPropagation(); // Prevent triggering the list item click event
const newText = prompt('Edit your bookmark note:', bookmark.text);
if (newText !== null) {
bookmark.text = newText.trim();
updateBookmark(index, bookmark);
}
});
const deleteBtn = document.createElement('button');
deleteBtn.textContent = 'Delete';
deleteBtn.className = 'delete-btn';
deleteBtn.addEventListener('click', (event) => {
event.stopPropagation(); // Prevent triggering the list item click event
deleteBookmark(index);
});
textContainer.appendChild(title);
textContainer.appendChild(time);
textContainer.appendChild(editBtn);
textContainer.appendChild(deleteBtn);
listItem.appendChild(textContainer);
listItem.addEventListener('click', () => {
chrome.tabs.create({ url: `${bookmark.url}&t=${Math.floor(bookmark.timestamp)}s` });
});
bookmarkList.appendChild(listItem);
});
});
}
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
const sec = Math.floor(seconds % 60);
return `${minutes}:${sec < 10 ? '0' : ''}${sec}`;
}
function updateBookmark(index, updatedBookmark) {
chrome.storage.local.get('bookmarks', (data) => {
const bookmarks = data.bookmarks || [];
bookmarks[index] = updatedBookmark;
chrome.storage.local.set({ bookmarks }, () => {
displayBookmarks();
});
});
}
function deleteBookmark(index) {
chrome.storage.local.get('bookmarks', (data) => {
const bookmarks = data.bookmarks || [];
bookmarks.splice(index, 1);
chrome.storage.local.set({ bookmarks }, () => {
displayBookmarks();
});
});
}