It seems I was wrong in some of my assumptions. I don't have to wait until pageshow
event. After a long research I found this bug. According to it there is an observer notification browser-delayed-startup-finished
that is enough to start doing things I had to do with window
.
XUL/Firefox: create new browser window on addon load
-
04-04-2022 - |
Вопрос
I'm developing bootstrapped Firefox addon. In my startup
function I am constructing new instance of object, that creates new Firefox window with the following code:
function createWindow (cb) {
const appHiddenWindow = Services.appShell.applicationProvidedHiddenWindow
, hidden = Services.appShell.hiddenDOMWindow;
// For definition of OpenBrowserWindow() see
// http://mxr.mozilla.org/mozilla-central/source/browser/base/content/browser.js
// Prior to Firefox 20 "private" option is silently ignored
let parent = appHiddenWindow ? hidden : RecentWindow.getMostRecentBrowserWindow()
, window = parent.OpenBrowserWindow({ private: true });
window.addEventListener("load", onWindowLoad);
return window;
function onWindowLoad (e) {
window.removeEventListener("load", onWindowLoad);
window.gBrowser.addEventListener("pageshow", onPageShow);
}
function onPageShow (e) {
dump("onPageShow\n");
try {
cb(window);
} catch (e) {}
}
}
For some unknown for me reasons this code behaves differently in different circumstances:
on Firefox prior to 20 if it is being executed on app startup, onPageShow
callback never fires. I assume this is because gBrowser
's selectedBrowser
just doesn't load homepage and stops after about:blank
has loaded. Why does it load homepage in new window when I reactivate my extension manually (when Firefox is loaded already)?
Trouble appears when it yet loads homepage into selectedBrowser
(in newer Fx versions): in this case it doesn't start loading URL, specified by me and instead continues to load my homepage.
So how can I determine if gBrowser
is gonna load homepage or not? And why does it behave differently on App startup and on addon reactivation?
PS: For the above code to work one must do these import
s and use following fallback:
Cu.import("resource://gre/modules/Services.jsm");
try {
Cu.import("resource:///modules/RecentWindow.jsm");
} catch (e) { // Firefox prior to v20
var RecentWindow = {
getMostRecentBrowserWindow: function ()
Services.wm.getMostRecentWindow("navigator:browser")
};
}
Решение