Search code examples
javascriptfunctiongoogle-chromegoogle-chrome-extensionbookmarks

Chrome extension - function in javascript not working properly


This is my first question on stackoverflow. I am trying to write code for a chrome extension that helps in shortlisting. The idea is to have a folder named Shortlists in bookmark bar, and a few subfolders by name "www.stackoverflow.com" or "www.amazon.com" or "www.bbcnews.com" etc. When I find something interesting but want to read later, I may click on "shortlist extension icon" in the browser bar, and that will automatically create bookmark (and parent folder if the need be). I have no prior experience in javascript programming. I have written the following code - bck.js, which is invoked by 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  }
    }

With this I am able to create bookmarks, but there are bugs like, the creating multiple shortlists, multiple shortlist folders in bookmark bar or multiple urls in subfolders. I am unable to decode a pattern in the bug. Please help me resolve this. Thanks in advance. Though I dont think there is a problem in manifest.json, here is the file for reference.

{
  "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"
        ]
}

Solution

  • A couple of pointers before I get started

    1. bck.js is loaded only once for every chrome restart, which means the global variables are declared and initialised only once.
    2. chrome.bookmarks.* is asynchronous. Whatever functions are written outside this api gets executed first , then the chrome apis are executed.

    So move the foldercheck global variable into the function. Remove any if conditions outside the apis.

    This should work

    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
    
            }
    
    
        })
    }
    

    and please indent your code