Skip to content

Instantly share code, notes, and snippets.

@srcrip
Created February 13, 2021 17:36
Show Gist options
  • Save srcrip/272b15ab162db676bdc8053562c30646 to your computer and use it in GitHub Desktop.
Save srcrip/272b15ab162db676bdc8053562c30646 to your computer and use it in GitHub Desktop.
Flatten the browser bookmarks tree from an extension (Chrome or Firefox)
import browser from 'webextension-polyfill'
// Flatten a tree with 'children' properties recursively
const flattenTree = data => {
return data.reduce((r, { children, ...rest }) => {
if (rest.url) r.push(rest)
if (children) r.push(...flattenTree(children))
return r
}, [])
}
// Reduce and flatten the bookmarks tree.
export async function getBookmarks () {
const rootNode = await browser.bookmarks.getTree()
return flattenTree(rootNode)
}
import { getBookmarks } from 'bookmarks'
getBookmarks().then(bookmarks => console.log(bookmarks))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment