Search code examples
javascriptfirefoxfirefox-addonfirefox-addon-sdkfirefox-addon-restartless

Opening a background window using the Firefox Add-ons SDK


I am writing a Firefox add-on, and I am using the high-level Firefox Add-on SDK API.

My add-on opens a new window, and opens several tabs in that window.

How can I get this new window to open in the background? I do not want its opening to disrupt the user's focus on the active window.

When opening a tab, there is an inBackground option that can be used for this.

I have searched the windows module documentation high and low, but I cannot find a similar option for when creating new windows!

How do I open this new window in the background?

If Mozilla forbids me from doing so, is there a way I can very quickly push the new window to the background just after it opens, so that it is minimally disruptive?


Solution

  • Not disallowed. Perfectly fine. Do it with a features option of alwaysLowered I think.

    Full list of features found here: https://developer.mozilla.org/en-US/docs/Web/API/window.open#Position_and_size_features

    var sa = Cc["@mozilla.org/supports-array;1"].createInstance(Ci.nsISupportsArray);
    var wuri = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
    wuri.data = 'about:blank';
    sa.AppendElement(wuri);
    let features = "chrome,dialog=no,alwaysLowered";
    var wantTabs = false;
    if (wantTabs) {
      features += ',all';
    }
    /*var sDOMWin = aTab.ownerGlobal; //source DOMWindow*/
    if (PrivateBrowsingUtils.permanentPrivateBrowsing/* || PrivateBrowsingUtils.isWindowPrivate(sDOMWin)*/) {
       features += ",private";
    } else {
       features += ",non-private";
    }
    var XULWindow = Services.ww.openWindow(null, 'chrome://browser/content/browser.xul', null, features, sa);
    

    You can tag this code onto the end to do something after the XULWindow loads:

    XULWindow.addEventListener('load', function() {
      //can lower or raise the window z-index here
      var DOMWindow = XULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
      DOMWindow.gBrowser.selectedTab.linkedBrowser.webNavigation.stop(Ci.nsIWebNavigation.STOP_ALL);
    }, false);