NsIWindowMediator

From MozillaZine Knowledge Base
Jump to navigationJump to search

This page is part of the extension development documentation project.

Ask your questions in MozillaZine Forums. Also try browsing example code.

Note: development documentation is in process of being moved to Mozilla Development Center (MDC).

Window mediator is Mozilla component that keeps track of open windows. It's accessed through nsIWindowMediator interface. Two most common uses of nsIWindowMediator are

  1. Getting the most recent / any window of given type.
  2. Enumerating all windows of given type.

In the examples below type specifies the type of windows you want to search. You can specify a type of your own window by creating an windowtype attribute on the <window> element.

Browser windows have navigator:browser window type. To search all windows, regardless of their type, pass an empty string, "".

Getting most recent window

The following code is useful when you need any of the windows of given type, or to check if a window of a particular type (e.g. your extension's Options dialog) is already open.

getMostRecentWindow returns a Window object, or null, if there are no windows with given windowtype open.

var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
         getService(Components.interfaces.nsIWindowMediator);
var win = wm.getMostRecentWindow(type);

Enumerating windows

var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
         getService(Components.interfaces.nsIWindowMediator);
var enumerator = wm.getEnumerator(type);
while(enumerator.hasMoreElements()) {
  var win = enumerator.getNext();
  // do something with |win|
}


References