NsIWindowMediator: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
No edit summary
Line 21: Line 21:


== Enumerating windows ==
== 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.
<pre>
<pre>
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
Line 31: Line 33:
</pre>
</pre>


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


== References ==
== References ==

Revision as of 04:11, 1 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