Kidades
Kidades

Reputation: 670

How to inject javascript into an iframe with webbrowser control?

I am trying to block certain javascript functions which show alerts to the user.

I am currently using this code, but it is not working:

    private void InjectAlertBlocker()
    {            
        string alertBlocker = @"window.alert = function () { }; 
                        window.print = function () { }; 
                        window.open = function () { }; 
                        window.onunload = function () { }; 
                        window.prompt = function () { };
                        window.confirm = function() { };
                        window.onbeforeunload = function () { };";

        wb.Document.InvokeScript("execScript", new Object[] { alertBlocker, "JavaScript" });
    }

The webbrowser control runs in the background and some sites that need to be visited are showing up alerts which randomly pop up to the user.

I have tried similar codes but could not get any of them to work.

UPDATE:

After some checking, it seems that this code does block alerts. However, in my particular case, the alert is coming from an iframe, and that's why my code is not blocking it. The javascript alert example on w3schools is a perfect example for this: http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert

How can I override the scripts inside an iframe?

UPDATE 2:

I have tried to inject javascript code (provided by Buzinas in his answer) into the main page, which will iterate through all the iframes and override their alert functions, but it is causing errors most of the time due to the same origin policy.

Upvotes: 2

Views: 951

Answers (1)

Buzinas
Buzinas

Reputation: 11725

Instead of invoking a script, you can append it as a <script> element:

HtmlElement head = wb.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = wb.Document.CreateElement("script");
IHTMLScriptElement el = (IHTMLScriptElement)scriptEl.DomElement;
el.text =  @"window.alert = function () { }; 
             window.print = function () { }; 
             window.open = function () { }; 
             window.onunload = function () { }; 
             window.onbeforeunload = function () { };";
head.AppendChild(scriptEl);

Update

To ensure that all the iframes are blocked, you can do something like this:

HtmlElement head = wb.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = wb.Document.CreateElement("script");
IHTMLScriptElement el = (IHTMLScriptElement)scriptEl.DomElement;
el.text =  @"window.alert = function () { }; 
             window.print = function () { }; 
             window.open = function () { }; 
             window.onunload = function () { }; 
             window.onbeforeunload = function () { };

             var iframes = document.getElementsByTagName('iframe');
             for (var i = 0; i < iframes.length; i++) {
               var win = iframes[i].contentWindow;
               win.alert = function () { }; 
               win.print = function () { }; 
               win.open = function () { }; 
               win.onunload = function () { }; 
               win.onbeforeunload = function () { };
             }";
head.AppendChild(scriptEl);

Upvotes: 1

Related Questions