35 lines
1.2 KiB
JavaScript
35 lines
1.2 KiB
JavaScript
// Recursively create bookmarks from nodes
|
|
export async function createBookmarks(nodes, parentId) {
|
|
for (const node of nodes) {
|
|
if (node.url) {
|
|
chrome.bookmarks.create({ parentId, title: node.title, url: node.url });
|
|
} else {
|
|
chrome.bookmarks.create({ parentId, title: node.title }, (newFolder) => {
|
|
if (node.children && node.children.length > 0) {
|
|
createBookmarks(node.children, newFolder.id);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
// Recursively delete all children
|
|
export async function removeBookmarks(parentId, done) {
|
|
const children = await chrome.bookmarks.getChildren(parentId);
|
|
if (!children || children.length === 0) return done?.();
|
|
let count = children.length;
|
|
children.forEach((child) => {
|
|
if (child.url) {
|
|
chrome.bookmarks.remove(child.id, () => {
|
|
if (--count === 0) done?.();
|
|
});
|
|
} else {
|
|
removeBookmarks(child.id, () => {
|
|
chrome.bookmarks.remove(child.id, () => {
|
|
if (--count === 0) done?.();
|
|
});
|
|
});
|
|
}
|
|
});
|
|
}
|