-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
183 lines (154 loc) · 4.51 KB
/
script.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
class NoActiveTabError extends Error {
constructor(message) {
super(message);
this.name = "NoActiveTabError";
}
}
class BookmarkCreationError extends Error {
constructor(message) {
super(message);
this.name = "BookmarkCreationError";
}
}
class ClearBookmarksFolderError extends Error {
constructor(message) {
super(message);
this.name = "ClearBookmarksFolderError";
}
}
async function save() {
try {
const activeTab = await getActiveTab();
const { groupId } = activeTab;
const grouped = groupId && groupId !== -1;
const title = grouped
? (await chrome.tabGroups.get(groupId)).title
: new Date().toLocaleString();
let bookmarksFolder =
(await getBookmarksFolderByTitle(title)) ||
(await createBookmarksFolder(title));
await clearBookmarksFolder(bookmarksFolder);
const tabs = await chrome.tabs.query({ groupId, active: false });
await Promise.all(
tabs.map((tab) => saveTabToBookmarks(tab, bookmarksFolder))
);
await saveTabToBookmarks(activeTab, bookmarksFolder);
if (grouped) {
await closeTabGroup(groupId);
}
} catch (error) {
handleSaveError(error);
}
}
async function clearBookmarksFolder(bookmarksFolder) {
try {
const existingBookmarks = await chrome.bookmarks.getChildren(
bookmarksFolder.id
);
for (const bookmark of existingBookmarks) {
await chrome.bookmarks.remove(bookmark.id);
if (chrome.runtime.lastError) {
throw new ClearBookmarksFolderError(chrome.runtime.lastError.message);
}
}
} catch (error) {
throw new ClearBookmarksFolderError(
`Error clearing bookmarks folder: ${error.message}`
);
}
}
async function createBookmark(tab, bookmarksFolder) {
try {
await chrome.bookmarks.create({
parentId: bookmarksFolder.id,
title: tab.title,
url: tab.url,
});
if (chrome.runtime.lastError) {
throw new BookmarkCreationError(chrome.runtime.lastError.message);
}
} catch (error) {
throw new BookmarkCreationError(
`Error creating bookmark: ${error.message}`
);
}
}
async function getActiveTab() {
const queryOptions = { active: true, lastFocusedWindow: true };
const [tab] = await chrome.tabs.query(queryOptions);
if (!tab) {
throw new NoActiveTabError(
"No Active tab. Please select a tab and try again."
);
}
return tab;
}
async function getBookmarksFolderByTitle(title) {
try {
const existingFolders = await chrome.bookmarks.search({ title });
return existingFolders.length > 0 ? existingFolders[0] : null;
} catch (error) {
throw new Error(`Error searching for bookmarks folder: ${error.message}`);
}
}
async function createBookmarksFolder(title) {
try {
const folder = await chrome.bookmarks.create({ title });
return folder;
} catch (error) {
throw new Error(`Error creating bookmarks folder: ${error.message}`);
}
}
async function saveTabToBookmarks(tab, bookmarksFolder) {
try {
// if (!(await doesBookmarkExist(tab, bookmarksFolder))) {
await createBookmark(tab, bookmarksFolder);
// }
await closeTab(tab);
} catch (error) {
throw new Error(`Error saving tab to bookmarks: ${error.message}`);
}
}
// async function doesBookmarkExist(tab, bookmarksFolder) {
// try {
// const query = {
// title: tab.title,
// url: tab.url,
// parentId: bookmarksFolder.id,
// };
// const existingBookmarks = await chrome.bookmarks.search(query);
// return existingBookmarks.length > 0;
// } catch (error) {
// throw new Error(`Error checking if bookmark exists: ${error.message}`);
// }
// }
async function closeTab(tab) {
await chrome.tabs.remove(tab.id);
if (chrome.runtime.lastError) {
throw new Error(chrome.runtime.lastError.message);
}
}
async function closeTabGroup(groupId) {
const activeTabGroup = await chrome.tabGroups.get(groupId);
await chrome.tabGroups.close(activeTabGroup, true);
if (chrome.runtime.lastError) {
throw new Error(chrome.runtime.lastError.message);
}
}
function handleSaveError(error) {
if (error instanceof NoActiveTabError) {
alert(error.message);
} else if (
error instanceof BookmarkCreationError ||
error instanceof ClearBookmarksFolderError
) {
console.error(error);
alert(error.message);
} else {
console.error(error);
alert("An error occurred. Please try again.");
}
}
document.addEventListener("DOMContentLoaded", () => {
document.getElementById("save-tabs-btn").addEventListener("click", save);
});