I’m using C# .NET Framework 2.0 to create application that can pick up clicked part of DOM element by user in WebBrowser control.
So far I can pick html tags and its attributes in the HTML in WebBrowser control.
//adding event when user mouse down or focus out
webBrowser1.Document.MouseDown += new HtmlElementEventHandler(myMouseDown);
And those event would output DOM information to listbox etc
private void myMouseDown(object sender, HtmlElementEventArgs e)
{
HtmlElement tag = webBrowser1.Document.GetElementFromPoint(e.ClientMousePosition);
txtRecord.Items.Add("TagName=" + tag.TagName);
txtRecord.Items.Add("id=" + tag.GetAttribute("id"));
txtRecord.Items.Add("name=" + tag.GetAttribute("name"));
txtRecord.Items.Add("type=" + tag.GetAttribute("type"));
txtRecord.Items.Add("value=" + tag.GetAttribute("value"));
txtRecord.Items.Add("class=" + tag.GetAttribute("class"));
txtRecord.Items.Add("inner text=" + tag.InnerText);
}
But then the events mouseDown is not working in iFrame.
When I click inside of the iFrame, it does not spit DOM information.
Is it possible to pick up DOM info by user clicking on that spot in iFrame?
UPDATE:
Found HtmlWindow object and applied same event logic.
HtmlWindow iFrame;
iFrame = webBrowser1.Document.Window.Frames[0];
iFrame.Document.MouseDown += new HtmlElementEventHandler(myMouseDown);
But then the last line throws UnauthorizedAccessException 🙁
I found out why I’m getting UnauthorizedAccessException. It’s because my main page is http://localhost/index.html while iFrame src is which violates cross-frame scripting security according to:
I’ve changed iFrame src to use same domain, http://localhost/secondPage.html
then it start working!