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
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
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
.
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.
// Copyright 2015 The Chromium Authors. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.