When I send an array from content script to background script it becomes undefined in background script.
Content Script:
var sendLink = [];
var canList = document.querySelectorAll(".userName.name");
for(i=0;i<canList.length;i++)
{
sendLink[i] = canList[i].href;
console.log(sendLink[i]); //shows correct links
}
chrome.runtime.sendMessage(sendLink, function(response) {
console.log(`message from background: ${JSON.stringify(response)}`); // shows undefined
});
Background Script:
var recLink = [];
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
recLink = request.sendLink;
sendResponse({ result: recLink[0] }); //gives error cannot read property 0 of undefined.
chrome.tabs.create({url: recLink[0]});
window.open(recLink[0]);
return true;
});
Please tell me whats wrong and how do I send the array successfully.