简体   繁体   English

如何在C#WebBrowser中插入文件js并使用它?

[英]how can insert file js in C# WebBrowser and use it?

Is it possible to inject javascript into the .NET WebBrowser Windows Forms control? 是否可以将javascript注入.NET WebBrowser Windows Forms控件中? If so, how would I go about doing this? 如果是这样,我将如何去做?

You can execute any javascript you want from the WebBrowser control. 您可以从WebBrowser控件中执行所需的任何javascript。 See an example here . 在这里查看示例。

I'd recommend doing something like this in javascript to load an external js file: 我建议在javascript中执行以下操作以加载外部js文件:

var element = document.createElement('script');
element.setAttribute('type', 'text/javascript');
element.setAttribute('src', 'http://code.jquery.com/jquery-1.6.2.min.js');
document.getElementsByTagName('head')[0].appendChild(element);

I'm loading JQuery specifically here. 我在这里专门加载JQuery。

I believe you can just call yourBrowser.Document.InvokeScript() and pass a huge JS string in to do whatever you want. 我相信您可以只调用yourBrowser.Document.InvokeScript()并传递一个巨大的JS字符串来执行您想要的任何操作。

UPDATE: 更新:

A comment pointed out you can only call existing JS functions with InvokeScript. 指出您只能使用InvokeScript调用现有JS函数的注释。 Turns out you can do something similar in C#. 事实证明,您可以在C#中执行类似的操作。

See the top answer on this SO link. 请参阅 SO链接上的最佳答案。

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

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