[英]XUL/Firefox: create new browser window on addon load
I'm developing bootstrapped Firefox addon. 我正在开发自举式Firefox插件。 In my
startup
function I am constructing new instance of object, that creates new Firefox window with the following code: 在
startup
函数中,我正在构造对象的新实例,该实例使用以下代码创建新的Firefox窗口:
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. 出于某种未知的原因,该代码在不同情况下的行为会有所不同:在Firefox 20之前的版本中,如果在应用启动时执行,
onPageShow
永远不会触发onPageShow
回调。 I assume this is because gBrowser
's selectedBrowser
just doesn't load homepage and stops after about:blank
has loaded. 我认为这是因为
gBrowser
的selectedBrowser
只是不加载主页,并且在about:blank
加载后停止。 Why does it load homepage in new window when I reactivate my extension manually (when Firefox is loaded already)? 当我手动重新激活扩展程序(已经加载Firefox)时,为什么在新窗口中加载主页?
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. 在仍将主页加载到
selectedBrowser
(在新的Fx版本中)中时出现问题:在这种情况下,它不会开始加载由我指定的URL,而是继续加载我的主页。
So how can I determine if gBrowser
is gonna load homepage or not? 那么,如何确定
gBrowser
是否要加载主页? 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: PS:为使以上代码正常工作,必须执行以下
import
并使用以下备用:
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")
};
}
It seems I was wrong in some of my assumptions. 我的某些假设似乎是错误的。 I don't have to wait until
pageshow
event. 我不必等到
pageshow
事件。 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
. 根据它,有一个观察者通知,
browser-delayed-startup-finished
,足以开始执行我必须使用window
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.