3

I'm writing a packaged Chrome Web App and trying to parse external XML. It works when I download the XML file to my server, but does not work when I change the XML file to the location on the web.

For example, I am trying to access this file: http://www.w3schools.com/xml/note.xml

Here is my JavaScript code (note that HTML ):

function dashboardContent() {
html="";

xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","http://www.w3schools.com/xml/note.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

html+=xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
html+=xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
html+=xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;

document.getElementById("contentArea").innerHTML=html;

}

In my Chrome Web App manifest.json file, I put the following under permissions per Google's manifest requirement for Cross-origin XMLHttpRequests:

{
    "name": "BirdTrax",
    "version": "0.1.1",

    "description": "Birding explorer to help you plan birding trips and find recently eBirded sightings, rarities, and checklists in your area",
    "icons": {
         "16": "icons/helloWorld_16.png",
        "128": "icons/helloWorld_128.png"
    },

    "app": {
        "launch": {
            "local_path": "main.html",
            "container": "tab"
        }
    },

    "background_page": "background.html",
    "options_page": "options.html",


"permissions": [
  "http://www.w3schools.com/*",
  "notifications",
  "unlimitedStorage"
],

    "incognito": "split"


}

I'm not sure if I'm still missing something, but the code isn't working for me. Any suggestions, tips, ideas? I really appreciate any help!!

zdebruine
  • 3,687
  • 6
  • 31
  • 50

1 Answers1

3

Suffice a wildcard after /. Otherwise, you're allowing your extension to only request pages at the root.

"permissions": [
  "http://www.w3schools.com/*",

In your case, you're requesting only one URL. It might be even better to restrict the pattern even more:

"permissions": [
  "http://www.w3schools.com/xml/note.xml",
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • 1
    @wagtail Don't forget to (repack and) reload your extension. – Rob W Jul 07 '12 at 14:25
  • Yes :) I did that. But I'm not sure now, maybe the error lies in the way I did my XML parsing. But why would it work when the file is on my server then? – zdebruine Jul 07 '12 at 14:28
  • @wagtail Can you give more details? "It (...) does not work when I change the XML file to the location on the web." is a vague description of your problem. Is your page being redirected, .e.g `http://example.com/` to `http://www.example.com/`? This **does** matter to Chrome extensions. In this case, also add the location of the redirected URL to the permissions field. – Rob W Jul 07 '12 at 14:31
  • If I save the XML file on the web to my server, I can access the same file offline and successfully parse it. If I try to access the file online, however, the process doesn't go through. It's therefore not an error with my parsing, it's simply an error with the permissions or getting the XML document or something. – zdebruine Jul 07 '12 at 14:34
  • My page is not being redirected – zdebruine Jul 07 '12 at 14:34
  • 1
    @wagtail Could you show your actual code (CRX / manifest file + requestor)? – Rob W Jul 07 '12 at 14:41
  • This is the first time I am trying to download external XML content for a CWS app. I am starting out by trying the W3Schools file. I figure that if it works, the rest of the URLs should work too. – zdebruine Jul 07 '12 at 14:46