user2146441
user2146441

Reputation: 228

Trap remote Security Sandbox Violation in Webbrowser

I'm visiting a website with a Webbrowser control like this:

webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.Navigate("http://www.mywebsite.com/");

I'm getting the following error.

SecurityError: Error #2060: Security sandbox violation: 
ExternalInterface caller
http://www.anotherwebsite.com/flash.swf
cannot access 
http://www.mywebsite.com/.

When I navigate to the initial url, it's not in the local domain. I'm not calling anything remote from a local location or vice versa. This is just an error in the website's javascript.

How can I trap this error, as it keeps putting a MessageBox prompt onto the screen?

Upvotes: 1

Views: 176

Answers (1)

drevans
drevans

Reputation: 58

Had this exact problem in my application and it was driving me mad!

I thought the ScriptErrorsSupressed flag on the webBrowser control should handle this but it doesn't. After a lot of trawling I found the answer on the MSDN page (who would have thought!):

// Hides script errors without hiding other dialog boxes.
private void SuppressScriptErrorsOnly(WebBrowser browser) {
    // Ensure that ScriptErrorsSuppressed is set to false.
    browser.ScriptErrorsSuppressed = false;

    // Handle DocumentCompleted to gain access to the Document object.
    browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);
}

private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
    ((WebBrowser)sender).Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error);
}

private void Window_Error(object sender, HtmlElementErrorEventArgs e) {
    // Ignore the error and suppress the error dialog box. 
    e.Handled = true;
}

That's it, simples.

Simply hook into the error event and set e.Handled = true. You don't need to manually add your event handler to browser.DocumentCompleted (you can do it in the event properties section) but you get the idea.

Upvotes: 1

Related Questions