如何检测 WebBrowser 控件中的 Javascript 执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9567715/
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
How to detect Javascript execution in WebBrowser control
提问by Gabber
I have a WebBrowser
control in my C# application. The web browser is under the user's control, that is, he can load any web page his computer can access on the web (of course limited by proxy, hosts file and so on).
WebBrowser
我的 C# 应用程序中有一个控件。网络浏览器在用户的控制之下,也就是说,他可以加载他的电脑在网络上可以访问的任何网页(当然受代理、主机文件等的限制)。
I need to know and to be notified when there is a Javascript call inside the page loaded in the web browser component.
当 Web 浏览器组件中加载的页面内有 Javascript 调用时,我需要知道并收到通知。
First example: given a link like this
第一个例子:给出这样的链接
<a href="javascript:void(0)" onclick="jsFunct();">test</a>
When the user clicks the link I need to know that the function "jsFunct" has been called.
当用户单击链接时,我需要知道函数“jsFunct”已被调用。
Second example: given a call like
第二个例子:给定一个电话
<script type="text/javascript">
window.setTimeout("jsFunct()", 1000);
</script>
I need to know that, 1 second after the execution of the script, the function jsFunct has been called.
我需要知道,脚本执行后 1 秒,函数 jsFunct 已被调用。
The best thing would be to have an event fired when the function is called. It would also be great if the event could get the Javascript code executed, or at least the function name in the arguments.
最好的办法是在调用函数时触发一个事件。如果事件可以执行 Javascript 代码,或者至少是参数中的函数名称,那就太好了。
EDIT:
编辑:
Even if the question is related to the webbrowser component, anything that allows the user to detect javascript activation (even via js) would be fine, being able to inject a js that handles the javascript event and passes it to the wb control triggering some event that it can handle.
即使问题与 webbrowser 组件有关,任何允许用户检测 javascript 激活(甚至通过 js)的东西都可以,能够注入处理 javascript 事件的 js 并将其传递给触发某些事件的 wb 控件它可以处理。
采纳答案by Jeff
You can use window.external
to call a C# method when a global function is fired in JavaScript. See WebBrowser Control Overviewfor details on window.external.
window.external
当在 JavaScript 中触发全局函数时,您可以使用它来调用 C# 方法。有关window.external 的详细信息,请参阅WebBrowser 控件概述。
You'll need to set ObjectForScripting: Webbrowser control's window.external is ALWAYS null.for this to work.
您需要设置 ObjectForScripting: Webbrowser 控件的 window.external 始终为空。为了这个工作。
Take @Krishna's answer to add the JavaScript (but drop jQuery because it won't be needed):
以@Krishna 的回答来添加 JavaScript(但删除 jQuery,因为它不需要):
private void addScript(HtmlElement head, string scriptSource)
{
HtmlElement lhe_script = head.Document.CreateElement("script");
IHTMLScriptElement script = (IHTMLScriptElement)lhe_script.DomElement;
script.src = scriptSource;
head.AppendChild(lhe_script);
}
addScript(WebBrowser.Head, @"InjectMonitor.js");
The JavaScript below (InjectMonitor.js) will find all global functions and attach your specified handler:
下面的 JavaScript (InjectMonitor.js) 将找到所有全局函数并附加您指定的处理程序:
function augment(withFn) {
var name, fn;
for (name in window) {
fn = window[name];
if (typeof fn === 'function') {
window[name] = (function(name, fn) {
var args = arguments;
return function() {
withFn.apply(this, args);
fn.apply(this, arguments);
};
})(name, fn);
}
}
}
augment(function(name, fn) {
console.log("calling " + name, fn);
// window.external.yourC#method
});
In this example, taken from Adding Console Log to Every Function, it just logs the call to console; but using window.external you could send some message back to your C# application with details of what function was called from the client.
在这个例子中,取自将控制台日志添加到每个函数,它只记录对控制台的调用;但是使用 window.external 您可以将一些消息发送回您的 C# 应用程序,其中包含从客户端调用的函数的详细信息。
Finally, here's a JS Bin example (run it and don't forget the console): JS Bin Example
最后,这是一个 JS Bin 示例(运行它并不要忘记控制台): JS Bin 示例
回答by Krishna
On the webbrowser load event,
在浏览器加载事件中,
- Inject Jquery
- Inject Monitor scripts
- 注入 Jquery
- 注入监控脚本
,
,
private void addScript(HtmlElement head, string scriptSource)
{
HtmlElement lhe_script = head.Document.CreateElement("script");
IHTMLScriptElement script = (IHTMLScriptElement)lhe_script.DomElement;
script.src = scriptSource;
head.AppendChild(lhe_script);
}
addScript(Webbrowser.Head, @"<Change File Path here>jquery.min.js");
addScript(WebBrowser.Head, @"InjectMonitor.js");
your file InjectMonitor.js should be something like this
你的文件 InjectMonitor.js 应该是这样的
$(document).ready(function () {
//Add click event for every anchor on the page loaded- note this merely alerts text on click. you can however add your own function
$("a").click(function (e) { alert($(this).text()); return false;})
});
回答by Chandra Sekhar Walajapet
Well what krishna has answered is interms of pure javascript attaching to events, however i see that you might need to attach it to all the tags(a,p,div,input) etc and to all the events attached to each tag.
好吧,克里希纳回答的是将纯 javascript 附加到事件,但是我看到您可能需要将它附加到所有标签(a、p、div、输入)等以及附加到每个标签的所有事件。
i believe the another way is to play around with the BHO(browser helper object) available to your in .net, and if not and you are good at VC++ and MFC you can also play around with Windows Hooks.
我相信另一种方法是使用 .net 中可用的 BHO(浏览器助手对象),如果没有,并且您擅长 VC++ 和 MFC,您也可以使用 Windows Hooks。