Chrome 扩展 - javascript 中的功能无法正常工作

Chrome extension - function in javascript not working properly

这是我关于 Whosebug 的第一个问题。我正在尝试为有助于入围的 chrome 扩展编写代码。这个想法是在书签栏中有一个名为 Shortlists 的文件夹,以及一些名称为 "www.whosebug.com" 或 "www.amazon.com" 或 "www.bbcnews.com" 等的子文件夹。当我发现一些有趣的东西但想稍后阅读时,我可以单击浏览器栏中的 "shortlist extension icon",这将自动创建书签(如果需要,还可以创建父文件夹)。我之前没有 javascript 编程经验。我编写了以下代码 - bck.js,它由 manifest.json

调用
    var foldercheck = false;

chrome.browserAction.onClicked.addListener(function(tab){
    var taburl  = tab.url
    var urlsplit  = taburl.split("://")
    var actual_url 
    if (urlsplit.length == 2) 
    {
        actual_url = urlsplit[1]
    }
    else 
    {
        actual_url = urlsplit[0]
    }
    var website = actual_url.split("/") 
    website = website[0] 
    console.log ('taburl: '+ taburl + ' actual_url: '+ actual_url+' website:  ' + website )
    createShortList(website,taburl)
    });

    function createShortList(website,taburl) {
    console.log(website + '    ' + taburl)
    chrome.bookmarks.getTree(function(bookmarks){
    chrome.bookmarks.search("Shortlists", function(slist){  
        console.log ("slist")
        if (slist[0]){
        console.log ("slistw2")
        chrome.bookmarks.getChildren(slist[0].id,function(slistchildren){
        slistchildren.forEach(function (slistchild){
        if (slistchild.title == website)
        {
            chrome.bookmarks.create({'parentId' : slistchild.id, 'title' : taburl , 'url' : taburl})
            console.log('added in Shortlists1, '+slistchild.id +' ')
            foldercheck = true
        }
        })})}
        if (foldercheck == false)       
        {
            chrome.bookmarks.create({'parentId' : slist[0].id, 'title' :  website}, function(folder) {
                chrome.bookmarks.create({'parentId' : folder.id, 'title' : taburl, 'url' : taburl})
                console.log('added in Shortlists2, '+folder.id +' ')
                foldercheck = true
            })
            foldercheck = true
        }
        })
    })

    if (foldercheck == false) {
    chrome.bookmarks.create({'parentId': "1", 'title': 'Shortlists'}, function(shortlist) {
    chrome.bookmarks.create({'parentId' : shortlist.id, 'title' :  website}, function(folder2)
    {chrome.bookmarks.create({'parentId' : folder2.id, 'title' : taburl, 'url' : taburl});
    console.log('added in Shortlists3, '+folder2.id +' ')
    ;})})
    foldercheck = true  }
    }

有了这个我可以创建书签,但是有一些错误,比如创建多个候选名单、书签栏中的多个候选名单文件夹或子文件夹中的多个 url。我无法解码错误中的模式。请帮我解决这个问题。提前致谢。虽然我认为 manifest.json 中没有问题,但这里是供参考的文件。

{
  "name": "Shortlist",
  "manifest_version" : 2,
  "version" : "1.0",
  "description" : "To ShortList",
  "background" : {"scripts" : ["bck.js"], "persistent" : false},
  "browser_action": {"default_icon" : "icon.png"},
  "permissions": [
          "activeTab", "bookmarks"
        ]
}

开始之前的一些提示

  1. bck.js 每次 chrome 重启只加载一次,这意味着全局变量只声明和初始化一次。
  2. chrome.bookmarks.* 是异步的。在此 api 之外编写的任何函数首先执行,然后执行 chrome api。

因此将 foldercheck 全局变量移动到函数中。删除 apis.

之外的所有 if 条件

这应该有效

function createShortList(website,taburl) {
    foldercheck = false;

    chrome.bookmarks.search("Shortlists", function(slist){  

        // Shortlist folder exists
        if (slist[0]){

            // Folder with website name exists
            chrome.bookmarks.getChildren(slist[0].id,function(slistchildren){
                slistchildren.forEach(function (slistchild){
                    if (slistchild.title == website) {
                        chrome.bookmarks.create({'parentId' : slistchild.id, 'title' : taburl , 'url' : taburl})
                        foldercheck = true
                    }
                })
                // Folder with website name doesnt exist
                if (foldercheck == false){

                    chrome.bookmarks.create({'parentId' : slist[0].id, 'title' :  website}, function(folder) {
                        chrome.bookmarks.create({'parentId' : folder.id, 'title' : taburl, 'url' : taburl})
                        foldercheck = true

                    })
                }
            })
        }
        // Shortlist folder does not exist
        else{

            chrome.bookmarks.create({'parentId': "1", 'title': 'Shortlists'}, function(shortlist) {
                chrome.bookmarks.create({'parentId' : shortlist.id, 'title' :  website}, function(folder2){
                    chrome.bookmarks.create({'parentId' : folder2.id, 'title' : taburl, 'url' : taburl})
                })
            })
            foldercheck = true

        }


    })
}

请缩进您的代码