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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5167522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:54:16  来源:igfitidea点击:

How to handle Javascript events via WebBrowser control for WinForms

javascriptwinformswebbrowser-controldom-events

提问by garik

I have read WebBrowser Control from .Net — How to Inject Javascript, Is it possible to call Javascript method from C# winformsand many others. Those examples were returns function value or alert window (synchronous calls). I have to get result from event handler (async call):

我已经阅读了 .Net 中的 WebBrowser Control - How to Inject JavascriptIs it possible to call Javascript method from C# winformsand many others。这些示例是返回函数值或警报窗口(同步调用)。我必须从事件处理程序(异步调用)中获取结果:

<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>

回答by garik

Calling C# from JavaScript

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. The object must have the ComVisibleAttribute set true

从 JavaScript 调用 C#

简单地说,您可以将 C# 对象公开给 JavaScript 可以直接调用的 WebBrowser WebBrowser 类公开一个名为 ObjectForScripting 的属性,该属性可由您的应用程序设置,并成为 JavaScript 中的 window.external 对象。该对象必须将 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

回答by QuinnG

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.

然后可以调用。这就是我所做的。

回答by Ahmad

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();
    }
}