Pepita
Pepita

Reputation: 13

How i load a webpage in a popup chrome extension without iframe and using ajax

Manifiest.json

    {
        "name":"MyExtension",
        "version":"1.0",
        "description":"MyExtension Description.",
        "icons":{"128":"browseraction.png"},
        "browser_action":
                        {
                            "default_icon":"icon.png",
                            "default":"Mi default title",
                            "popup":"popup.html"
                        },
        "permissions":[
        "http://*/*",
        "tabs"
        ]
    }

and this is my popup.html

<html>
    <head>
        <script type="text/javascript">     
            var address = "http://www.google.com";
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = handleStateChange; 
            xhr.open("GET", chrome.extension.getURL(address), true);//This dont work
           //xhr.open("GET", chrome.extension.getURL('/popup1.html'), true);//thisWork
           xhr.send();
           function handleStateChange()
           {
               if (xhr.readyState == 4) 
               {
            
                  alert(xhr.responseText);
               }
               else
               {
                  alert('Not Yet');
               }
           }
        </script>
    </head>
    <body>

    </body>
</html>

I just want display the html code, with alert I read,http://code.google.com/chrome/extensions/xhr.html , and other resource but just dont work Thank in advance

Upvotes: 1

Views: 10309

Answers (1)

Rob W
Rob W

Reputation: 348992

Your code does not work, because chrome.extension.getURL does not work as you expect. For a given string, it attempts to resolve the URL to a local file in the chrome extension.

When http://www.google.com/ is given as input, www.google.com is returned. The XHR request does not complete because the URI is invalid (the protocol [and path] is missing). Also, both the (browser action) popup and Content scripts are restricted by the cross-origin policy.

To create cross-domain XHR requests, you have to incorporate a Background page and add the URI in manifest.json, at permissions file.

A full demo is available in this answer:

Upvotes: 4

Related Questions