Search code examples
javascriptc#webbrowser-controllocal-variablesdeveloper-tools

Make javascript variable global scope in C# web browser


Is there any possibility to read local variable from javascript in c# WebBrowser component? In standard web browser like Chrome I have access to local variables by debugger (Google Developer Tools) and then I can bring variable to global scope like this:

window.myGlovalVar = myLocalVar;

What possibilities I have in my case?


Solution

  • You still can use Visual Studio Debugger to debug JavaScript in a custom application hosting the WebBrowser control, as described here.

    Another option is to type and execute WebBrowser.InvokeScript (WPF) or WebBrowser.Document.InvokeScript (WinForms) directly from the debugger's QuickWatch window.

    E.g., set a variable:

    webBrowser.InvokeScript("execScript", "window.myGlovalVar = myLocalVar")
    

    Query it later (will return the value myLocalVar remembered above):

    webBrowser.InvokeScript("eval", "window.myGlovalVar")