From ba91280b3595945cf09bb2729622af0da7cfabe2 Mon Sep 17 00:00:00 2001 From: Gergo Dulai Date: Wed, 1 Oct 2025 01:13:20 +0200 Subject: [PATCH] Better handling of chrome lib async calls --- background.js | 30 +++++++++++++++--------------- bookmark.js | 38 +++++++++++++++++--------------------- 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/background.js b/background.js index 92560b8..c358d8e 100644 --- a/background.js +++ b/background.js @@ -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(); diff --git a/bookmark.js b/bookmark.js index e13e422..8ef3e48 100644 --- a/bookmark.js +++ b/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); + }; + } +} \ No newline at end of file