如何使用 WPF WebBrowser 覆盖网页上的 btnSubmit_onclick javascript 函数?

发布于 2024-07-14 02:32:02 字数 924 浏览 12 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┼── 2024-07-21 02:32:02

我认为你需要将它添加到头部的末端,而不是在其他之前注入它。 我相信 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.

醉城メ夜风 2024-07-21 02:32:02

我没有测试过,这只是一个猜测:
尝试这个:

// 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'); };");

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'); };");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文