-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
47 lines (40 loc) · 1.37 KB
/
background.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
// background.js
chrome.action.onClicked.addListener(() => {
chrome.tabs.query({ currentWindow: true }, (tabs) => {
if (tabs.length === 0) {
console.log("No tabs to bookmark.");
return;
}
const today = new Date().toISOString().split("T")[0];
chrome.bookmarks.search({ title: "Bookit" }, (results) => {
let bookitFolder = results.find((folder) => folder.title === "Bookit");
if (!bookitFolder) {
chrome.bookmarks.create({ title: "Bookit" }, (newFolder) => {
bookitFolder = newFolder;
saveTabsToFolder(bookitFolder.id, today, tabs);
});
} else {
saveTabsToFolder(bookitFolder.id, today, tabs);
}
});
});
});
function saveTabsToFolder(parentId, date, tabs) {
chrome.bookmarks.getChildren(parentId, (children) => {
const dateFolders = children.filter((child) => child.title.startsWith(date));
let folderTitle = date;
if (dateFolders.length > 0) {
folderTitle = `${date} (${dateFolders.length + 1})`;
}
chrome.bookmarks.create({ parentId: parentId, title: folderTitle }, (dateFolder) => {
tabs.forEach((tab) => {
chrome.bookmarks.create({
parentId: dateFolder.id,
title: tab.title,
url: tab.url,
});
});
console.log(`Bookmarked ${tabs.length} tabs into folder 'Bookit/${folderTitle}'.`);
});
});
}