Chrome扩展-功能在javascript不能正常工作

Chrome extension - function in javascript not working properly

本文关键字:常工作 工作 不能 扩展 功能 javascript Chrome      更新时间:2023-09-26

这是我关于stackoverflow的第一个问题。我正试图编写一个chrome扩展,有助于入围代码。这个想法是在书签栏中有一个名为Shortlists的文件夹,以及一些名为"www.stackoverflow.com"或"www.amazon.com"或"www.bbcnews.com"等的子文件夹。当我发现一些有趣的东西,但想稍后阅读时,我可以点击浏览器栏中的"候选列表扩展图标",这将自动创建书签(和父文件夹,如果需要的话)。我以前没有javascript编程的经验。我已经写了下面的代码- back .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  }
    }
有了这个,我就可以创建书签,但是有一些bug,比如创建多个候选列表,书签栏中的多个候选列表文件夹或子文件夹中的多个url。我无法破解这个bug的模式。请帮我解决这个问题。提前感谢。不过我认为舱单上没有问题。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. back .js只在每次chrome重启时加载一次,这意味着全局变量只被声明和初始化一次。
  2. chrome.bookmarks。*是异步的。在这个api之外编写的任何函数都会首先执行,然后执行chrome api。

因此将foldercheck全局变量移动到函数中。删除api外部的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
        }

    })
}

请缩进你的代码