Download javascript in Wpf WebBrowser

I have this WebBrowseron my page:

<WebBrowser HorizontalAlignment="Stretch" Name="Browser"
            VerticalAlignment="Stretch" Grid.Column="1"/>

Now after loading the page I want to enter the code JavaScriptusing

var doc = (HTMLDocument)Browser.Document;
var head = doc.getElementsByTagName("head").Cast<HTMLHeadElement>().First();
var script = (IHTMLScriptElement)doc.createElement("script");
script.text = MYSCRIPTCODE;
head.appendChild((IHTMLDOMNode)script);

And in MYSCRIPTCODEI have this function:

function checkSomething() {

    try{
           var xhr = new XMLHttpRequest();
           var url = 'weburl';
           xhr.open("GET", url, true);
           xhr.onreadystatechange = function () {
           //Somecode
           }

           xhr.send();
    } catch {

    }
}

And I launched it with

Browser.InvokeScript("checkSomething");

It works fine and runs the script without problems.

I have a different state when I upload a local html file to the browser:

Uri uri = new Uri(AppDomain.CurrentDomain.BaseDirectory + @"OfflinePage.html");
Browser.Navigate(uri);

And when I want to run it with:

Browser.InvokeScript("checkSomething");

This line in the script get exception is Permission Denied:

xhr.open("GET", url, true);

Any idea why this is happening? why does it work on a page that I download from the Internet and will not work in local html?

+4
source share
1 answer

OfflinePage.html , URL XHR - -. WebBrowser . XDomainRequest , - , Access-Control-Allow-Origin: * HTTP-, HTTP Access Control (CORS) .

+3

All Articles