MozillaZine

NsIWindowMediator

From MozillaZine Knowledge Base

(Difference between revisions)
Revision as of 04:11, 1 August 2005
Asqueella (Talk | contribs)
(Enumerating windows)
<-- Previous diff
Revision as of 11:50, 9 August 2005
Asqueella (Talk | contribs)
(References)
Next diff -->
Line 39: Line 39:
** [http://xulplanet.com/references/xpcomref/ifaces/nsIWindowMediator.html nsIWindowMediator reference] ** [http://xulplanet.com/references/xpcomref/ifaces/nsIWindowMediator.html nsIWindowMediator reference]
** [http://xulplanet.com/references/objref/ChromeWindow.html ChromeWindow object reference] ** [http://xulplanet.com/references/objref/ChromeWindow.html ChromeWindow object reference]
 +* Devmo
 +** [http://developer.mozilla.org/en/docs/Working_with_windows_in_chrome_code Working with windows in chrome code]
[[Category:Example code|nsIWindowMediator]] [[Category:XPCOM example code|nsIWindowMediator]] [[Category:Example code|nsIWindowMediator]] [[Category:XPCOM example code|nsIWindowMediator]]

Revision as of 11:50, 9 August 2005

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 document element (the top-level one, e.g. <window> or dialog).

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 ChromeWindow object, or null, if there are no windows of given type open.

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

Enumerating windows

The following code can be used if you need to do something for each open window of a particular type. For example you could use it in "OK" handler of your Options dialog to apply the new settings to each open browser window.

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();
  // |win| is [Object ChromeWindow] (just like |window|), do something with it
}

Note: in nsIWindowMediator's reference page the return type of getMostRecentWindow and the type of enumerator's elements is said to be nsIDOMWindow/nsIDOMWindowInternal. In fact, those methods usually (always?) return a ChromeWindow object, implementing both of those interfaces and a few others, when called from JavaScript code. The global window object, you're probably familiar with, is of ChromeWindow type.

References