Reputation: 781
I am writing an extension (my first want, also I am not well versed in JS, actually this is my first project in the language). And I am having problems while fetching an url using XHR the backgroud page.
I am getting an status code of 0 and no data from a request. The strange thing is that when I use Wireshark (to sniff the packages sent by Chrome) I get the data OK and with a status code of 200. Below you see my manifest file and the code. I basically copied the code form the content script documentation page link:
I have read that it sometimes happens when you don't indicate in the manifest.json the permissions but I think they are ok. Thank you in advance.
Heres the manifest:
Manifest.json
{
"name": "XXXXX",
"version": "0.1",
"description": "test extension",
"browser_action": {
"default_icon": "icon.png",
"popup": "popup.html"
},
"content_scripts": [
{
"matches": ["http://*.amazon.com/*"],
"js": ["jquery.js", "content.js"]
}
],
"background_page": "background.html",
"permissions": [
"tabs",
"http://finance.yahoo.com/*",
"http://*.amazon.com/"
]
}
background.html
function fetchCurrency(callback) {
var invocation = new XMLHttpRequest();
var url = 'http://download.finance.yahoo.com/d/quotes.csv?e=.csv&f=l1&s=USDCOP=X';
invocation.onreadystatechange = function(data) {
alert('Invocation Ready State: ' +invocation.readyState);
if (invocation.readyState == 4) {
alert('Invocation Status: ' + invocation.status); //shows 0!!
if (invocation.status == 200 ) {
var data = invocation.responseText;
alert('The data es ' + data);
callback(data);
} else {
callback(null);
}
}
}
invocation.open('GET', url, true);
invocation.send();
};
function onRequest(request, sender, callback) {
if (request.action == 'getRate') {
fetchCurrency(callback);
}
};
// Wire up the listener.
chrome.extension.onRequest.addListener(onRequest);
/*
I then connect the content script as exemplified in the link I
posted. All is working OK but I am getting a weird 0 status code :'(
*/
Upvotes: 0
Views: 736
Reputation: 23253
Try this in your manifest instead:
"http://*.finance.yahoo.com/*"
Upvotes: 1