Search code examples
sortinggoogle-chromegoogle-chrome-extensionbookmarks

Sorting Google Chrome Bookmarks


Is there a way to sort Google Chrome bookmarks?

Sort on-save/periodically/on-demand by name/date-added.

According to google bookmarks API https://developer.chrome.com/docs/extensions/reference/bookmarks/ I can possibly add callback to onCreated method, which retrieves all the siblings and rearrange them.

Any help or design tips will be highly appreaciated.

Searching for extensions, browsing chrome extensions API.


Solution

  • I resolved this.

    const bm = chrome.bookmarks
    type BookmarkTreeNode = chrome.bookmarks.BookmarkTreeNode
    type BookmarkDestinationArg = chrome.bookmarks.BookmarkDestinationArg
    
    bm.onCreated.addListener((id: string, bookmark: BookmarkTreeNode ) => {
        let parent = bookmark.parentId!;
        bm.getChildren(parent, (siblings: BookmarkTreeNode[]) => {
            siblings.sort((a, b) => a.title.localeCompare(b.title))
            siblings.forEach((sibling, index) => sibling.index = index)
            siblings.forEach(({ id, index}) => {
                bm.move(id, {
                    parentId: parent,
                    index: index,
                })
            })
        })
    })
    

    Full code: https://github.com/piotrpersona/bookmark-sort