Following up on my previous post about using the WebBrowser control to display HTML content that is stored in the filesystem and so available offline I thought I’d experiment with a little code.
I wrote a couple of HTML pages that look like this in IE9 (running from the filesystem);
and these pages are copies of each other really and use a simple stylesheet, some simple JavaScript that runs a clock in the bottom left of the page and some images. The structure of my content on disk looks like;
I zipped that all up into a zip file and embedded it into a Silverlight application as content. You can see it here in my Visual Studio solution;
The idea is that I’ll have code which extracts this Zip file into a sub-folder of MyDocuments on the user’s machine so the HTML is available offline.
The application will need to be trusted to write into MyDocuments in this way.
I can’t pre-determine the location of MyDocuments on a particular user’s machine so I’ve made a bit of a hack to the HTML files in that they look like this;
The (very simplistic) idea is that when I load these HTML files to display them in the WebBrowser control, I’ll do a search for that pre-defined token and replace it with the actual sub-folder location where I’ve unpacked the HTML on the user’s machine.
With that done, I wrote an “application” that’s embedded into the page here if you want to try it ( and you trusted the elevated permissions it’s asking for ). This was built on Silverlight 5 but if you’re not running that then I suspect you can download the code at the end of the post and switch it to Silverlight 4 – I don’t think I did anything SL5 specific
When the application runs up in the browser, it spots the lack of elevated trust and offers an installation button;
and when the application is installed it will look like this on Windows in the first instance;
The first CheckBox indicates that the HTML content has not yet been copied to the hard-drive so clicking on the button should cause that zip file to be unzipped to MyDocuments\localHtmlFolder and the UI should update;
Now, the UI and behaviour differs for OS X and Windows here. On OS X you should not see the other 2 CheckBoxes and the “Dynamically Create WebBrowser Control” button should be enabled.
On Windows, as per my previous post, those other 2 CheckBoxes relate to the sllauncher.exe DWORD value in 2 places within the registry being set to a value of 1.
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BLOCK_LMZ_SCRIPT
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BLOCK_LMZ_IMG
you’d need to clear those DWORD values down to a value of 0 and re-run the application before the “Dynamically Create WebBrowser Control” button becomes enabled (I don’t attempt to dynamically monitor the registry here).
Having taken those extra steps on Windows you should now be in the same place as OS X and able to create the WebBrowser control which should navigate through the simple content on the hard-drive including loading the images, allowing for simple navigation and running the clock script.
I say “simple” because only basic relative hyperlinks are likely to work – e.g. I could imagine that links with fragments within the document won’t work because my code currently assumes that every hyperlink is a request to another file that it can find on the disk.
I’ve tried this on OS X and it seems to work fine for me and, in fact, it sort of works better because there’s no registry keys to mess around with up front.
Naturally, the ideal next step would be to be able to mitigate the need for those 2 registry changes on Windows. I don’t think there’s a way that your application would be able to set the values to 0 as part of its own initialisation process but I need to look into that a little bit more.
However, in the meantime I’ve shared the code for download here if you’re interested in it as a starting point (i.e. I’m sure there are lots of problems to be ironed out).
Finally, I’d recommend putting those registry keys back to their original values if you experimented with this code.