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 enjoyed this ..!
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();
}
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)?
ReplyDeleteI 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!
Do you know how to load a .xpi extension in GeckoFX?
ReplyDeleteCan anyone tell me how to delete cookies for a particular site or domain in GeckoFX through C# code.
ReplyDeleteI USED this code but
ReplyDeleteangularjs and jquery not supporting .
What can i do?
like minified file
ReplyDeletewhen I am using www.angularjs.org.Its not working properly like expression "{{}}" is showing where data is binding
ReplyDeleteaudio calling website not supporting .
ReplyDelete