zoukankan      html  css  js  c++  java
  • 使 WebBrowser 更简单的新加和执行 js, 可安装 jQuery 脚本的 C# 开源代码 IEBrowser [1]

    Visual Studio 中提供了浏览器控件 WebBrowser, 可以用在 WinForm 中充当一个小型的浏览器.

    WebBrowser 本身提供了调用页面中 js 函数的方法, 但没有直接提供可以添加执行新的 js 脚本的功能. 如果有如下的 js 函数:

    1 <script type="text/javascript">
    2 function add(num1, num2) {
    3 return num1 + num2;
    4 }
    5  </script>


    则, 通过如下代码即可调用 add 函数:

    1 // browser 为窗口上的 WebBrowser 控件.
    2  this.browser.Document.InvokeScript ( "add", new object[] { 1, 2 } );
     

    IEBrowser
    提供在 WebBrowser 的页面中添加执行新 js 脚本的功能.

    IEBrowser 是开源共享的 C# 代码, 示例参考: http://code.google.com/p/zsharedcode/wiki/IEBrowserDoc , 下载使用: http://code.google.com/p/zsharedcode/wiki/Download .

    下面的例子是增加并调用新的 js 函数 showMessage:

    1 // 从当前的 WebBrowser 控件创建 IEBrowser 对象, WebBrowser 的 Url 属性已经设置为 "about:blank".
    2  IEBrowser ie = new IEBrowser ( this.webBrowser );
    3
    4  // 定义 javascript 脚本, 声明一个 showMessage 函数.
    5  string showMessageScript = "function showMessage(message){alert('消息:' + message);}";
    6  // 将脚本安装到 WebBrowser 中.
    7  ie.InstallScript ( showMessageScript );
    8
    9  // 执行脚本, 调用 showMessage 函数.
    10  ie.ExecuteScript ( "showMessage('哈哈!');" );

    使用 IEBrowser 的 InstallScript 方法即可完成添加 js 脚本的功能, 而 ExecuteScript 则可以执行 js 脚本. InstallScript 除了可以直接传递包含脚本的字符串外, 也可以是脚本的地址.

    还可以为 WebBrowser 安装 jQuery, 并执行一系列的 jQuery 脚本.

    IEBrowser 提供了一个 JQuery 类, 简化了 jQuery 脚本的书写.

    下面的示例, 演示了在 Google 页面安装 jQuery, 并使用 jQuery 获取页面上所有的链接.

    1 // 从当前的 WebBrowser 控件创建 IEBrowser 对象.
    2  IEBrowser ie = new IEBrowser ( this.webBrowser );
    3
    4  // 导航到页面 http://www.google.com.hk/.
    5  ie.Navigate ( "http://www.google.com.hk/" );
    6
    7  // 等待页面载入完毕.
    8  ie.IEFlow.Wait ( new UrlCondition ( "wait", "http://www.google.com.hk", StringCompareMode.StartWith ) );
    9
    10  // 安装跟踪脚本, 执行 jquery 必需.
    11  ie.InstallTrace ( );
    12
    13  // 安装本地的 jquery 脚本.
    14  ie.InstallJQuery ( new Uri ( Path.Combine ( AppDomain.CurrentDomain.BaseDirectory, @"jquery-1.5.min.js" ) ) );
    15
    16  // 执行 jquery 脚本 $('*').length, 获得页面上总元素个数.
    17  Console.WriteLine ( "页面上共有 {0} 个元素", ie.ExecuteJQuery ( JQuery.Create ( "'*'" ).Length ( ) ) );
    18
    19  // 执行 jquery 脚本 $('a'), 获得页面上所有的 a 元素并将结果保存在 __jAs 变量中.
    20  ie.ExecuteJQuery ( JQuery.Create ( "'a'" ), "__jAs" );
    21
    22  // 得到 __jAs 变量中包含的 a 元素的个数.
    23  int count = ie.ExecuteJQuery<int> ( JQuery.Create ( "__jAs" ).Length ( ) );
    24
    25  for ( int index = 0; index < count; index++ )
    26 {
    27 // 得到 __jAs 变量中索引为 index 的 a 元素, 并保存在 __jA 变量中.
    28   ie.ExecuteJQuery ( JQuery.Create ( "__jAs" ).Eq ( index.ToString ( ) ), "__jA" );
    29
    30 // 输出 a 元素的 innerText 和 href 属性.
    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 }

    调用 IEBrowser 的 InstallTrace 和 InstallJQuery 即可安装 jQuery 脚本, 脚本的位置可以在本地或者网络. 之后, 可以配合 ExecuteJQuery 方法和 JQuery 类在页面上完成各种 jQuery 操作. JQuery 类参照 jQuery 的 js 脚本命名, 很容易掌握.

    注意: 如果出现 jQuery 脚本编码格式导致的出错, 可以将 jQuery 脚本作为资源导入项目, 然后使用 InstallScript 方法安装即可.

    了以上功能, IEBrowser 还可以复制图片, 使 js 调用托管代码, 以及记录用户操作和完成复杂的流程控制等, 之后文章会说明.
  • 相关阅读:
    网上找的面试题-之一
    python里的Join函数
    【转载51CTO】Linux中引号那些事儿
    【面试编程题】巧妙排序:排序只有1,2,3三个元素的数组,不能统计1,2,3的个数。
    [转载]mininet的安装和使用
    Open vSwitch源码阅读【转】及自己的理解【稍后更新】
    7、8月份安排 进度条
    请不要忽视基础小细节
    【编程之美】2.20 程序理解问题
    GDOI2017爆炸记
  • 原文地址:https://www.cnblogs.com/zoyobar/p/IEBrowser_1.html
Copyright © 2011-2022 走看看