我如何可以覆盖使用WPF web浏览器在网页上的btnSubmit_onclick javascript函数? [英] How can I overwrite the btnSubmit_onclick javascript function on a web page using the WPF WebBrowser?
问题描述
我的WPF web浏览器
控制是从的WinForms版本,并从这个优秀的网站根本就没有在WPF工作的许多建议不同的工作。
I'm working with the WPF WebBrowser
control which is different from the WinForms version and many recommendations from this excellent web site simply do not work in WPF.
我怎么能覆盖 btnSubmit_onclick()
网页上的javascript函数?
How can I overwrite the btnSubmit_onclick()
javascript function on a web page?
该函数生成一个确认对话框(如你确定吗?),我想绕过。基本上我想注入新的功能 btnSubmit_onclick()
将取代现有的,我可以省略恼人的确认对话框。我曾访问MSHTML和所有页面对象,但挣扎在页面的页眉注入脚本。到目前为止,我有:
This function generates a confirm dialog (like "Are you sure?") which I want to bypass. Basically I want to inject a new function btnSubmit_onclick()
which will replace the existing one and I can omit annoying confirm dialog. I have access to MSHTML and all page objects, but struggle to inject script at the header of the page. So far I've got:
mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)webBrowser.Document;
mshtml.IHTMLElementCollection heads = doc.all.tags("head") as mshtml.IHTMLElementCollection;
mshtml.IHTMLElement head = heads.item(null, 0) as mshtml.IHTMLElement;
mshtml.IHTMLElement se = doc.createElement("script") as mshtml.IHTMLElement;
se.innerHTML = "function btnSubmit_onclick() { alert('here we are'); }";
但现在我需要先于其他其他脚本注入新的脚本元素插入头部的对象,我希望它会owerwrite现有的 btnSubmit_onclick()
函数下游。
我试过失败而head.injectAdjusentHTML拒绝注入任何脚本到页眉即使我使用DEFER属性。
I unsuccessfully tried head.injectAdjusentHTML which refuses to inject any script into the header even I use DEFER property.
我该怎么办呢?
推荐答案
我还没有测试这一点,只是一个猜测:
试试这个:
I havent tested this, is just a guess: Try this:
// For this code to work: add the Microsoft.mshtml .NET reference
mshtml.IHTMLDocument2 doc = WebBrowser1.Document as mshtml.IHTMLDocument2;
doc.parentWindow.execScript("document.body.onsubmit=new function(){ alert('here we are'); };");
这篇关于我如何可以覆盖使用WPF web浏览器在网页上的btnSubmit_onclick javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!