diff --git a/src/functions/getBookmarks.ts b/src/functions/getBookmarks.ts new file mode 100644 index 0000000..c2ce973 --- /dev/null +++ b/src/functions/getBookmarks.ts @@ -0,0 +1,43 @@ +import { Bookmarks, Bookmark } from "../types/Bookmarks"; + +function createBookmark(from: browser.bookmarks.BookmarkTreeNode): Bookmark { + return { + name: from.title, + url: from.url + }; +} + +export default async (): Promise => { + const treeNode = await browser.bookmarks.getTree(); + + const root: Bookmarks = { + folder: [], + orphans: [] + }; + + const rootNode = treeNode[0].children.find((e)=>e.id === "menu________"); + + // Set all top level bookmarks (without folder) + root.orphans = rootNode.children + .filter((e)=> e.type === "bookmark" ) + .sort((a,b)=> a.index - b.index ) + .map(createBookmark); + + // Get all top level folders + root.folder = rootNode.children + .filter((e)=> e.type === "folder") + .filter((e)=> e.children.length > 0) + .sort((a,b)=> a.index - b.index ) + .map((e)=>{ + const children: Bookmark[] = e.children + .filter((e)=> e.type === "bookmark") + .sort((a,b)=> a.index - b.index ) + .map(createBookmark); + return { + name: e.title, + bookmarks: children + }; + }); + + return root; +}; diff --git a/src/manifest.json b/src/manifest.json index bf73653..b1d2319 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -13,7 +13,8 @@ "homepage": "startpage/index.html" }, "permissions": [ - "" + "", + "bookmarks" ], "applications": { "gecko": { diff --git a/src/types/Bookmarks.ts b/src/types/Bookmarks.ts new file mode 100644 index 0000000..73d87f8 --- /dev/null +++ b/src/types/Bookmarks.ts @@ -0,0 +1,16 @@ +interface Bookmarks { + folder: BookmarkFolder[]; + orphans: Bookmark[]; +} + +interface BookmarkFolder { + name: string; + bookmarks: Bookmark[]; +} + +interface Bookmark { + name: string; + url: string; +} + +export { Bookmarks, Bookmark, BookmarkFolder };