Wednesday, August 08, 2007
« Jake Showing Off | Main | Almost a month now with iPhone ShieldZon... »

Sounds really simple huh? I was creating a little tray application that did some screen scraping of HTML pages.

I ended up with a large string (scraped HTML data) that I needed to display on a Form.

This is what I came up with, use the WebBrowser control, but it does not take a string as input, I ddin't really want to save the string to a file on the disk.

   this.webBrowser1.Navigate("about:blank");
   HtmlDocument doc = this.webBrowser1.Document;
   doc.Write(strHtmlString);

Works like a charm. If this is a dumb way to do this, please comment and tell me a better way.

Thursday, August 09, 2007 7:55:51 AM (Pacific Standard Time, UTC-08:00)
Not sure how "robust" you need this to be or if you're just messing around. I do this in my CR_Documentor plugin and it's a little harder than that, at least with the stuff I need to support. Are you going to have to change the HTML beign displayed? Does it need to be styled? What happens if the person right-clicks and selects "Refresh?" Are there links? What happens if someone clicks one? What I end up doing:
* Embed a default "blank" HTML page - include the <head> and everything and leave the <body> empty. This lets you define all of the styles and scripts in the <head>.
* When the form displays, write the default page to a temporary location and tell the page to navigate to that temporary location. Once that's done, delete the temp file. (You know it's there by intercepting the NavigateComplete event - when that event fires, check to see if the temp file exists and delete it.)
* Rather than swapping out the Document, swap out the body:
((mshtml.IHTMLDocument2)this.Document).body.innerHTML = myBodyString;
All of the styling will stay intact, all of the script will stay intact, just the content will change. Then you don't have to rebuild it every time.
* On the web browser control, set up a handler for the BeforeNavigate event and cancel it.
* In the default "blank" HTML page, add some JavaScript that intercepts the right-click and cancels it. That way the user can't "refresh" and screw things up.

Hosting a browser is easy - controlling what it does so the user can't screw themselves is way harder.
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):