MDN’s new design is in Beta! A sneak peek: https://blog.mozilla.org/opendesign/mdns-new-design-beta/

View Source for XUL Applications

Add-ons using the techniques described in this document are considered a legacy technology in Firefox. Don't use these techniques to develop new add-ons. Use WebExtensions instead. If you maintain an add-on which uses the techniques described here, consider migrating it to use WebExtensions.

From Firefox 53 onwards, no new legacy add-ons will be accepted on addons.mozilla.org (AMO).

From Firefox 57 onwards, WebExtensions will be the only supported extension type, and Firefox will not load other types.

Even before Firefox 57, changes coming up in the Firefox platform will break many legacy extensions. These changes include multiprocess Firefox (e10s), sandboxing, and multiple content processes. Legacy extensions that are affected by these changes should migrate to WebExtensions if they can. See the "Compatibility Milestones" document for more.

A wiki page containing resources, migration paths, office hours, and more, is available to help developers transition to the new technologies.

Importing gViewSourceUtils

XUL applications wanting to show the source code for documents should import the viewSourceUtils.js script instead of attempting to open the viewSource.xul window themselves:

<script type="application/javascript" src="chrome://global/content/viewSourceUtils.js"/>

viewSourceUtils.js exposes a gViewSourceUtils global into the scope of the window that imports that script.

Method overview

gViewSourceUtils exposes several methods, but the only one you should be using directly is the viewSource method. The rest of those methods should be considered private, and might become inaccessible in the future.

void viewSource(aObject);

Methods

viewSource

Opens a viewer to the source code for some document or URI.

Parameters

The viewSource method takes an Object as its first and only parameter. The Object can include the following properties:

URL (required)
A string URL for the document to view the source of.
browser (optional)
The browser containing the document that we would like to view the source of. This is optional if outerWindowID is not passed.
outerWindowID (optional)
The outerWindowID of the content window containing the document that we want to view the source of. Pass this if you want to attempt to load the document source out of the network cache.
lineNumber (optional)
The line number to focus on once the source has loaded.

Deprecated API

This API is likely to go away in the future, and should no longer be used.

 The viewSource method will also accept the following arguments in place of the Object:

aURL
A string URL for the document to view the source of.
aPageDescriptor (optional)
A "descriptor" that allows the source to be retrieved from the network cache. This is usually gotten from the nsIWebPageDescriptor interface via currentDescriptor.
aDocument (optional)
The content document that we're attempting to load the source from.
aLineNumber (optional)
The line number to focus on once the source has loaded.

Document Tags and Contributors

 Contributors to this page: bunnybooboo, rolfedh, mconley
 Last updated by: bunnybooboo,