Skip to content

Commit

Permalink
Fix bookmark feature
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aledeg committed Mar 22, 2019
1 parent 654a231 commit 608a470
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
31 changes: 17 additions & 14 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

"manifest_version": 2,
"name": "LinkDump",
"version": "1.8",
"version": "1.9",
"description": "__MSG_extensionDescription__",
"icons": {
"48": "icons/linkdump-48.png"
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "linkdump",
"version": "1.8.0",
"version": "1.9.0",
"description": "Store links and dump them",
"main": "background.js",
"scripts": {
Expand Down

0 comments on commit 608a470

Please sign in to comment.