views:

18

answers:

1

I'm a beginner with XUL. I have the following problem.

In the browser overlay I declare a menu item which opens a new window - so far so good.

menu_entry.addEventListener('command', function() {
    window.open('chrome://myextension/content/mywindow.xul',
    'myextension-mywindow',
    'chrome,centerscreen');
}, false);

I'd like this to be more flexible. If the window is already open, it should be focused instead. This is what I have tried

menu_entry.addEventListener('command', function() {
    let mywindow = document.getElementById('myextension-mywindow');
    if (mywindow) {
        mywindow.focus();
    }
    else {
        window.open('chrome://myextension/content/mywindow.xul',
        'myextension-mywindow',
        'chrome,centerscreen');
    }
}, false);

The problem is that document.getElementById('myextension-mywindow') always returns null, so I never enter the if. I guess this is because the window is another chrome in itself.

But if this is so, how can I make the windows interact with each other? Is it possible to focus a window from a widget in another window? I cannot do this from a module, since document and window are not available there.

+1  A: 

I finally found I have to use the nsiWindowMeditator. All I had to do is

menu_entry.addEventListener('command', function() {
    let windowManager = Cc['@mozilla.org/appshell/window-mediator;1'].
        getService(Ci.nsIWindowMediator);
    let mywindow = windowManager.getMostRecentWindow('mywindowtype');
    if (mywindow) {
        mywindow.focus();
    }
    else {
        window.open('chrome://myextension/content/mywindow.xul',
        'myextension-mywindow',
        'chrome,centerscreen');
    }
}, false);

and then in the xul

<window id="myextension-mywindow" windowtype="mywindowtype>
...
</window>
Andrea