簡體   English   中英

Chrome擴充功能-JavaScript中的功能無法正常運作

[英]Chrome extension - function in javascript not working properly

這是我關於stackoverflow的第一個問題。 我正在嘗試為Chrome擴展程序編寫代碼,以幫助其入圍。 想法是在書簽欄中有一個名為“ Shortlists”的文件夾,以及一些名為“ www.stackoverflow.com”或“ www.amazon.com”或“ www.bbcnews.com”的子文件夾。當我發現一些有趣的東西但要稍后閱讀,我可以在瀏覽器欄中單擊“短列表擴展圖標”,這將自動創建書簽(如果需要,還可以創建父文件夾)。 我沒有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. 每次重新啟動chrome時,只會加載一次bck.js,這意味着全局變量僅被聲明和初始化一次。
  2. chrome.bookmarks。*是異步的。 無論在此api外部編寫的任何函數都將首先執行,然后執行chrome api。

因此,將foldercheck全局變量移至函數中。 刪除api以外的所有條件。

這應該工作

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

        }


    })
}

並請縮進您的代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM