views:

999

answers:

1

Hi,

I work with WPF WebBrowser control which different from WinForms and many recommendations from this excellent web site simply does not work in WPF.

I try to find solution how I can overwrite particular javascript function btnSubmit_onclick() on a web page. This function generates confirm dialog (like "Are you sure?") which I want to bypass. Basically I want to inject new function btnSubmit_onclick() () which will replace 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 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?

A: 

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.

justin.m.chase