-
Notifications
You must be signed in to change notification settings - Fork 1
/
bookmark_helper.js
47 lines (45 loc) · 1.21 KB
/
bookmark_helper.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
export async function findOrCreateBookmarkFolder(name) {
let results = await browser.bookmarks.search({ title: name });
if (results.length === 0) {
let id = await createFolder();
return id;
} else if (results.length === 1) {
return results[0].id;
} else {
let candidate = null;
for (let result of results) {
if (
"undefined" == typeof result.url &&
"unfiled_____" === result.parentId
) {
if (candidate) {
// more than one valid candidate
throw new Error("more than one folder with name: " + name);
} else {
candidate = result;
}
}
}
if (candidate) {
return candidate.id;
} else {
return await createFolder();
}
}
async function createFolder() {
let folder = await browser.bookmarks.create({ title: name });
return folder.id;
}
}
export function saveCurrentWindow(bookmarkFolderId) {
browser.tabs.query({ currentWindow: true }).then((tabList) => {
for (let i = tabList.length - 1; i >= 0; i--) {
browser.bookmarks.create({
parentId: bookmarkFolderId,
index: i,
title: tabList[i].title,
url: tabList[i].url,
});
}
});
}