I am making an extension, which has a background page (and a script associated with it: eventPage.js) and a content script (work.js)
The content script scans the web page, and retreives data in an array:
var datas = new Array();
var i = 0;
$('.szovegbox_kn tbody').each(function () {
var data = $(this).first("tr").find("td:nth-child(5)").html();
if (data !== undefined) {
datas[i] = data.replace(/\s/g, '');
chrome.runtime.sendMessage({
data: datas[i]
});
i++;
}
});
As you can see, data[i] gets sent to the background page when the variable data isn't undefined.
This is my background page's script:
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if (request.data !== undefined) {
var data = request.data;
var newURL = "http://www.something.com/loadpage.php?dest=" + data;
chrome.tabs.create({
url: newURL
});
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo) {
if (changeInfo.status === 'complete') {
window.setTimeout(function () {
chrome.tabs.remove(tabId);
}, 3000);
}
});
}
});
What I'd like to do is, that when a valid message (which's type isn't undefined) gets to the background page, the tab creation gets delayed by n seconds, and only ONE tab gets created until the tab gets closed. Right now, if in my datas[] array there's 100 records, my background page opens all 100 tabs instantely. I tried with setTimout, without success.
tl;dr: Data array gets sent to the background page, open tabs one by one with a ten second delay between them.
If you need more info, I can provide