【问题标题】:Calling javascript object method using WebBrowser.Document.InvokeScript使用 WebBrowser.Document.InvokeScript 调用 javascript 对象方法
【发布时间】:2011-11-11 10:52:30
【问题描述】:

在我的 WinForms 应用程序中,我需要从我的 WebBrowser 控件调用 javascript 函数。我使用了 Document.InvokeScript,它可以完美地与单独的函数配合使用,例如

Document.InvokeScript("function").

但是当我想调用 javascript 对象方法时,例如

Document.InvokeScript("obj.method")

它不起作用。有没有办法让它工作?或者这个问题的不同解决方案? 无需更改 javascript 代码中的任何内容!

提前致谢:)

【问题讨论】:

  • 可以说我的 javascrip 中有类似的东西:obj = {method : function() {alert("...)} 我想从我的 webbrowser 控件中调用 obj.method()

标签: c# javascript .net winforms browser


【解决方案1】:

documentation 中的示例不包含括号。

private void InvokeScript()
{
    if (webBrowser1.Document != null)
    {
        HtmlDocument doc = webBrowser1.Document;
        String str = doc.InvokeScript("test").ToString() ;
        Object jscriptObj = doc.InvokeScript("testJScriptObject");
        Object domOb = doc.InvokeScript("testElement");
    }
}

试试

Document.InvokeMethod("obj.method");

请注意,如果您使用HtmlDocument.InvokeScript Method (String, Object[]),则可以传递参数。

编辑

看来您不是唯一遇到此问题的人:HtmlDocument.InvokeScript - Calling a method of an object。您可以像该链接的海报所建议的那样制作“代理功能”。基本上,您有一个调用对象函数的函数。这不是一个理想的解决方案,但它肯定会奏效。我会继续看看这是否可行。

关于同一问题的另一篇文章:Using WebBrowser.Document.InvokeScript() to mess around with foreign JavaScript。 C. Groß 在 CodeProject 上提出的有趣解决方案:

private string sendJS(string JScript) {
    object[] args = {JScript};
    return webBrowser1.Document.InvokeScript("eval",args).ToString();
}

您可以在 HtmlDocument 上创建一个扩展方法并调用它来运行您的函数,仅使用这个新函数,您将在传入的字符串中包含括号、参数、整个九码(因为它只是传递给一个评估)。

看起来 HtmlDocument 不支持对现有对象调用方法。只有全局函数。 :(

【讨论】:

  • 哦,抱歉,这是我写这篇文章时的错误。在我的代码中,我从未使用过括号(我已经编辑了帖子)。有什么想法现在可能出了什么问题?
  • 第二种解决方案效果很好!谢谢,因为在您链接的帖子中,我主要关心的是不要更改 javascript 代码。所以再次感谢它是我正在寻找的:)
  • 第二个解决方案非常棒,节省了我几个小时。
【解决方案2】:

很遗憾,您不能使用 WebBrowser.Document.InvokeScript 直接调用对象方法。

解决方案是在 JavaScript 端提供一个全局函数,可以重定向您的调用。最简单的形式如下:

function invoke(method, args) {

    // The root context is assumed to be the window object. The last part of the method parameter is the actual function name.
    var context = window;
    var namespace = method.split('.');
    var func = namespace.pop();

    // Resolve the context
    for (var i = 0; i < namespace.length; i++) {
        context = context[namespace[i]];
    }

    // Invoke the target function.
    result = context[func].apply(context, args);
}

在您的 .NET 代码中,您可以按如下方式使用它:

var parameters = new object[] { "obj.method", yourArgument };
var resultJson = WebBrowser.Document.InvokeScript("invoke", parameters);

正如您提到的,您无法对现有 JavaScript 代码进行任何更改,因此您必须以某种方式注入上述 JavaScript 方法。幸运的是,WebBrowser 控件也可以通过调用 eval() 方法为您完成:

WebBrowser.Document.InvokeScript("eval", javaScriptString);

有关更健壮和完整的实现,请参阅我编写的 WebBrowser 工具和解释 ScriptingBridge 的文章,该文章专门用于解决您描述的问题。

【讨论】:

    【解决方案3】:
     webBrowser.Document.InvokeScript("execScript", new object[] { "this.alert(123)", "JavaScript" })
    

    你应该是这样的

     webBrowser.Document.InvokeScript("execScript", new object[] { "obj.method()", "JavaScript" })
    

    【讨论】:

      猜你喜欢
      • 2013-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-10
      • 2011-10-22
      • 1970-01-01
      相关资源
      最近更新 更多