Read iFrame contents in a WPF browser window c#

1.3k views Asked by At

I have an HTML document with an iFrame, which i am loading in a webbrowser window. My objective is once that iFrame document is loaded, capture the document contents and search for a particular line.

I am not been able to do it so far, any suggestions ?

HTML CODE

<html>
  <body>
    <iframe id="monitor" src="http://monitor.baseline.com"></iframe>
  </body>
</html>

2

There are 2 answers

0
Telson Alva On

i got it by doing the below

var javascript = @"

                        var Frame = document.getElementById('monitor');
                        window.external.iFrameCheck(Frame.contentWindow.document);
                        ";

            var doc = (IHTMLDocument2)webBrowser1.Document;

            //Once we have the document, execute our JavaScript in it
            doc.parentWindow.execScript(javascript);

this would then call the below c# code

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        [ComVisible(true)]
        public class ComVisibleObjectForScripting
        {
            public void iFrameCheck(HTMLDocument FrameDoc)
            {
                //Do what you want here with your FrameDoc
                // the innerHTML will give you the content of the iframe
            }
        }
2
LInsoDeTeh On

You can add a JavaScript function to the OnLoad event of the iFrame:

document.getElementById('monitor').onload = function() { }

And then follow these solutions to parse your document. But be aware about cross-site-scripting restrictions. If the src of your iFrame is a totally different domain, it probably won't work.