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)     { ...

CHECK IF FUNCTION EXISTS EXAMPLE

http://css-tricks.com/snippets/javascript/check-if-function-exists-before-calling/ When using scripts that are shared between different areas of a site, there may be cases where a function is called that doesn't exist. It makes sense on one page (the dependency is there) but not another. The difference is too slight to warrant forking the file into different versions. Instead, you can just check if the function exists before calling it to avoid the error: if ( typeof yourFunctionName == 'function' ) { yourFunctionName ( ) ; } jQuery code snippet to check whether a function exists within the JavaScript code. This can be easily achieved by using the jQuery.isFunction() function. Useful for checking if a jQuery function exists before calling it! < script type = "text/javascript" >      function  somenoobfunction ( )   {      }         // using jQuery      if   ( $. isFunction ( wi...

Various components of the .NET Platform and the functions performed by them

Now we will go in detail about the various components that build the .NET framework and its functionalities. Common Language Runtime At the core of the .NET platform is the Common Language Runtime (CLR). The CLR simplifies application development, provides a robust and secure execution environment, supports multiple languages and simplifies application deployment and management. The diagram below provides more details on the CLR's features: In this section we will cover some of the more significant features provided to .NET applications by the CLR. These include: • Memory Management • Common Type System Before moving further let us discuss briefly about Common Language Infrastructure(CLI) according to Standardizing Information and Communication Systems(ECMA) specifications. The Microsoft Shared Source CLI Implementation is a file archive containing working source code for the ECMA-334 (C#) and ECMA-335 (Common Language Infrastructure, or CLI) s...