Access javascript variable from GeckoFx

This article helps developers to retrieve javascript variables from any webpage.
Two things are covered in the below code.

  • Injecting javascript into any webpage
  • Accessing javascript variable from the webpage.

Step 1: Navigating to the webpage (“https://gcatch.in/wp-content/uploads/2018/03/GecoFx.html”)


GeckoWebBrowser browser_gecko= new GeckoWebBrowser();
browser_gecko.Navigate("https://gcatch.in/wp-content/uploads/2018/03/GecoFx.html");
browser_gecko.DocumentCompleted += browser_DocumentCompleted_gecko;


private void browser_DocumentCompleted_gecko(object sender, GeckoDocumentCompletedEventArgs e)
{

            string jCode = " function variable() {return geckovariable;}"; // geckovariable is the variable name that exists in the webpage.
            GeckoElement script = browser_gecko.Document.CreateElement("script");
            script.TextContent = jCode;
            GeckoNode res = browser_gecko.Document.Head.AppendChild(script);

            string outString = "";
            AutoJSContext context = new AutoJSContext(browser_gecko.Window);
            JsVal result = context.EvaluateScript("variable()", browser_gecko.Window.DomWindow);
}

GeckoFx in WPF

Webbrowser component for WPF application. Default web browser component may have some limitation in terms of rendering webkits, html5.

GecoFx will help to render html5, etc.,
Below is a sample , how integrate GecoFx into your WPF Application

Install GecoFx from Nuget
PM> Install-Package GeckoFX -Version 1.0.5

Below are some of the functionalities frequency used with GeckoFx

  1. How to Invoke a script from GecoFx Control
    if user needs to execute a certain javascript function found in html page , you can use the below code to trigger the function.

    
    GeckoWebBrowser browser_gecko= new GeckoWebBrowser();
    browser_gecko.Navigate("https://gcatch.in/wp-content/uploads/2018/03/GecoFx.html");
    browser_gecko.DocumentCompleted += browser_DocumentCompleted_gecko;

     


    https://gcatch.in/wp-content/uploads/2018/03/GecoFx.html , you can check the page source and check javascript code for reference. below code will execute the function geckofunctioncheck

    
    private void browser_DocumentCompleted_gecko(object sender, GeckoDocumentCompletedEventArgs e)
    {
    AutoJSContext context = new AutoJSContext(browser_3w.Window); 
    context.EvaluateScript("geckofunctioncheck()"); 
    }
    


 
Namespaces



using Gecko;
using Gecko.Events;