Better handling of chrome lib async calls
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import { createBookmarks, removeBookmarks } from "./bookmark.js";
|
||||
import { saveRevision, getRevision } from "./revision.js";
|
||||
|
||||
const syncBookmarks = async (id, bookmark) => {
|
||||
console.log('Syncing bookmarks! id: ' + id + ' bookmark: ' + JSON.stringify(bookmark));
|
||||
|
||||
const saveBookmarks = async (id, bookmark) => {
|
||||
console.log('Saving bookmarks! id: ' + id + ' bookmark: ' + JSON.stringify(bookmark));
|
||||
await saveRevision();
|
||||
}
|
||||
|
||||
@@ -19,7 +20,6 @@ const loadBookmarks = async () => {
|
||||
|
||||
console.log('Different revision IDs found! Old: ' + data.revId + ' New:' + savedRev.revId);
|
||||
|
||||
|
||||
removeListeners();
|
||||
|
||||
await chrome.storage.sync.set({ revId: data.revId });
|
||||
@@ -34,21 +34,21 @@ const loadBookmarks = async () => {
|
||||
}
|
||||
|
||||
function removeListeners() {
|
||||
chrome.bookmarks.onCreated.removeListener(syncBookmarks);
|
||||
chrome.bookmarks.onRemoved.removeListener(syncBookmarks);
|
||||
chrome.bookmarks.onChanged.removeListener(syncBookmarks);
|
||||
chrome.bookmarks.onMoved.removeListener(syncBookmarks);
|
||||
chrome.bookmarks.onChildrenReordered.removeListener(syncBookmarks);
|
||||
//chrome.bookmarks.onImportEnded.removeListener(syncBookmarks);
|
||||
console.log('Removing listeners!');
|
||||
chrome.bookmarks.onCreated.removeListener(saveBookmarks);
|
||||
chrome.bookmarks.onRemoved.removeListener(saveBookmarks);
|
||||
chrome.bookmarks.onChanged.removeListener(saveBookmarks);
|
||||
chrome.bookmarks.onMoved.removeListener(saveBookmarks);
|
||||
chrome.bookmarks.onChildrenReordered.removeListener(saveBookmarks);
|
||||
}
|
||||
|
||||
function addListeners() {
|
||||
chrome.bookmarks.onCreated.addListener(syncBookmarks);
|
||||
chrome.bookmarks.onRemoved.addListener(syncBookmarks);
|
||||
chrome.bookmarks.onChanged.addListener(syncBookmarks);
|
||||
chrome.bookmarks.onMoved.addListener(syncBookmarks);
|
||||
chrome.bookmarks.onChildrenReordered.addListener(syncBookmarks);
|
||||
//chrome.bookmarks.onImportEnded.addListener(syncBookmarks);
|
||||
console.log('Adding listeners!');
|
||||
chrome.bookmarks.onCreated.addListener(saveBookmarks);
|
||||
chrome.bookmarks.onRemoved.addListener(saveBookmarks);
|
||||
chrome.bookmarks.onChanged.addListener(saveBookmarks);
|
||||
chrome.bookmarks.onMoved.addListener(saveBookmarks);
|
||||
chrome.bookmarks.onChildrenReordered.addListener(saveBookmarks);
|
||||
}
|
||||
|
||||
addListeners();
|
||||
|
||||
36
bookmark.js
36
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