1

I am trying to list all the links which are not visited yet from a list of URLs (Array) in Chrome Extension. But my below function is returning only one URL. Where I am doing wrong?

function remove_visited(urls, fn) {
    var links = urls,
    unvisitedUrls = [],
    done = false;
    console.log(urls);

    var checkUrl = function(d) {
        var url = d;
        console.log(url);
        return function(visitItems) {
            if (visitItems && visitItems.length > 0) {
                unvisitedUrls.push(url);
            }
            links.splice(links.indexOf(url));
            if(links.length <= 0) {
                return;
            }
        }
    }

    links.forEach(function(d) {
        chrome.history.getVisits(d, checkUrl(d));
    });

    fn(links);
}

Ref: Wrangling Asynchronous chrome.history calls

Community
  • 1
  • 1
chanchal1987
  • 2,320
  • 7
  • 31
  • 64

1 Answers1

3

You have probably missunderstood the meaning (and inner workings) of an asynchronous call.

I would propose the following approach:

  1. Provide a list of URLs and a callback to be executed with a list of unvisited URLs as argument (after the history check has been completed for all URLs).

  2. For each URL in the original list:
    a. Check if it has been visited (and add it to the list of unvisited URLs if so).
    b. Increment a checkedURLs counter.
    c. Check if all URLs have been (asynchronously) checked, i.e. checkedURLs equals the length of the original URL list.

  3. When you detect that all URLs have been checked (see 2.c.), execute the specified callback (see 1.), passing the list of unvisited URLs as argument.


Some sample code for a demo extension:

manifest.json:

{
    "manifest_version": 2,
    "name":    "Demo",
    "version": "0.0",

    "background": {
        "persistent": false,
        "scripts": ["background.js"]
    },
    "browser_action": { "default_title": "Demo Extension" },
    "permissions": ["history"]
}

background.js:

/* List of URLs to check against */
var urlList = [
    "http://stackoverflow.com/",
    "http://qwertyuiop.asd/fghjkl",
    "https://www.google.com/",
    "https://www.qwertyuiop.asd/fghjkl"
];

/* Callback to be executed after all URLs have been checked */
var onCheckCompleted = function(unvisitedURLs) {
    console.log("The following URLs have not been visited yet:");
    unvisitedURLs.forEach(function(url) {
        console.log("    " + url);
    });
    alert("History check complete !\n"
          + "Check console log for details.");
}

/* Check all URLs in <urls> and call <callback> when done */
var findUnvisited = function(urls, callback) {
    var unvisitedURLs = [];
    var checkedURLs = 0;

    /* Check each URL... */
    urls.forEach(function(url) {
        chrome.history.getVisits({ "url": url }, function(visitItems) {
            /* If it has not been visited, add it to <unvisitedURLs> */
            if (!visitItems || (visitItems.length == 0)) {
                unvisitedURLs.push(url);
            }

            /* Increment the counter of checked URLs */
            checkedURLs++;

            /* If this was the last URL to be checked, 
               execute <callback>, passing <unvisitedURLs> */
            if (checkedURLs == urls.length) {
                callback(unvisitedURLs);
            }
        });
    });
}

/* Bind <findUnvisited> to the browser-action */
chrome.browserAction.onClicked.addListener(function() {
    findUnvisited(urlList, onCheckCompleted);
});
gkalpak
  • 47,844
  • 8
  • 105
  • 118
  • Great. Thanks a lot. It is working perfect. I need to learn more on "asynchronous call". If possible could you please provide some good online tutorials links? – chanchal1987 Oct 27 '13 at 15:31
  • I don'y know any tutorial on the subject (it is more like "get the idea behind it, then pactice to get the hang of it"). For a short explanation see a couple of answers from **[here](http://stackoverflow.com/questions/16336367/what-is-the-difference-between-synchronous-and-asynchronous-programming-in-node)**. Take a look **[here](http://www.i-programmer.info/programming/theory/6040-what-is-asynchronous-programming.html)** for a more in-depth (possibly redantant) look. – gkalpak Oct 27 '13 at 15:54