3

I'm trying to use the Chrome Extension to get a content of a site when browsing a other site. I'm calling "XMLHttpRequest" in the occurrence of chrome.webRequest.onCompleted But whenever I call the method XHR.Open get the following error: [Exception: DOMException] in fields Status and StatusText from XHR object.

Any idea ?

Thanks.

I am using the code below:

chrome.webRequest.onCompleted.addListener(
function(details) {
    if (details.url.substring(0, 23) == "https://www.google.com/") // I know I do not need this
    {
        console.info("URL :" + details.url);
        FindData("www.altavista.com");
    }
}, 
// filters
{
    urls: [
        "http://*.google.com/*", 
        "https://*.google.com/*", 
    ],
    types: ["image"]
},
["responseHeaders"]);

function FindData(strURL) {
    var req = new XMLHttpRequest();
    req.open("GET", strURL, true);
    req.onreadystatechange=function() {
        if (req.readyState==4) {
            if (req.status==200)
            {
                console.info("Sucess!");
                console.info("Data: " + req.responseText);
            }
        else if (req.status==404) console.info("URL doesn't exist!")
        else console.info("Error: Status is " + req.status)
        }
    }
    req.send();
}

My manifest.json

{
  "name": "Test",
  "version": "1.0",
  "manifest_version": 2,
  "description": "The first extension that I made.",
  "browser_action": {
    "default_popup": "popup.html"
  },
    "permissions": ["webRequest", "webRequestBlocking",
                  "http://www.altavista.com/*",
                  "http://*.google.com/*",
                  "https://*.google.com/*"]
}
brunossilva
  • 33
  • 1
  • 1
  • 3

1 Answers1

1

You have to add a protocol. www.altavista.com resolves to chrome-extension://..../www.altavista.com. Using http://www.altavista.com should solve your problem.

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Rob W, Do you mean to use `FindData("http://www.altavista.com");` This didn't work either. – brunossilva Jul 02 '12 at 17:21
  • @brunossilva I have created a demo extension based on your code, and it works as expected. I assumed that you want to permanently catch all google visits during the life time of the extension. Adjustments: 1. I've copy-pasted your code in a file called `background.js`. In the manifest, I referenced this file using the `"background":{"scripts":["background.js"]}`. 2. I've removed the "I know I do not need this"-if-block. It works: [background.js](http://pastebin.com/ut5z00d1), [manifest.json](http://pastebin.com/v0NKSMJK). – Rob W Jul 02 '12 at 17:29
  • Your current code may not work when `http://www.google.com/` redirects to e.g. `http://www.google.nl/`, because you did not declare the permission to access the other domain in the manifest file. Make sure that you check this. – Rob W Jul 02 '12 at 17:30
  • 1) How can I debug with script in background ? 2) The listener catches everything OK. The problem happens when I call the Open method of XHR. In debug mode I see the content of the status field getting the error: [Exception: DOMException]. In your example, do the line bellow work? `console.info("Data: " + req.responseText);` – brunossilva Jul 02 '12 at 18:26
  • I put a "/" after link, false on XHR.Open and ... success ! 'FindData("http://www.altavista.com/");' Thanks ! – brunossilva Jul 02 '12 at 18:44
  • @brunossilva The backgrounds script can be debugged via the extension page. For a simple explanation + screenshot, see [this answer](http://stackoverflow.com/a/10258029/938089?google-chrome-extension-logging-from-background-js). – Rob W Jul 02 '12 at 19:06