使用 WebBrowser.Document.InvokeScript 调用 javascript 对象方法
在我的 WinForms 应用程序中,我需要从 WebBrowser 控件调用 javascript 函数。我使用了 Document.InvokeScript 并且它可以单独使用函数完美地工作,例如
Document.InvokeScript("function").
但是当我想调用 javascript 对象方法时,例如
Document.InvokeScript("obj.method")
它不起作用。有办法让它发挥作用吗?或者这个问题的不同解决方案? 无需更改 javascript 代码中的任何内容!
提前致谢 :)
In my WinForms application I need to call javascript function from my WebBrowser control. I used Document.InvokeScript and it works perfect with functions alone e.g
Document.InvokeScript("function").
But when i want to call javascript object method e.g.
Document.InvokeScript("obj.method")
it doesn't work. Is there a way to make it work? Or different solution to this problem? Without changing anything in the javascript code!
Thanks in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
文档中的示例不包含括号。
请
注意,如果使用 HtmlDocument.InvokeScript 方法 (String, Object[ ])。
编辑
看起来您不是唯一一个遇到此问题的人: HtmlDocument.InvokeScript - Calling a method of an对象。您可以像该链接的海报所建议的那样创建“代理功能”。基本上你有一个调用对象函数的函数。这不是一个理想的解决方案,但它肯定会起作用。我将继续寻找这是否可能。
关于同一问题的另一篇文章:使用 WebBrowser。 Document.InvokeScript() 与外部 JavaScript 混在一起 。 C. Groß 在 CodeProject 上提出的有趣解决方案:
您可以在 HtmlDocument 上创建一个扩展方法,并调用它来运行您的函数,仅使用这个新函数,您将在您传入的字符串中包含括号、参数和整个九码(因为它只是传递给 eval)。
看起来 HtmlDocument 不支持调用现有对象的方法。仅全局函数。 :(
The example in the documentation does NOT include the parenthesis.
Try
Note that you can pass arguments if you use HtmlDocument.InvokeScript Method (String, Object[]).
Edit
Looks like you aren't the only one with this issue: HtmlDocument.InvokeScript - Calling a method of an object . You can make a "Proxy function" like the poster of that link suggests. Basically you have a function that invokes your object's function. It's not an ideal solution, but it'll definitely work. I'll continue looking to see if this is possible.
Another post on same issue: Using WebBrowser.Document.InvokeScript() to mess around with foreign JavaScript . Interesting solution proposed by C. Groß on CodeProject:
You could make that an extension method on HtmlDocument and call that to run your function, only using this new function you WOULD include parenthesis, arguments, the whole nine yards in the string you pass in (since it is just passed along to an eval).
Looks like HtmlDocument does not have support for calling methods on existing objects. Only global functions. :(
不幸的是,您无法使用 WebBrowser.Document.InvokeScript 调用现成的对象方法。
解决方案是在 JavaScript 端提供一个全局函数,可以重定向您的调用。在最简单的形式中,这看起来像:
在您的 .NET 代码中,您将按如下方式使用它:
正如您提到的,您无法对现有 JavaScript 代码进行任何更改,您必须以某种方式注入上述 JavaScript 方法。幸运的是,WebBrowser 控件还可以通过调用 eval() 方法来为您做事:
有关更强大和完整的实现,请参阅 WebBrowser 我编写的工具和解释 ScriptingBridge 专门旨在解决您所描述的问题。
Unfortunately you can't call object methods out of the box using WebBrowser.Document.InvokeScript.
The solution is to provide a global function on the JavaScript side which can redirect your call. In the most simplistic form this would look like:
In your .NET code you would use this as follows:
As you mention that you cannot change anything to your existing JavaScript code, you'll have to inject the above JavaScript method in some how. Fortunately the WebBrowser control can also do for you by calling the eval() method:
For a more robust and complete implementation see the WebBrowser tools I wrote and the article explaining the ScriptingBridge which specifically aims to solve the problem you describe.
因为你应该是这样的
for you supposed to be like this