0

The DevTools that come with WebView2 can either be opened by the user e.g. by pressing the F12 key, and they also can be opened programatically from code.

What I do not know is how to determine whether the DevTools are currently being opened.

I want to store the state (opened/closed) upon the exit of my WPF application so that I can show them automatically upon the next start of my WPF application.

My question

How to get the current showing/not showing state of the WebView2 Developer Tools?


Update 1

I found a dirty hack because of the fact that the window title of the DevTools looks like this:

DevTools - localhost:38472/my/url

Whereas localhost:38472/my/url is the currently loaded URL of the WebView2.

So I'm doing the following.

With the help of this SO answer, I was able to create this method:

public static IEnumerable<string> GetAllDesktopMainWindowTitles()
{
    // https://stackoverflow.com/a/7268375/107625

    var processlist = Process.GetProcesses();

    foreach (var process in processlist)
    {
        if (!string.IsNullOrEmpty(process.MainWindowTitle))
        {
            yield return process.MainWindowTitle;
        }
    }
}

I then can use it like this:

public static bool AreDevToolsOpen(this WebView2? webView)
{
    var url = webView?.Source?.ToString();
    if (string.IsNullOrEmpty(url)) return false;

    url = url.Replace(@"https://", string.Empty).Replace(@"http://", string.Empty);

    var titles = GetAllDesktopMainWindowTitles().ToList();

    var devToolsOpen = titles.Any(t => t.Contains(@"DevTools") && t.Contains(url));

    return devToolsOpen;
}

While this is a total hack, it seems to work good enough for now.

Still, I'm looking for a better way to do it.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1
    Related: https://stackoverflow.com/questions/61549409/is-the-debugger-attached – Poul Bak Aug 07 '22 at 19:17
  • Why do you want to know this? There is a way to know it is not open with AreDevToolsEnabled Property. Then once you have set this it will only be open if you open it. – darbid Oct 07 '22 at 19:35
  • [The documentation](https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2settings.aredevtoolsenabled?view=webview2-dotnet-1.0.1343.22) says: "**Determines whether the user is able to use the context menu or keyboard shortcuts to open the DevTools window.**" How is this related to my question? – Uwe Keim Oct 07 '22 at 20:08

0 Answers0