Filtering a specific bookmark folder item in chrome extension

非 Y 不嫁゛ 提交于 2020-05-08 05:57:36

问题


I need to obtain a specific bookmark group or folder "codebase" among many root folders present. Also, i need to display its sub folder or sub group under "codebase" into my Popup html form. From chrome docs, i believe below function can do the stuff, but it needs ID of the folder, how to obtain the ID ?

chrome.bookmarks.getSubTree(string id, function callback)

FYI, popup html form is externally controlled by a script, where i need to place the code for above requirement. Thanks for your time !!!


回答1:


I used the below code to solve the problem, Could anyone please certify the below code by checking the "correct" symbol for this answer or by comment.

var temP = [];
  var found;
  $(document).ready(function(){
    chrome.bookmarks.getTree(function(bookmarks){
        found = search_for_title(bookmarks, "here goes title of an existing bookmark which user need to search"); // found variable will have the id of bookmark we r searching.
        chrome.bookmarks.getChildren(found, function(children) { //using the ID of the bookmark we can process further requirement
            children.forEach(function(bookmark) {   // here i'm dwelling into sub folder to extract the content
            console.debug(bookmark.title);// these were the subfolder titles
            console.log(bookmark.id);// these were the subfolder ids
            });
        });
    });

    function search_for_title(bookmarks, title){ // first argument is entire bookmarks, second argument is title which we specified for search
        for(var i=0; i < bookmarks.length; i++){ 
            if(bookmarks[i].title == title){ 
            return bookmarks[i].id;   // we will get the id of the bookmark we are searching for
            }
            else{
                if(bookmarks[i].children){  
                    var id = search_for_title(bookmarks[i].children, title);
                    if(id)
                    return id;
                }
            }
        }

    return false;
    }
});

Thanks



来源:https://stackoverflow.com/questions/15130078/filtering-a-specific-bookmark-folder-item-in-chrome-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!