MozNet Code Snippets

XPCom Initialization
Below, you will find two initialization examples. One in C# and the otherin Visual Basic. Each shows the proper way to initialize XPCom.

XPCom should be initialized in the startup file of your application.
The name of the file, depending on your chosen language, is:
  • WPF - App.xaml.cs/vb
  • Winforms/C# - Program.cs
  • Winforms/VB - ApplicationEvents.vb
C# Initialization Example: (App.xaml.cs or Program.cs)

Visual Basic Initialization Example: (ApplicationEvents.vb)

Javascript Injection

Look below for an updated method of using the scripting system!

The following examples will demonstrate how to use the script injection feature in MozNET.

You're probably wondering about __doMozJSPostback(...) in the examples above.
MozNetEventRunner is an intrinsic javascript variable that is accessible from any of your injected scripts.In order to return values to your code you must use __doMozJSPostbackin your script as this is how MozNET receives the values from your injected code.The parameters passed to __doMozJSPostback are the name of the script (_SimpleScript_ in the examples above) and the value(s) you want returned to your code. You cannot return objects such as nodes or elements. To return multiple values you can use JSON string notation or a comma separated string or whatever other method you can think up and works for you.
MozScriptEventArgs returns the MozWindow instance in which the injected script was ran, the name of the script andthe results, if any, returned by your script. Results are returned as a string.
Although the system is far from perfect, it's 100% better than what is currently available in the GeckoFX, OGS and other such wrappers.
Also, keep in mind that InvokeScript is a blocking method so watch what you're doing in your scripts because it will 'hang' your UI while they run.

This scripting system is also readily available for use directly from your applications' location bar.
To access the scripting capabilities from your address bar just prefix your script as you would if using the WebView.Navigate("javascript:...") hack.
When MozNET receives a url prefixed with javascript: the scripting system will automatically kick in and take over. Instead of your browser object invoking an unneeded navigation event the scripting system will run your script in the context of the current page. Scripts ran using this system are under the same security context as any other scripts on the page. They cannot access XPCom components. If you need a higher security context you can write an XPCom component that is able to interact with web pages.
MozNet gives you unrestricted access to all, previously declared interal/private, native XPCom interfaces. This gives you more flexibility than was ever possible in previous releases of MozNET and all releases of GeckoFX and other wrappers. Using this flexibility you'll be able to access your custom XPCom components from either the MozNet source code, for a cleaner approach, or directly from your application.
I've written a simple tutorial that walks you through the steps required to create a custom, Javascript based, XPCom component that you can use in your own application - Custom XPCom Component Tutorial

The scripting system will not take over when interacting with page elements that affect navigation via javascript.

Starting with the latest release, R18 at the time of this writing, the scripting system has been enhanced to allow inline return values from your injected scripts!

A quick example is as follows:


WPF : Bitmap To ImageSource Converter

If you're writing a WPF application and want to bind an Image or other control to the FavIcon property of MozWebViewer, this converter can be used to easily make the conversion for you. It's used in the example project to bind the FavIcon property to the Image element that's embedded in the template for the tab control.


nsIObserver Example

With the new MozObserverNotifier class you may need to listen for your own notifications. This requires setting up a listener that implementsnsIObserver, nsIWeakReference and nsISupportsWeakReference. Below, you will find a complete class that you can use in your own project or you can model a class after it - your choice. This class can even be used as an inline observer for notifications other than your own.