Roll Your Own Browser
Pages: 1, 2
The Script
Let's look now at the source code that initializes the browser. We'll cover the basics just to get some browser functionality working. In my opinion, the best way to find out how Mozilla works is to look at the code. The way to do this is via LXR, the Mozilla Cross Reference. The particular files you should be looking at are navigator.js and help.js, the main JavaScript files associated with the Browser and Help windows respectively.
The following code is a generic way to set things up, but is loosely based on starting these windows and setting up the browser. This script is best placed in the function associated with the load handler of the XUL window.
The first step is to add a couple more attributes to the browser widget in the XUL file.
<browser id="browser-content" type="content-primary" src="about:blank" flex="1"/>
The type attribute is optional but in this instance it is important because
it affects the way the content is treated in the application. The value of
"content-primary" means that this is the main browser content area (for example, content.focus()), and should gain precedence over other areas. The content in this browser will be accessed when you use the content keyword in your script.
The id attribute gives the browser a unique identifier within the
XUL content.This allows you to easily get a handle on the browser widget
from your script:
var myBrowser // declare globally for re-use
myBrowser = document.getElementById("browser-content");
Next, we'll create the browser instance and set up browser window:
var appCore = null; // declare globally for re-use
appCore = Components.classes["@mozilla.org/appshell/component/browser/instance;1"]
.createInstance(Components.interfaces.nsIBrowserInstance);
appCore.setWebShellWindow(window);
The browser is initially blank (because of using the "about:blank" as the src
attribute), so you'll have to load some content into the browser. Here an http URL is used:
const nsIWebNavigation = Components.interfaces.nsIWebNavigation;
myBrowser.webNavigation.loadURI("http://www.mozdev.org",
nsIWebNavigation.LOAD_FLAGS_NONE, null, null, null);
This creates an instance of the nsIWebNavigation XPCOM interface, which
exposes a number of handy utilities for changing and navigating through the
content displayed in the browser. In this instance, to load a page, the loadURI
method is used. Other methods of this interface include goBack(), goForward(), and reload(). nsIWebNaviagtion is just one of the interfaces available when you use the browser widget. Others include:
preferenceswebBrowserFindcontentViewerdocumentCharsetInfo
A full list can be found in the bindings file for the widget. The steps provided in this section will get you well on your way to creating a rich content browser in your Mozilla application and act as a foundation for adding some of the other bells and whistles associated with a browser.
The Art of Navigation
What follows is a brief description of adding Back and Forward widgets and functionality to your window. This is, after all, the key to any reasonable browser, so I'll illustrate the process for hooking up this functionality to your browser. The XUL file is the place to define your UI buttons. If you are proficient at XUL and CSS, you can make them into the style and appearance that you wish, but in this example the current navigator styles (using the Modern theme) are used.

Back and Forward Buttons
These are simple toolbar buttons. The Back and Forward buttons used in the Mozilla browser have an integrated menu that creates a list of the pages available for each one. Appearance is achieved by including the navigator CSS file and using a class specific to buttons (toolbarbutton-1). Here is the XUL code for the buttons:
...
<?xml-stylesheet href="chrome://navigator/skin" type="text/css"?>
...
<toolbarbutton id="back-button" class="toolbarbutton-1"
tooltiptext="Go Back"
oncommand="goBack();"
observes="canGoBack">
</toolbarbutton>
<toolbarbutton id="forward-button" class="toolbarbutton-1"
tooltiptext="Go Forward"
oncommand="goForward();"
observes="canGoForward">
</toolbarbutton>
Apart from the class attribute on the buttons, others to watch for are oncommand, which bridges the functionality, and observes. The observes attribute determines if the button is active. There is a little bit of trickery involved to get the buttons to move to and from a disabled state. It depends on whether navigation can occur either Forward or Back. We will not go into that here, but the full source code for the XUL file and the JavaScript file used in this article is available for reference. Let's look at the code for the goBack() and goForward() functions.
function goBack()
{
var webNavigation = myBrowser.webNavigation;
if (webNavigation.canGoBack)
webNavigation.goBack();
}
function goForward()
{
var webNavigation = myBrowser.webNavigation;
if (webNavigation.canGoForward)
webNavigation.goForward();
}
Once again the webNavigation interface is accessed through the browser widget. After an efficiency check on the observer, the goBack() or goForward() routine from the interface is called. That's all there is to it--three lines of code. The foundations were laid in the initialization code, so now you reap the benefits.
Summary
The rest, as they say, is up to you. You can choose to make your browser as simple or as complex as you wish. Mozilla provides the widgets to hold your content, and as was illustrated in this article, it also gives you the source code to add rich functionality. I just touched the surface here. Other features you can hook in are context menus, events such as drag&drop and onclick on the content area, and view page source. If you are truly ambitious, you can even go one step further and use the tabbed browser. Integrated into the Mozilla browser and turned on by default, this has been a popular addition to the browsing experience, and has won over many new users. The <tabbrowser> widget is more complex than the basic browser, but could bring more rewards. And don't forget, if it doesn't exactly meet your needs, you can change the source code!
Brian King is an independent consultant who works with web and open source technologies.
Return to the Mozilla DevCenter.
Showing messages 1 through 11 of 11.
-
XMLHTTPRequest is missing from the XUL browser
2009-05-22 23:50:35 bertung [View]
XMLHTTPRequest object is missing from the XUL browser. Do we need to add XMLHTTPRequest component externally? how do we do that?
-
questions
2008-06-11 22:29:47 xylogeist [View]
I have a question, I am new to this - what downloads do I need to make my own browser? Just please link me to what all I need to make my own browser and tell me the scripts in them i need to edit.
-
browser constantly refreshes
2006-10-10 18:28:16 jaybird2 [View]
I've got this example working for me somewhat, only the web page keeps refreshing. The url specified in the loadPage() function (last line of the initBrowser() function) is refreshed over and over and over, non-stop.
Anyone else have this issue? Your help would truly be appreciated. Thanks!
-
command line service with gecko
2006-03-07 19:39:14 Isaack [View]
how do you do command line handling with gecko?
I tried
var cmdLineService = Components.classes["@mozilla.org/appshell/commandLineService;1" ].getService( Components.interfaces.nsICmdLineService );
var url = cmdLineService.getCmdLineValue("-x");
but gecko does not seem to have the class @mozilla.org/appshell/commandLineService;1???
any ideas
-
XPI available
2003-02-12 12:15:50 Brian King |
[View]
An XPInstall package is now available for the mini-browser described in this article. You will notice a few changes (renamed to 'mybrowser' ...), but it is essentially the same.
Just click on the link on the following page to install:
http://eu.mozdev.org/Brussels2003/talks/brian/slide25.html
After a full re-start of Mozilla, the 'myBrowser' item should appear in the tools menu.
-
Instructions to get this example up and running
2003-01-22 07:27:34 Brian King |
[View]
Here are quick and dirty instructions to get the customised browser example from this article up and running:
1. Create the following directory structure below the chrome directory on your Mozilla installation:
mybrowser/content
2. Create the following file, name it 'contents.rdf', and put it in the new content directory:
<?xml version="1.0"?>
<RDF:RDF xmlns:RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:chrome="http://www.mozilla.org/rdf/chrome#">
<!-- list all the packages being supplied -->
<RDF:Seq about="urn:mozilla:package:root">
<RDF:li resource="urn:mozilla:package:mybrowser"/>
</RDF:Seq>
<!-- package information -->
<RDF:Description about="urn:mozilla:package:mybrowser"
chrome:displayName="myBrowser"
chrome:author="frillies"
chrome:name="mybrowser">
</RDF:Description>
</RDF:RDF>
3. Add the XUL and JS file to the content folder too.
Please rename the files to mybrowser.xul/.js, as I don't want any clashes if you choose to install it on a Phoenix distribution.
When you do this, you will have to modify in the XUL file the path to the JS file from:
<script type="application/x-javascript" src="chrome://jslib/content/browser.js"/>
to
<script type="application/x-javascript" src="chrome://mybrowser/content/mybrowser.js"/>
4. Close Mozilla. In the chrome folder, add the following line to 'installed-chrome.txt'.
content,install,url,resource:/chrome/mybrowser/content/
5. Launch the example from the command line:
mozilla -chrome chrome://mybrowser/content
That should do it!
-
Modify a Mozilla distribution?
2002-11-11 17:44:43 anonymous2 [View]
I'm running the latest release of Mozilla 1.2b and am wondering if it's possible to alter the UI via changing XUL files. Is this possible?
-
how to use it?
2002-10-23 00:13:59 anonymous2 [View]
This article is very interesting and show us the power of Mozilla technology but I did not understand where to put those 2 files (.xul and .js) in a mozilla structure to work?
I am actually looking at using mozilla as a viewer for autorun CDROM under MSWindows. I have a couple of issue like:
- how to make it run without any specific profile (it seems that the current article answer more or less this issue)
- how to auto load a local file in the browser without a complete URL like file:////C:/dir/myfile.html since I don't know the letter of the CDROM. The only workaround I found is to use the URL type resource://res/myfile.html
any ideas?
Thanks anyway for this very useful article. please continue to add more!
-
Reverse Browser
2002-10-12 14:59:47 anonymous2 [View]
I am working on the mozilla code to produce a browser which will display exactly the mirror image of the web pages, meaning a total reverse look to the WWW. Not only the images look reverse but the text can only be read from right to left.
Can anyone help me with this or tell me wheather such a browser exists? I am interested in getting into contact with developers to produce this art project. Thanks, Genco Gulan. istanbulmuseum@yahoo.com
-
Starting up with specified chrome - the answer
2002-09-25 04:39:20 anonymous2 [View]
Found it.
var cmdLineService = Components.classes["@mozilla.org/appshell/commandLineService;1" ].getService( Components.interfaces.nsICmdLineService );
var url = cmdLineService.getCmdLineValue("-url");
-
Starting up new chrome with a specified URL
2002-09-25 03:15:43 anonymous2 [View]
I'm trying to start Mozilla up using a new chrome:
mozilla -chrome nav2.xul
This works OK, but when I also try to load a URL at the same time:
mozilla -chrome nav2.xul -url http://localhost
the url parameter seems to be ignored. Is there anything I can do about this?









