Better handling of chrome lib async calls
This commit is contained in:
38
bookmark.js
38
bookmark.js
@@ -2,33 +2,29 @@
|
||||
export async function createBookmarks(nodes, parentId) {
|
||||
for (const node of nodes) {
|
||||
if (node.url) {
|
||||
chrome.bookmarks.create({ parentId, title: node.title, url: node.url });
|
||||
await 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);
|
||||
}
|
||||
});
|
||||
const newFolder = await chrome.bookmarks.create({ parentId, title: node.title });
|
||||
if (node.children && node.children.length > 0) {
|
||||
await createBookmarks(node.children, newFolder.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Recursively delete all children
|
||||
export async function removeBookmarks(parentId, done) {
|
||||
export async function removeBookmarks(parentId) {
|
||||
const children = await chrome.bookmarks.getChildren(parentId);
|
||||
if (!children || children.length === 0) return done?.();
|
||||
let count = children.length;
|
||||
children.forEach((child) => {
|
||||
if (!children || children.length === 0) return;
|
||||
|
||||
for (let i = 0; i < children.length; i++) {
|
||||
const child = children[i];
|
||||
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?.();
|
||||
});
|
||||
});
|
||||
await chrome.bookmarks.remove(child.id);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
await removeBookmarks(child.id);
|
||||
await chrome.bookmarks.remove(child.id);
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user