Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

bookmarks.getChildren()

bookmarks.getChildren() retrieves all the immediate children of a given bookmark folder, identified as a BookmarkTreeNode ID.

Syntax

chrome.bookmarks.getChildren(
  id,                     // string
  function(               // function
    results                 // array of BookmarkTreeNode objects
  ) {...}
)

This API is also available as browser.bookmarks.getChildren() in a version that returns a promise.

Parameters

id
A string which specifies the ID of the folder whose children are to be retrieved.
callback
A function to be called once the list of child nodes has been retrieved; it is passed the following parameters:
results
An array of objects of type bookmarks.BookmarkTreeNode; each entry represents a single child node. The list is ordered in the same order in which the bookmarks appear in the user interface. Separators are currently not included in the results. If the specified node has no children, the array is empty.

Browser compatibility

Chrome Edge Firefox Firefox for Android Opera
Basic support Yes No 45.0 No 33

Examples

Basic usage

function gotChildren(children) {
  console.log(children);
}

chrome.bookmarks.getChildren(bookmarkItemId, gotChildren);

Toggle a bookmark in a specific folder

This example creates a new bookmark in a particular folder, given that folder's BookmarkTreeNode. If a bookmark with the specified title already exists, however, the existing bookmark is instead deleted and no new bookmark is created.

function toggleBookmark(folderNode, title, url) {
  chrome.bookmarks.getChildren(folderNode.id, function(results) {
    let node = results.find(function(el) {
      return el.title === title;
    });

    // If the bookmark is present, we need to delete it; otherwise,
    // create it instead.

    if (node !== undefined) {
      chrome.bookmarks.remove(node.id);
    } else {
      chrome.bookmarks.create({
        parentId: folderNode.id,
        title: title,
        url: url
      });
    }
  });
}

The toggleBookmark() function begins by calling bookmarks.getChildren() to get a list of all the bookmarks which already exist in the BookmarkTreeNode specified by folderNode.

The anonymous function provided as a callback receives a single parameter, results, which is an array of all of the folder's immediate children. The first step is to look to see if a node with the specified title exists in the folder; this is done using the Array.find() method and a callback which compares the specified title to the title of the bookmark node being examined by find().

If a matching node is found (that is, node is not undefined), then we know a bookmark with the specified title already exists, so we call bookmarks.remove() to delete the existing bookmark.

Otherwise, bookmarks.create() is called to create the new bookmark, specifying the ID of the folder passed in the folderNode parameter as the new bookmark's parent, and with the specified title and url.

Acknowledgements

This API is based on Chromium's chrome.bookmarks API. This documentation is derived from bookmarks.json in the Chromium code.

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.

Document Tags and Contributors

 Contributors to this page: Makyen, wbamberg, Sheppy
 Last updated by: Makyen,