I'm trying to get a request from my database in my popup.js which responds with an array of objects. I created a for loop so that it iterates through it all, but on each iteration I would like to spawn a new tab. I have no problem spawning the tabs. My problem is that I can't seem to pass any data to them. I've been looking all over SO and cant find how to do this??
popup.js:
var button = document.getElementById("button");
button.addEventListener("click", function() {
chrome.runtime.sendMessage({opened: true}, function(response) {
console.log(response.example);
});
});
background.js:
// receives message from popup script
chrome.runtime.onMessage.addListener( function(request, sender, sendResponse) {
if (request) {
// sends response back to popup script
sendResponse({example: "sent from background.js"});
// sends response to content script
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var data = JSON.parse(xhr.responseText);
for (i = 0; i < data.length; i++) {
console.log(data)
chrome.tabs.create({url: "https://www.google.com/", active: false}, function(tab) {
var tab = tabs[0];
setTimeout(function(){
chrome.tabs.sendMessage(tab.id, data[i], function(response) {
console.log(response);
});
}, 6000);
});
}
}
}
xhr.open('GET', 'https://www.somedata.com', true);
xhr.send(null);
}
});
contentscript.js:
chrome.runtime.onMessage.addListener(function(request,sender,response) {
console.log(request)
console.log(sender)
console.log(response)
});
manifest.json:
{
"manifest_version": 2,
"name": "demo",
"short_name": "tabs",
"description": "Multiple tabs",
"version": "0.0.1",
"browser_action": {
"default_icon": "img/logo.png",
"128": "128.png",
"default_popup": "popup.html",
"default_title": "Sample"
},
"content_scripts": [{
"matches": ["https://www.google.com/*"],
"js": ["js/contentscript.js"]
}],
"permissions": [
"storage",
"tabs",
"http://*/*",
"https://*/*"
]
}