From 608a4701b045ae19ec1d89e590f4bc90d9f64db7 Mon Sep 17 00:00:00 2001 From: Alexis Degrugillier Date: Thu, 21 Mar 2019 23:05:55 +0100 Subject: [PATCH] Fix bookmark feature Before, when adding a folder of bookmarks, not all bookmarks weren't added to the dump due to the local storage interractions. There was many concurrent calls to the local storage and they all wrote what they know at the time they were accessing it. Thus we add data loss. Now the process is rewritten to make a single call with all links needed to be dumped. --- background.js | 31 +++++++++++++++++-------------- manifest.json | 2 +- package-lock.json | 2 +- package.json | 2 +- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/background.js b/background.js index 46c0f2e..22fa4c3 100644 --- a/background.js +++ b/background.js @@ -21,24 +21,27 @@ async function addLink(link) { const obj = await browser.storage.local.get('urls'); const urls = obj.urls || []; urls.push(link); - await browser.storage.local.set({ urls }); + await browser.storage.local.set({ "urls": urls.flat() }); } -async function addBookmark(id) { - const bookmarks = await browser.bookmarks.get(id); - for (const bookmark of bookmarks) { - switch (bookmark.type) { - case 'bookmark': - await addLink({ url: bookmark.url, title: bookmark.title}); - break; - case 'folder': - const children = await browser.bookmarks.getChildren(bookmark.id); - addBookmark(children.map(obj => obj.id)); - break; - default: - // Do nothing on purpose +function getLinks(bookmark, initialLinks = []) { + let links = [...initialLinks]; + + if (bookmark.url) { + links.push({ url: bookmark.url, title: bookmark.title}); + } + else if (bookmark.children) { + for (var child of bookmark.children) { + links = getLinks(child, links); } } + + return links; +} + +async function addBookmark(id) { + const [root] = await browser.bookmarks.getSubTree(id); + await addLink(getLinks(root)); } function getDownloadOptions(format) { diff --git a/manifest.json b/manifest.json index ea21d95..793e90c 100644 --- a/manifest.json +++ b/manifest.json @@ -7,7 +7,7 @@ "manifest_version": 2, "name": "LinkDump", - "version": "1.8", + "version": "1.9", "description": "__MSG_extensionDescription__", "icons": { "48": "icons/linkdump-48.png" diff --git a/package-lock.json b/package-lock.json index 5d6b72c..1fda183 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "linkdump", - "version": "1.8.0", + "version": "1.9.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index be6fd2f..f7d0dd9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "linkdump", - "version": "1.8.0", + "version": "1.9.0", "description": "Store links and dump them", "main": "background.js", "scripts": {