如何检查 Xul 中给定的窗口是否打开?

发布于 2024-10-15 17:09:28 字数 726 浏览 3 评论 0原文

如何检查 Xul 中给定的窗口是否打开?

我想检查我的桌面应用程序中是否已打开窗口。所以如果是的话我就不会再打开它了。

--我的尝试

我试图使用窗口标题来完成此操作,因此我从 windowManager 获取窗口列表并检查标题,但是 getAttribute 不是来自我可以查询的接口,它来自 element,什么我应该使用接口吗?

var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService(Components.interfaces.nsIWindowMediator);
var enum = windowManager.getXULWindowEnumerator(null);
while(enum.hasMoreElements()) {
    var win = enum.getNext().QueryInterface(Components.interfaces[" WHICH INTERFACE TO PUT HERE? "]);
    write("WINDOW TITLE = " + win.getAttribute("title"));
}

How to check if a given window is open in Xul?

I would like to check if a window is already openned in my desktop app. So if it is, I'll not open it again.

-- my attempt

I'm trying to accomplish this using the window title, so I get the list of windows from windowManager and check the title, but the getAttribute is not from an interface that I can query, it's from element, what interface should I use?

var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService(Components.interfaces.nsIWindowMediator);
var enum = windowManager.getXULWindowEnumerator(null);
while(enum.hasMoreElements()) {
    var win = enum.getNext().QueryInterface(Components.interfaces[" WHICH INTERFACE TO PUT HERE? "]);
    write("WINDOW TITLE = " + win.getAttribute("title"));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苍暮颜 2024-10-22 17:09:29
var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService(Components.interfaces.nsIWindowMediator);
var enum = windowManager.getEnumerator(null);
while(enum.hasMoreElements()) {
  var win = enum.getNext().QueryInterface( Components.interfaces.nsIDOMChromeWindow );
  write("WINDOW TITLE = " + win.document.documentElement.getAttribute("title") );
}

如果您使用 getXULWindowEnumerator 您应该使用 Components.interfaces.nsIXULWindow

如果您自己打开窗口,您可能可以使用 nsIDOMWindow 属性名称,因为您在 open 函数中设置了窗口的名称。这对用户来说是不可见的,因此您有更多的灵活性。

var win = window.open( "chrome://myextension/content/about.xul", 
                          "windowName", "chrome,centerscreen" );

write( "WINDOW NAME: " + win.name ); // Should now give  WINDOW NAME: windowName

如果您将窗口名称留空,它每次都会打开一个新窗口。但是,如果您使用窗口名称(除 "" 之外的其他名称),如果窗口不存在,它将创建它,或者使用您指定的名称在已存在的窗口中加载新内容。

这看起来几乎就是你想要的。但是如果有必要,您可以使用 name 属性来避免重新加载。

var openNewWindow = true;
var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService(Components.interfaces.nsIWindowMediator);
var enum = windowManager.getEnumerator(null);

while(enum.hasMoreElements()) {
  var win = enum.getNext().QueryInterface( Components.interfaces.nsIDOMChromeWindow );
  if( win.name == "windowName" ) {
    openNewWindow = false;
  }
}

if( openNewWindow ) {
  var win = window.open( "chrome://myextension/content/about.xul", 
                      "windowName", "chrome" );
}
var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService(Components.interfaces.nsIWindowMediator);
var enum = windowManager.getEnumerator(null);
while(enum.hasMoreElements()) {
  var win = enum.getNext().QueryInterface( Components.interfaces.nsIDOMChromeWindow );
  write("WINDOW TITLE = " + win.document.documentElement.getAttribute("title") );
}

if you are using getXULWindowEnumerator you should use Components.interfaces.nsIXULWindow

you probably could use the nsIDOMWindow attribute name if you open the windows your self because you set the name of the window in the open function. This is not visible to the user so you have a little more flexibility

var win = window.open( "chrome://myextension/content/about.xul", 
                          "windowName", "chrome,centerscreen" );

write( "WINDOW NAME: " + win.name ); // Should now give  WINDOW NAME: windowName

If you are leaving the window name blank it will open a new window every time. If you however use a window name (something else than "" ) it will create it if it does not exists, or load the new content in the already existing window with the name you have specified.

Which seems like almost what you want. Butt you could use name attribute to avoid the reload if you have to.

var openNewWindow = true;
var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService(Components.interfaces.nsIWindowMediator);
var enum = windowManager.getEnumerator(null);

while(enum.hasMoreElements()) {
  var win = enum.getNext().QueryInterface( Components.interfaces.nsIDOMChromeWindow );
  if( win.name == "windowName" ) {
    openNewWindow = false;
  }
}

if( openNewWindow ) {
  var win = window.open( "chrome://myextension/content/about.xul", 
                      "windowName", "chrome" );
}
山色无中 2024-10-22 17:09:28

如果您在文档的 元素上设置 windowtype="myWindowType" 属性,则只需使用 windowMediator.getMostRecentWindow('myWindowType');< /code> 查看您是否已经打开了一个。

If you set a windowtype="myWindowType" attribute on your document's <window> element then you can just use windowMediator.getMostRecentWindow('myWindowType'); to see whether you already have one open.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文