如何使用 WPF WebBrowser 覆盖网页上的 btnSubmit_onclick javascript 函数?
我正在使用 WPF WebBrowser
控件,该控件与 WinForms 版本不同,并且这个优秀网站的许多建议根本不适用于 WPF。
如何覆盖网页上的 btnSubmit_onclick()
javascript 函数?
此函数会生成一个确认对话框(例如“您确定吗?”),我想绕过该对话框。 基本上我想注入一个新函数 btnSubmit_onclick()
它将替换现有的函数,并且我可以省略烦人的确认对话框。 我可以访问 MSHTML 和所有页面对象,但很难在页面标题处注入脚本。 到目前为止,我已经得到了:
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'); }";
但现在我需要在其他脚本之前将这个新的脚本元素注入到 head 对象中,并且我希望它将写入下游现有的 btnSubmit_onclick()
函数。
我没有成功尝试 head.injectAdjusentHTML,即使我使用 DEFER 属性,它也拒绝将任何脚本注入标头。
我该怎么做?
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.
How can I overwrite the btnSubmit_onclick()
javascript function on a web page?
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'); }";
But now I need to inject this new script element into head object before other the other scripts, and I hope it will owerwrite existing btnSubmit_onclick()
function downstream.
I unsuccessfully tried head.injectAdjusentHTML which refuses to inject any script into the header even I use DEFER property.
How can i do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你需要将它添加到头部的末端,而不是在其他之前注入它。 我相信 javascript 是一种最后获胜的交易类型。
I think you need to add it to the end of the head instead of inject it before the others. I believe javascript is a last-one-wins type of deal.
我没有测试过,这只是一个猜测:
尝试这个:
I havent tested this, is just a guess:
Try this: