开发者

Send data between XUL components

开发者 https://www.devze.com 2023-01-23 00:18 出处:网络
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):

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&g开发者_如何学Pythont;

<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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消