¿Cómo maneja Google Chrome Api la creación de pestañas?

I am building an extension, and I have come over a problem recentely.

I have a content script injected into a website, which scans a website, and sends that data to my background page's script.

Here's an example: (that's the content script)

var i = 0;
var cegszamok = new Array();
$('.szovegbox_kn tbody').each(function () {
    var $cegszam = $(this).first("tr").find("td:nth-child(5)").html();
    if ($cegszam !== undefined) {
        cegszamok[i] = $cegszam.replace(/\s/g, '');
        var port = chrome.runtime.connect({
            name: "jon"
        });
        port.postMessage({
            cegszam: cegszamok[i]
        });
        i++;
    }
});

This is the receiving end's code:

var szamok = new Array();
var i = 0;

chrome.runtime.onConnect.addListener(function (port) {
    console.assert(port.name == "jon");
    port.onMessage.addListener(function (msg) {
        szamok[i] = msg.cegszam;
        var newURL = "http://www.website.hu/loadpage.php?id=" + szamok[i];
        chrome.tabs.create({
            url: newURL
        });
        i++;
    });
});

What I am trying to achieve, is that if the content script's array has (let's say) 100 rows, is that the tabs get opened one-by-one, close the tab, wait a few seconds, then open the next one, etc.

However it seems to me, that chrome.tabs.create() doesn't get executed every time a message comes in, only after the connection has closed, and opens ALL tabs at the same time. I tried to set a delay with alarm(), setTimeout() e incluso _.throttle().

If I'm right, then I guess, that google chrome api saves all these tab creation methods in an array, and launches all of them when the connection is closed. Am I right? If not, how could I put a delay between the opening of the tabs? (one-by-one, as said before)

preguntado el 11 de septiembre de 13 a las 22:09

1 Respuestas

chrome.tabs.create() is asynchronous, use callback to handle something on operation completion. You code may look like

  chrome.tabs.create({
        url: newURL
    }, function(tab) { i++; });

Respondido el 13 de Septiembre de 13 a las 14:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.