views:

37

answers:

1

Now I am developing a firefox extension, can I define a global array in a js with the main xul . 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. I found a common array can not be transfered between two js files with different xul files. and then I difined a xpcom mutablearray in a js:

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? Thank you very much!

+2  A: 

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. From the other XUL, you don't see that variable so you cannot access the array.

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.

  • You can create your own XPCOM object, although this is not simple to do, specially for a simple case like yours

  • You can used JavaScript modules to create a global singleton.

You should consider looking into the JavaScript modules option. 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

Check here for more details:

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.

You define the variable in the browser.xul overlay (main window):

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:

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/Code_snippets/Tabbed_browser

fms
Firstly thank you very much! and as you said:"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." I didn't know it clearly. and my case just same you have said:I get a array of observing swf files path in a js of main window(browser xul overlay),and I want to get them from a sidebar js, so I would to defined a variable in browser.xul? and how to do , how to get it from a sidebar js? Thank you again.
jin
Just added some more information to the response. Hope it helps!
fms
Thank you for your help , I think these code maybe add in js of xul,because my js code is long?
jin
hi @jin, I'm not sure I understand your last question?
fms