I'm brand new to js and making a chrome extension and I want to take a message from the background.js and use it in the popup.js after I click a button.
popup.js
window.onload=function(){
document.getElementById("BTC").addEventListener("click", bitcoin);
}
function bitcoin(){
console.log("Clicked!");
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse){
if(msg.greeting == "BTC"){
var prices = msg.data;
console.log(prices);
sendResponse({});
}
});
}
background.js
function coinArray(coins){
console.log(coins);
for (var i = 0; i < 12; i++){
var prices = new Array();
prices[i] = coins[i].price_usd;
}
chrome.runtime.sendMessage({greeting: "BTC", data: prices}, function(response) {});
}
I expect to see an array of the top 12 cryptocurrencies' prices from Coinmarketcap.com as an array in my popup console when I click the "BTC" button. I can console.log it in background.js no problem. The function coinArray is called when I load the data from the online API which is an XMLHTTPRequest not shown in the code.
Currently, only the "Clicked!" is coming through on the other end and I feel as if the problem might be that the onMessage line is within the function itself? Not sure where to go from here after looking up solutions to this problem. Any Clarity would be much appreciated!