Visual Studio
Provides the browser control webbrowser, which can be used in winform to act as a small browser.
Webbrowser provides the method to call JS functions on the page, but does not directly provide the function to add and execute new JS scripts. If the following JS functions are available:
1
<Script
Type = "text/JavaScript"
>
2
Function
Add (num1, num2 ){
3
Return
Num1
+
Num2;
4
}
5
</
Script
>
Then, you can call the Add function using the following code:
1 // browser is the webbrowser control on the window.
2 This. browser. Document. invokescript ("add", new object [] {1, 2 });
Iebrowser
The webbrowser page allows you to add and execute new JS scripts.
Iebrowser is an open-source shared C # Code. For example, refer:
Http://code.google.com/p/zsharedcode/wiki/IEBrowserDoc
, Download and use:
Http://code.google.com/p/zsharedcode/wiki/Download
.
The following example adds and calls the new JS function showmessage:
1 // create an iebrowser object from the current webbrowser control. The URL attribute of webbrowser has been set to "about: blank ".
2 iebrowser Ie = new iebrowser (this. webbrowser );
3
4 // define a Javascript script and declare a showmessage function.
5 string showmessagescript = "function showmessage (Message) {alert ('message: '+ message );}";
6 // install the script in webbrowser.
7 ie. installscript (showmessagescript );
8
9 // execute the script and call the showmessage function.
10 ie. executescript ("showmessage ('haha! '); "); Use the installscript method of iebrowser to add JS scripts, while executescript can execute JS scripts. in addition to passing the string containing the script, installscript can also be the script address.
You
You can also install jquery for webbrowser and execute a series of jquery scripts.
Iebrowser provides a jquery class that simplifies the writing of jquery scripts.
The following example demonstrates how to install jquery on the Google Page and use jquery to obtain all links on the page.
1 // create an iebrowser object from the current webbrowser control.
2 iebrowser Ie = new iebrowser (this. webbrowser );
3
4 // navigate to the page http://www.google.com.hk /.
5 ie. navigate ("http://www.google.com.hk /");
6
7 // wait until the page is loaded.
8 ie. ieflow. Wait (New urlcondition ("wait", "http://www.google.com.hk", stringcomparemode. startwith ));
9
10 // install the tracking script. It is required to execute jquery.
11 ie. installtrace ();
12
13 // install the local jquery script.
14 ie. installjquery (New uri (path. Combine (appdomain. currentdomain. basedirectory, @ "jquery-1.5.min.js ")));
15
16 // run the jquery script $ ('*'). length to obtain the total number of elements on the page.
17 console. writeline ("there are {0} elements on the page", ie. executejquery (jquery. Create ("'*'"). Length ()));
18
19 // execute the jquery script $ ('A') to obtain all the elements on the page and save the result in the _ JAS variable.
20 ie. executejquery (jquery. Create ("'A'"), "_ JAS ");
21
22 // obtain the number of A elements contained in the _ JAS variable.
23 int COUNT = ie. executejquery <int> (jquery. Create ("_ JAS"). Length ());
24
25 For (INT Index = 0; index <count; index ++)
26 {
27 // obtain the element of index in the _ JAS variable and save it in the _ ja variable.
28 ie. executejquery (jquery. Create ("_ JAS"). eq (index. tostring (), "_ ja ");
29
30 // output the innertext and href attributes of Element.
31 console. writeline (string. Format (
32 "A [{0}], '{1}', '{2 }'",
33 index,
34 ie. executejquery <string> (jquery. Create ("_ ja"). Text ()),
35 ie. executejquery <string> (jquery. Create ("_ ja"). ATTR ("'href '"))
36)
37 );
38}
Call installtrace and installjquery of iebrowser to install the jquery script. The script can be located locally or on the network. then, you can use the executejquery method and the jquery class to complete various jquery operations on the page. the jquery class is named based on jquery's Js script, which is easy to understand.
Note:
If an error occurs due to the jquery script encoding format, you can import the jquery script as a resource to the project and install it using the installscript method.
Division
With the above features, iebrowser can also copy images so that JS can call managed code, record user operations, and complete complicated process control. This will be explained later.