-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
141 lines (110 loc) · 3.96 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
document.addEventListener('DOMContentLoaded', function () {
function debounce(func, wait) {
let timeout;
return function (...args) {
const context = this;
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(context, args), wait);
};
}
var ul = document.getElementById('bookmarks');
var toggleFavicons = document.getElementById('toggleFavicons');
var enabledFavicons = localStorage.getItem('enableFavicons') !== 'false';
toggleFavicons.checked = enabledFavicons;
toggleFavicons.addEventListener('change', function () {
enabledFavicons = this.checked;
localStorage.setItem('enableFavicons', this.checked);
renderBookmarks(
enabledFavicons
);
});
// Step 1: Add an input field for search
var searchInput = document.getElementById('searchInput');
var filteredBookmarks = [];
// Step 2: Listen for 'input' event on the search field
searchInput.addEventListener('input', debounce(function () {
renderBookmarks(enabledFavicons, this.value);
}, 300)); // 300ms delay
function simpleSearch(term, text) {
term = term.toLowerCase()?.split(' ');
text = text.toLowerCase();
return term.some(function (word) {
return text.includes(word);
})
}
const renderBookmarks = function (
enabledFavicons,
searchTerm = ''
) {
Array.from(ul.children).forEach(function (li) {
li.classList.add('hiding');
});
setTimeout(function () {
// Clear the list after the transition
ul.innerHTML = '';
chrome.bookmarks.getTree(function (bookmarkTreeNodes) {
var bookmarks = flattenBookmarks(bookmarkTreeNodes);
if (searchTerm) {
bookmarks = bookmarks.filter(bookmark => simpleSearch(searchTerm, bookmark.title + bookmark.url));
}
// Sort bookmarks by dateAdded
bookmarks.sort((a, b) => b.dateAdded - a.dateAdded);
// Get the 50 most recent bookmarks
bookmarks = bookmarks.slice(0, 50);
filteredBookmarks = bookmarks;
bookmarks.forEach(function (bookmark) {
var li = document.createElement('li');
li.classList.add('bookmark', 'hiding');
var a = document.createElement('a');
var img = document.createElement('img');
var date = document.createElement('p');
var deleteButton = document.createElement('button');
a.href = bookmark.url;
a.textContent = bookmark.title;
a.target = '_blank';
a.title = bookmark.url;
var bookmarkDate = new Date(bookmark.dateAdded);
date.textContent = 'Date added: ' + bookmarkDate.toLocaleDateString();
deleteButton.textContent = 'Delete';
deleteButton.classList.add('delete');
deleteButton.addEventListener('click', function () {
if (window.confirm('Are you sure you want to delete this bookmark?')) {
chrome.bookmarks.remove(bookmark.id, function () {
li.remove();
});
}
});
if (
enabledFavicons
) {
img.src = 'https://www.google.com/s2/favicons?domain=' + bookmark.url;
img.style.height = '16px';
img.style.width = '16px';
img.style.marginRight = '5px';
a.insertBefore(img, a.childNodes[0]);
}
li.appendChild(a);
li.appendChild(date);
li.appendChild(deleteButton);
ul.appendChild(li);
setTimeout(function () {
li.classList.remove('hiding');
}, 0);
});
});
}, 0)
}
// Call renderBookmarks with the current search term
renderBookmarks(enabledFavicons, searchInput.value);
});
function flattenBookmarks(bookmarkTreeNodes) {
var bookmarks = [];
bookmarkTreeNodes.forEach(function (node) {
if (node.children) {
bookmarks = bookmarks.concat(flattenBookmarks(node.children));
} else {
bookmarks.push(node);
}
});
return bookmarks;
}