Reputation: 598
I am trying to bring back the folder id of any bookmark folder which has a title matching a given string.
Problem is it doesn't return back folder ids when the text is the same :C
This is my code:
chrome.bookmarks.getTree(function(bookmarks)
{
search_for_url(bookmarks, "herpaderp");
});
function search_for_title(bookmarks, title)
{
for(i=0; i < bookmarks.length; i++)
{
if(bookmarks[i].url != null && bookmarks[i].title == title)
{
// Totally found a folder that matches!
return bookmarks[i].id;
}
else
{
if(bookmarks[i].children)
{
// inception recursive stuff to get into the next layer of children
return search_for_title(bookmarks[i].children, title);
}
}
}
// No results :C
return false;
}
Upvotes: 0
Views: 403
Reputation: 4162
There are two problems with your search_for_title
function.
The variable i
must be local. To make it a local variable, you have to use var i = 0 instead of i = 0
in the for
statement.
search_for_title
returns false
when it can't find a bookmark that has the specified title, but you still need to look into the next item, so after recursively calling search_for_title
, you return
the return value only if the bookmark has been found. Otherwise, search should be continued instead of returning false
.
Here's the code I tested to run correctly:
function search_for_title(bookmarks, title)
{
for(var i=0; i < bookmarks.length; i++)
{
if(bookmarks[i].url != null && bookmarks[i].title == title)
{
// Totally found a folder that matches!
return bookmarks[i].id;
}
else
{
if(bookmarks[i].children)
{
// inception recursive stuff to get into the next layer of children
var id = search_for_title(bookmarks[i].children, title);
if(id)
{
return id;
}
}
}
}
// No results :C
return false;
}
Upvotes: 1