To override these Javascript Functions, you need to make sure they are going into the right page.
You may be seeing a staged event in the page loading. This means that once the page is loaded, the scripts may not be loaded but are loading still. Or possibly the absolute URL is not the URL where the scripts are being loaded from.
Try this first:
public void navigateURL(string URL)
{
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(page_Loaded);
webBrowser1.Navigate(URL);
}
void page_Loaded(object sender, WebBrowserDocumentCompletedEventArgs e)
{
MessageBox.Show(e.Url.AbsolutePath)
}
This will tell you if the URL you are trying to inject the code into is the correct one. I have seen on some pages, e.Url.AbsolutePath can change several times in between the loading of the final page. Record the URL's displayed in the MessageBox in the following URL Strings.
private string URL1 = "<First URL to load>";
private string URL2 = "<Second URL to load>";
private string URL3 = "<Third URL to load>";
void page_Loaded(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser1.Url.ToString() == "URL3")
{
// Block POP Ups...
HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
string alertBlocker = "window.alert = function () { }";
element.text = alertBlocker;
head.AppendChild(scriptEl);
this.webBrowser1.PerformLayout();
webBrowser1.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(Page_Loaded);
}
}
I have put URL1 - 3 in there so as to indicate the possible URL's you need to work with.
In some situations I have also needed to implement a Timer to wait for objects to load:
void page_Loaded(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// Wait for Objects to Load...
delaytimer.Interval = 500;
delaytimer.Enabled = true;
delaytimer.Tick += new EventHandler(PageObjects_Loaded);
delaytimer.Start();
}
void PageObjects_Loaded(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// Do your stuff Here...
}
Check all the other simple things also, make sure the page has a head tag, there may already be a code injection going on there, and your code is injected in the head tag.
Hope this helps.
[EDIT]
IHTMLScriptElement is in the mshtml library so you will need a reference to:
"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Common"
or
"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Visual Studio Tools for Office\PIA\Common"
and add: using mshtml;
I see you have this done already, so this is for others.