Skip to main content

Geckofx Browser in Winform Application

Bored with IE browser in your winform application ? Want to do everything as you doing in your Firefox or Chrome Browser ?
Play with automation ? Then here is your choice .

Introduction:

 GeckoFX is a Windows Forms control written in clean, commented C# that embeds the Mozilla Gecko browser control in any Windows Forms Application. It also contains a simple class model providing access to the HTML and CSS DOM.

GeckoFX was originally created by Andrew Young for the fast-growing visual CSS editor, Stylizer. It is now released as open-source under the Mozilla Public License. 

You can download it here :  Geckofx 22.0
And the supporting file Xulrunner here :  Xulrunner Files

Hope you have downloaded above two files. Here our journey going to start.
Create your winform application in visual studio and do the following:


right click the toolbox -> Choose items -> Browse the "Geckofx-winforms.dll" and click "yes" for “Load it anyway”.


Now in toolbox you can find "GeckoWebBrowser"




Just drag that GeckoWebBrowser to the form and add the tools as displayed in the following picture.



 So now you ready to do some interesting stuff with the browser.

The first step we need to set path for xulrunner which initialize the language package and basic dll files which are used to run Geckofx browser.

Path initialization should be earlier when project runs. So declare it In form’s constructor.

        public Form1()
        {
            InitializeComponent();
            Gecko.Xpcom.Initialize(AppDomain.CurrentDomain.BaseDirectory + "xulrunner");
        }

Initially geckobrowser does not support some memory consuming process like  playing videos , if you want to play just add the following code in constructor:


     Gecko.GeckoPreferences.Default["extensions.blocklist.enabled"] = false;


Then add the following code for the added tools,

        private void reloadButton_Click(object sender, EventArgs e)
        {
            geckoWebBrowser1.Reload();
        }

        private void backButton_Click(object sender, EventArgs e)
        {
            geckoWebBrowser1.GoBack();
        }


        private void forwardButton_Click(object sender, EventArgs e)
        {
            geckoWebBrowser1.GoForward();
        }

        privatevoid stopButton_Click(object sender, EventArgs e)
        {
            geckoWebBrowser1.Stop();
        }

        privatevoid textBox1_KeyUp(object sender, KeyEventArgs e)
        {
           if (e.KeyCode == Keys.Enter)
            {
                geckoWebBrowser1.Navigate(textBox1.Text);
            }
        }

And shall we add some external features to browser functionality.


        private void geckoWebBrowser1_StatusTextChanged(object sender, EventArgs e)
        {
            label1.Text = geckoWebBrowser1.StatusText;
        }

        private void geckoWebBrowser1_Navigating(object sender, GeckoNavigatingEventArgs e)
        {
            textBox1.Text = geckoWebBrowser1.Url.AbsoluteUri;
        }

        private void geckoWebBrowser1_Navigated(object sender, GeckoNavigatedEventArgse)
        {
            textBox1.Text = geckoWebBrowser1.Url.AbsoluteUri;
        }

Following code will remove the cookies :

      for (int c = 0; c < geckoWebBrowser1.Document.Cookie.Length; c++)
        {
            nsICookieManager CookieMan;
            CookieMan = Xpcom.GetService<nsICookieManager>("@mozilla.org/cookiemanager;1");
            CookieMan = Xpcom.QueryInterface<nsICookieManager>(CookieMan);
            CookieMan.RemoveAll();
        }

And the additional property of geckobrowser is we can change the proxy and UserAgents.

            Gecko.GeckoPreferences.User["network.proxy.http"] = "xxx.xxx.xxx.xxx";
            Gecko.GeckoPreferences.User["network.proxy.http_port"] = xxxx;
            Gecko.GeckoPreferences.User["network.proxy.type"] = 1;

Automation With Geckofx: 

Following code will explain about the automation in geckobrowser.  Shall we search in google with automation,

Add another button and name it as “autosearch” and write the following code.

        private void autosearch_Click(object sender, EventArgs e)
        {
            GeckoInputElement search =               new GeckoInputElement(geckoWebBrowser1.Document.GetElementsByName("q")[0].DomObject);
           GeckoInputElement button =     new GeckoInputElement(geckoWebBrowser1.Document.GetElementsByName("btnG")[0].DomObject);
            search.focus();
            search.Value = "Master Blaster Sachin";
            button.Click();
        }




Hope you enjoyed this ..!





Comments

  1. Hello, excelent article, but how can you load a .xpi extension in GeckoFX? Or is it possible to add some request headers to all requests (not just the main navigate request)?
    I wanted to install a headers plugin that allows me to add custom headers to the all requests made, but any other solution is appreciated.

    Thanks!

    ReplyDelete
  2. Do you know how to load a .xpi extension in GeckoFX?

    ReplyDelete
  3. Can anyone tell me how to delete cookies for a particular site or domain in GeckoFX through C# code.

    ReplyDelete
  4. I USED this code but
    angularjs and jquery not supporting .
    What can i do?

    ReplyDelete
  5. when I am using www.angularjs.org.Its not working properly like expression "{{}}" is showing where data is binding

    ReplyDelete
  6. audio calling website not supporting .

    ReplyDelete

Post a Comment

Popular posts from this blog

Code To Convert rupees(numbers) into words using C#.Net

Introduction: In my previous article I have explained about how to validate emailid using javascript . In this article I am going to explain about code used to convert rupees(numbers) into words using C#.Net . Explanation: For explanation purpose I have a page. It has a textbox to input the numbers. And when you click on the convert to words button then it will convert the input numbers into words and shows it in the below label. Below is the C# code used to do this functionality. public static string NumbersToWords( int inputNumber) {     int inputNo = inputNumber;     if (inputNo == 0)         return "Zero" ;     int [] numbers = new int [4];     int first = 0;     int u, h, t;     System.Text. StringBuilder sb = new System.Text. StringBuilder ();     if (inputNo < 0)     {         sb.Append( "Minus " );         inputNo = -inputNo;     }     string [] words0 = { "" , "One " ,

C# code to send mail using smtp from gmail,yahoo mail and live mail

Introduction: In my previous article I have explained about   How to bind/Unbind events in jQuery . In this article I am going to explain about how to send mail from ASP.Net using gmail,yahoomail and live mail credentials. Explanation: First Include the below namespaces in your code behind file. using System; using System.Net; using System.Net.Mail;