Previously I had the code:
$.get(url, function(data){
var $response = $(data);
overallRating = $response.find("div.grade").eq(0).html();
arr[pos].rating = overallRating;
in order to get the data I was looking for. This worked for the most part but I couldn't assign the value to arr[pos]. I found that because $.get() is asynchronous I can use chrome.runtime.onMesssage and .sendMessage and use the callback function to access the value. So I came up with this in my background script, tabcheck.js:
chrome.runtime.onMessage.addListener(function(message, send, callback){
console.log("message recieved");
$.get(send.url , function(data){
console.log(data);
callback(data);
});
return true; //https://stackoverflow.com/questions/17246133/contexts-and-methods-for-communication-between-the-browser-action-background-sc
});
and the sendmessage in my popup.js page:
chrome.runtime.sendMessage('message', {url: url } , function(data) {
console.log(data);
var $response = $(data);
overallRating = $response.find("div.grade").eq(0).html();
arr[pos].rating = overallRating;
});
and I think what is happening is that in the onmessage the url is not being accessed. I remember getting an error similar to: Chrome Extention Port error: Could not establish connection. Receiving end does not exist , although don't remember if that's the exact one. Data comes back undefined
So what has changed between the first and second that would make the new function not be able to access the url anymore? How can I fix it? My goal is to get the value overAllRating into arr[pos].rating.
Heres my manifest if its necessary:
{
"manifest_version": 2,
"name": "",
"description": "n",
"version": "1.0",
"background":{
"scripts": ["jquery-2.1.3.min.js","tabcheck.js"],
"persistent": false
},
"permissions":[
"tabs",
"https://www.myurl1/*",
"http://www.myurl2/*",
],
"browser_action": {
"default_icon": "icon.png",
"default_popup": "uv.html",
"default_title": ""
},
"content_scripts": [
{
"matches":["https://www.myurl/*"],
"js":["jquery-2.1.3.min.js","main.js"]
}
]
}