Join MDN and developers like you at Mozilla's View Source conference, 12-14 September in Berlin, Germany. Learn more at https://viewsourceconf.org

The NativeWindow object is only available to privileged code running on Firefox for Android, and is intended for use by Firefox for Android add-ons.

The NativeWindow object enables Firefox for Android add-ons to create user interface components.

NativeWindow is available as a property of the chrome window object. For example, if you use this template for initializing your extension, you can access it from the window argument passed into loadIntoWindow():

function loadIntoWindow(window) {
  window.NativeWindow.toast.show("I'm starting!", "short");
}

You can also access the chrome window using nsIWindowMediator:

let window = Services.wm.getMostRecentWindow("navigator:browser");
window.NativeWindow.toast.show("Here's another toast message!", "short");

Obtain NativeWindow object with add-on SDK:

// Obtain commonly used services : Services.jsm
// https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Services.jsm
const { Services } = require("resource://gre/modules/Services.jsm");

function getNativeWindow() {

	let window = Services.wm.getMostRecentWindow("navigator:browser");
	return window.NativeWindow;

}

The NativeWindow object enables developers of Firefox for Android add-ons to create UI components.

It supports the following components:

Add items to the main menu in Firefox for Android. See the menu API documentation.

/*
label: menu label
icon: file:// or data: URI for an icon
callback: JS function called when menu is tapped
returns a menu ID that can be used to remove the menu
*/
let id = NativeWindow.menu.add(label, icon, callback);
NativeWindow.menu.remove(id);

doorhanger

Show and hide doorhanger notifications. See the doorhanger API documentation.

/*
message: displayed text
value: string based tag
buttons: array of JS objects used to create buttons in the notification
tabId: tab associated with this notification
options: JS object that has 'persistence' and 'timeout' options
*/
NativeWindow.doorhanger.show(message, value, buttons, tabId, options);
NativeWindow.doorhanger.hide(value, tabId);

contextmenus

Add and remove context menu items. See the contextmenus API documentation.

/*
label: menu label
selector: JS object that has a 'matches(element)' function. Used to show the menu.
callback: JS function called when menu is tapped
returns a menu ID that can be used to remove the menu
*/
let id = NativeWindow.contextmenu.add(label, selector, callback);
NativeWindow.contextmenu.remove(id);

toast

Show Android toast notifications. See the toast API documentation.

NativeWindow.toast will be deprecated in Firefox 45.

New code should use Snackbars.jsm to display notifications. To maintain backward compatibility NativeWindow.toast will still work, by delegating calls to Snackbars.jsm.

/*
message: displayed text
duration: "short" or "long"; Used for alert timeout
*/
NativeWindow.toast.show(message, duration);

 

pageactions

Add and remove pageactions, i.e. clickable indicators in the URL bar. See the pageactions API documentation.

NativeWindow.pageactions was deprecated in Firefox 34. Use the new PageActions.jsm instead.

/*
title: Pageaction title
icon: Icon image for the pageaction
clickCallback: Callback called when pageaction is clicked
longClickCallback: Callback called when pageaction is long pressed
*/
let options = {
  title: "title",
  icon: "chrome://myaddon/skin/image.png",
  clickCallback: function() { },
  longClickCallback: function() { } (optional)
};

//Adding pageaction
let id = NativeWindow.pageactions.add(options);

//Remove pageaction
NativeWindow.pageactions.remove(id);

Document Tags and Contributors

 Last updated by: wbamberg,