简体   繁体   English

如何通过WebBrowser控件为WinForms处理Javascript事件

[英]How to handle Javascript events via WebBrowser control for WinForms

I have read WebBrowser Control from .Net — How to Inject Javascript , Is it possible to call Javascript method from C# winforms and many others. 从.Net阅读了WebBrowser控件 - 如何注入Javascript是否可以从C#winforms和其他许多人调用Javascript方法 Those examples were returns function value or alert window (synchronous calls). 这些示例是返回函数值或警报窗口(同步调用)。 I have to get result from event handler (async call): 我必须从事件处理程序(异步调用)获取结果:

<script type="text/javascript">
        window.onload = function() {
            var o = new M.Build(document.getElementById("ZID"));

            M.Events.observe(o, o.Events.Success, function() {
                // I have to get some value!!
            });

            M.Events.observe(o, o.Events.Fault, function() {
                // I have to get some value!!
            });
        }
    </script>

Calling C# from JavaScript 从JavaScript调用C#

Simply put, you can expose a C# object to the WebBrowser that the JavaScript can call directly The WebBrowser class exposes a property called ObjectForScripting that can be set by your application and becomes the window.external object within JavaScript. 简单地说,您可以将C#对象公开给JavaScript可以直接调用的WebBrowser WebBrowser类公开一个名为ObjectForScripting的属性,该属性可以由您的应用程序设置并成为JavaScript中的window.external对象。 The object must have the ComVisibleAttribute set true 该对象必须将ComVisibleAttribute设置为true

C#: C#:

 [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public class ScriptInterface
    {
        public void callMe()
        {
            … // Do something interesting
        }
    }

    webBrowser1.ObjectForScripting = new ScriptInterface();

Javascript: 使用Javascript:

window.external.callMe();

Calling JavaScript in a WebBrowser control from C# 从C#调用WebBrowser控件中的JavaScript

This is code I have. 这是我的代码。 In the DocumentCompleted event ('cause I'm getting a page from online) 在DocumentCompleted事件中(因为我从在线获取页面)

var wb = (WebBrowser)sender
//Lots of other stuff
object obj = wb.Document.InvokeScript("MyFunctionName");

Create a function that returns whatever value you need and invoke away. 创建一个函数,返回您需要的任何值并调用。

You can also inject a script into the page 您还可以将脚本注入页面

string js = "function MyFunctionName(){alert('Yea!');}";
HtmlElement el = wb.Document.CreateElement("script");
IHTMLScriptElement element2 = (IHTMLScriptElement)el.DomElement;
element2.text = js;
head.AppendChild(el);

which can then be invoked. 然后可以调用它。 That's what I've done. 这就是我所做的。

If your webBrowser control is in a form, you can do the following: 如果您的webBrowser控件位于表单中,则可以执行以下操作:

[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class Form1
{

    public Form1()
    {
       InitializeComponent();
       webBrowser1.ObjectForScripting = this;
    }

    public void CallMe()
    {
        //.... this method can be called in javascript via window.external.CallMe();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM