简体   繁体   English

xpcom数组可以在不同的xul文件中传输吗?

[英]xpcom array can transfer in different xul files?

Now I am developing a firefox extension, can I define a global array in a js with the main xul . 现在,我正在开发firefox扩展,可以在具有主要xul的js中定义全局数组。 and I found when I use it in another js with another xul , it could not worked , so I searched the document of Firefox development. 当我在另一个带有xul的js中使用它时,发现它不起作用,所以我搜索了Firefox开发文档。 I found a common array can not be transfered between two js files with different xul files. 我发现无法在具有不同xul文件的两个js文件之间传输通用数组。 and then I defined a xpcom mutablearray in a js: 然后我在js中定义了xpcom mutablearray:

var eleList = Components.classes["@mozilla.org/array;1"] .createInstance(Components.interfaces.nsIMutableArray); var eleList = Components.classes [“ @ mozilla.org/array;1”] .createInstance(Components.interfaces.nsIMutableArray);

but when I want to use it in another js : it still not work , why? 但是当我想在另一个js中使用它时:它仍然不起作用,为什么呢? Thank you very much! 非常感谢你!

The problem is that even when you create the mutable array, you are storing the reference to this array object in a variable in the context of the XUL that creates it. 问题是,即使创建可变数组,也要在对该数组对象的引用中将其存储在创建它的XUL上下文中的变量中。 From the other XUL, you don't see that variable so you cannot access the array. 在另一个XUL中,您看不到该变量,因此无法访问该数组。

There are several solutions to access an object accross different contexts: 有多种解决方案可用于跨不同上下文访问对象:

  • From one of the contexts, get a reference to the other context. 从其中一个上下文中,获得对另一个上下文的引用。 For example, you have a variable defined in the main window (browser.xul overlay), you can get it from a sidebar by using the window manager to get a reference to the main window. 例如,您在主窗口中定义了一个变量(browser.xul覆盖),您可以通过使用窗口管理器来获取对主窗口的引用,从边栏中获取它。

  • You can create your own XPCOM object, although this is not simple to do, specially for a simple case like yours 您可以创建自己的XPCOM对象,尽管这并不容易,特别是针对像您这样的简单情况而言

  • You can used JavaScript modules to create a global singleton. 您可以使用JavaScript模块来创建全局单例。

You should consider looking into the JavaScript modules option. 您应该考虑研究JavaScript模块选项。 It's the easiest and most reliable way. 这是最简单,最可靠的方法。 It was introduced in Firefox 3.0 and you can do it using only JavaScript without having to create a XPCOM 它是Firefox 3.0中引入的,您可以仅使用JavaScript来完成此操作,而无需创建XPCOM

Check here for more details: 在这里查看更多详细信息:

https://developer.mozilla.org/en/JavaScript_code_modules/Using_JavaScript_code_modules https://developer.mozilla.org/en/JavaScript_code_modules/Using_JavaScript_code_modules

UPDATED: 更新:

Here are some more details about how to get a reference to the main window to see what variables defined in browser.xul. 以下是有关如何获取对主窗口的引用的更多详细信息,以查看在browser.xul中定义了哪些变量。

You define the variable in the browser.xul overlay (main window): 您可以在browser.xul叠加层(主窗口)中定义变量:

myArray = [1, 2, 3];

Then from the sidebar xul, you can get a reference to the main window and access your variable using the reference: 然后从侧边栏xul中,可以获得对主窗口的引用,并使用该引用访问变量:

var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                    .getInterface(Components.interfaces.nsIWebNavigation)
                    .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
                    .rootTreeItem
                    .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                    .getInterface(Components.interfaces.nsIDOMWindow);

var value = mainWindow.myArray[0];

These links might have useful information for you: 这些链接可能为您提供有用的信息:

https://developer.mozilla.org/en/Working_with_windows_in_chrome_code https://developer.mozilla.org/en/Working_with_windows_in_chrome_code

https://developer.mozilla.org/en/Code_snippets/Tabbed_browser https://developer.mozilla.org/en/Code_snippets/Tabbed_browser

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM