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);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.