Is there a way to enable the IE8 Developer Tools from inside WebBrowser control in a .NET application

12,563

Solution 1

No, as others have said this is not possible. However there is a way you can wire most of the window errors through to a custom handler. Once the document has finished loading you can attach a listener to the window. e.g.

webBrowser.DocumentCompleted += (o, e) =>
{
    webBrowser.Document.Window.Error += (w, we) =>
    {
        we.Handled = true;

        // Do something with the error...
        Debug.WriteLine(
            string.Format(
               "Error: {1}\nline: {0}\nurl: {2}",
               we.LineNumber, //#0
               we.Description, //#1
               we.Url));  //#2
    };
};

Solution 2

I believe the developer tools are implemented in the IE host (iexplore.exe), not in MSHTML itself. Obviously the hooks are there for it, but I don't think you can get to the UI and stuff from the control.

Solution 3

There isn't a way for the embedded hosts to use the built-in developer tools. But if you want to debug you should still be able to, you can attach visual studio / windbg to your app, at worse you could insert breakpoints with the "debugger" keyword. In VS you might have to select script from the "select..." menu under "debug these code types".

Solution 4

One option is to open a child window from the embedded page, the child window opens in IE and the Developer Tools work, you can then do

window.opener

in the console to refer to the parent and manipulate the page.

Or replace the parents console with the child's and redirect to it.

 var logWindow = window.open();
        logWindow.document.write('<html><head><title>Child Log Window</title></head>\x3Cscript>window.opener.console = console;\x3C/script><body><h1>Child Log Window</h1></body></html>');
        window.onunload = function () {
            if (logWindow && !logWindow.closed) {
                logWindow.close();
            }
        };
Share:
12,563

Related videos on Youtube

Brien W.
Author by

Brien W.

Software Architect working in the video game industry

Updated on June 11, 2020

Comments

  • Brien W.
    Brien W. almost 4 years

    If you have IE8, you may have noticed a really handy feature that MS has added. Hit F12 and Developer Tools, a firebug like debugger, pops up. This is extremely useful for debugging purposes, and i'm wondering if there is a way to pop up the Developer Tools from a WebBrowser control inside a .NET application.

    My situation is this: I have a C# application that has an embedded WebBrowser control. The C# app and the DHTML web browser contents communicate with each other using the ObjectForScripting (C# side) and window.external (DHTML side) interfaces, so in order to test/debug/troubleshoot the full functionality, i need a way to trigger Developer Tools from within the WebBrowser control. Up to now we've been limited to using Firebug Lite which is severely limited or triggering a step debug session of the javascript using the 'debugger;' js, but now we're getting to the point where those options becoming a real hassle and don't allow the full features that we would get out of having something like Firebug or Developer Tools at our disposal.

    What I'd really love to do is to be able to pop up Developer Tools from inside my WebBrowser control while the app is running, but I haven't found a way to accomplish this yet.

    Has anybody else ran into this issue and found out if there's a way to make it happen?

  • casaout
    casaout over 8 years
    btw, this only works with the WinForms WebBrowser control, not the WPF one
  • Fraser
    Fraser over 8 years
    @casaout - you should be able to use the LoadCompleted event and then access the Window object. FWIW there is no "WPF browser", it is still the native WebBrowser ActiveX control.
  • JJS
    JJS over 3 years
    some of this only works in Visual Studio 2017. The javascript console was removed in VS2019.