views:

33

answers:

2

Hello,

I have an extension composed of a browser overlay(ff-overlay.xul) which launches a sidebar(ff-sidebar.xul) through this code (from mozilla extension generator): (in ff-overlay.xul)

<menupopup id="viewSidebarMenu">
    <menuitem key="key_openSidebar_testinstallPackage" observes="viewSidebar_testinstallPackage" />
</menupopup>

<keyset id="mainKeyset">
    <key id="key_openSidebar_testinstallPackage" command="viewSidebar_testinstallPackage" />
</keyset>

<broadcasterset id="mainBroadcasterSet">
    <broadcaster id="viewSidebar_testinstallPackage"
        label="&testinstallPackageSidebar.label;"
        autoCheck="false"
        type="checkbox"
        group="sidebar"
        sidebarurl="chrome://testinstallPackage/content/ff-sidebar.xul"
        sidebartitle="&testinstallPackageSidebar.label;"
        oncommand="toggleSidebar('viewSidebar_testinstallPackage');" />
</broadcasterset>

The main overlay has js code in overlay.js : (in ff-overlay.xul)

<script src="overlay.js"/>

The sidebar xul has js code in ff-sidebar.js : (in ff-sidebar.xul)

<script src="ff-sidebar.js"/>

I need to send some data (a String would be enough) from overlay.js to ff-sidebar.js

I have tried this but it didn't work (and I don't know if it could have worked, it is supposed to be between a main overlay and the current page).

Please provide me with some mean to do this.

A: 

Using nsIWindowMediator is inappropriate in your case. If you need to pass some data form overlay.js to ff-sidebar.js use window.QueryInterface like this :

OVERLAY.JS

var shell = {
message : function(){
       var msg = "Hello";
       return msg;
}
}


FF-SIDEBAR.JS

// put this in the function that is executed when the sidebar loads preferably 
var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
               .getInterface(Components.interfaces.nsIWebNavigation)
               .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
               .rootTreeItem
               .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
               .getInterface(Components.interfaces.nsIDOMWindow);
alert(mainWindow.shell.message); //this will alert Hello

As you could notice that the object of overlay was available in another scope.

Meher Ranjan
+2  A: 

Read https://developer.mozilla.org/en/Working_with_windows_in_chrome_code and then feel free to ask additional questions.

Nickolay
Thx =) "Example 3: Using nsIWindowMediator" did the trick.
BenoitParis
@BenoitParis: Uh, no. You shouldn't use that. To access the sidebar in the same window your overlay code is running in, you should use "Accessing a document in the sidebar". To work the other way, you'd need "Accessing the elements of the top-level document from a child window". In any case, I hope you understood the concept of multiple "scopes" or "windows" that your code ends up running in.
Nickolay