Created
February 13, 2021 17:36
-
-
Save srcrip/272b15ab162db676bdc8053562c30646 to your computer and use it in GitHub Desktop.
Flatten the browser bookmarks tree from an extension (Chrome or Firefox)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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