📜 ⬆️ ⬇️

How to connect a third-party browser in a C # application

image

At a certain point I was uncomfortable using the standard WebBrowser control offered by Visual Studio.
There were several reasons:
1. Used IE-engine, which in itself is already a strong argument.
2. Curve work with JS.
3. Lack of scaling.
4. If you run it on a machine where IE6 is installed, then all its “virtues” are transferred to the application.

As a result, the search for alternative solutions was started.
2 SDKs were reviewed. xulrunner (Mozilla) and Awesomium (Chrome)
')
Connecting both is about the same, but just in case I will describe both.


image
1. xulrunner
step 1
Go to the official site http://ftp.mozilla.org/pub/mozilla.org/xulrunner/releases/
Choose the version that suits us.
Here I would like to make a small digression. The later the version, the heavier it is: for example, 1.9v weighs 21Mb., And already 19v weighs 32Mb. In addition, each next version requires more and more resources. Looking ahead, I will say that this was the main reason for rejecting this SDK.

At this link go to the folder with the selected version, then sdk / xulrunner-XXen-US.win32.sdk.zip

step 2
Download and unpack the contents. We are interested in the archive only daddy bin. Copy it to the folder with the application and rename it to xulrunner. The name can be different, but so that there are no differences with my description, it’s better to do this.

step 3
To work with this SDK, we need the Skybound.GeckoFX.bin.v1.9.1.0 library. You can download it here http://code.google.com/p/geckofx/
This library runs xulrunner version 1.9.
If you decide to use a later version, then you need to look for Skybound.GeckoFX 2.0, it is suitable for versions 2.0 and higher.

step 4
In our application we add Skybound.Gecko.dll to the References from the downloaded archive.

step 5
Rule the Program class:
static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form2()); } } 


Initialize browser control:
 string path = "C:\\Program Files (x86)\\xulrunner\\"; //      bin   SDK Skybound.Gecko.Xpcom.Initialize(path); webBrowser1 = new Skybound.Gecko.GeckoWebBrowser(); webBrowser1.Parent = this.panel1; webBrowser1.Dock = DockStyle.Fill; string u = "http://www.ya.ru"; //   webBrowser1.Navigate(u); 

For this code, the parent panel will be panel1.

step 6
To execute any JavaScript programmatically I had to go for tricks, since whether the functions that are intended for this, in theory, are not written in this version, or something else. But I found the only way out:

 webBrowser1.Navigate("javascript:ImGecko()"); 


The downside is that you can not get the result of processing, only in the form of alert ();
This problem also caused me to start looking for a replacement and came to Awesomium

image

2. Awesomium
step 1
Download the SDK at http://awesomium.com/
The site has two versions - stable (1.6.5) and test (1.7). The test generally works better.
For the work we need the files
Awesomium.Core.dll
Awesomium.dll
Awesomium.Windows.Controls.Design.dll
Awesomium.Windows.Controls.dll
Awesomium.Windows.Forms.dll

step 2
We connect to the library project
Awesomium.Core.dll
Awesomium.Windows.Forms.dll

step 3
In the Toolbox, right-click and select Choose Items. Next, click browse and connect Awesomium.Windows.Forms.dll, then we will have new controls WebControl, AddressBox, etc. First of all, WebControl is important to us.

step 4
We place control in design

step 5
Examples of the use of various functions.

Opening pages
 webControl1.LoadURL("http://ya.ru"); 


Cookies download
 string cookie; //   cookie    string domen; //  string[] mascook; mascook = cookie.Split(';'); Awesomium.Core.WebCore.ClearCookies(); foreach (string cook in mascook) Awesomium.Core.WebCore.SetCookie("http://" + domen, cook + "; domain=" + domen, true, true); 


Getting the values ​​of the variables mx and my
  Awesomium.Core.JSValue x = webControl1.ExecuteJavascriptWithResult("mx", 500); Awesomium.Core.JSValue y = webControl1.ExecuteJavascriptWithResult("my", 500); 

500 is a timeout.

JS function call

 webControl1.CallJavascriptFunction("", "al", new Awesomium.Core.JSValue[] {}); 


al is the name of the function
the second parameter passes the values ​​to the function. In this case, it is empty.

The above examples work on Awesomium 1.6.5, in version 1.7 the architecture has been slightly modified and some methods may be missing or called differently.

findings
At the moment, in all projects where a browser is needed, I use Awesomium: it works more stable and has richer functionality. The load from it is small. The only drawback is that it does not work out right click on the flash, so you can’t change the necessary parameters (maybe I just didn’t figure out how). Otherwise, this SDK has arranged everything for me.

Source: https://habr.com/ru/post/170015/


All Articles