views:

1240

answers:

3

There is a webpage loaded in the firefox sidebar and another webpage loaded in the main document. Now, how do I ask access the main document object through the Firefox sidebar? An example to do this through Javascript code in the firefox sidebar document to access the main document would be helpful.

Thanks for the answers. I have to refine my question however. The main window has some webpage loaded and the sidebar has a webpage. I want the sidebar window to know what text the user has selected on the main window when a link on the sidebar window is clicked. I know how to get the selected text from a window. Only that the sidebar element adds complexity to the problem that I am not able to surpass.

@PConory:

I like your answer, but when I try it there is an error: Error: Permission denied to create wrapper for object of class UnnamedClass.

Thanks.

+1  A: 

Accessing the main window from a sidebar is much trickier than going back the other way.

The DOM tree you'll need to traverse, according to Mozilla's developer centre, is:

#document
  window                 main-window
    ...
      browser
        #document
          window         sidebarWindow

From the above link, the following code will allow you to get at the mainWindow object:

var mWin = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
               .getInterface(Components.interfaces.nsIWebNavigation)
               .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
               .rootTreeItem
               .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
               .getInterface(Components.interfaces.nsIDOMWindow);
ConroyP
That won't work for *web* pages however. This code is for cases when the page in sidebar is privileged (i.e. is a part of an extension).
Nickolay
+2  A: 

As far as I can tell, you are actually loading a web site in the sidebar (checked the 'Load this bookmark in Sidebar'). If this is the case, AND if the sidebar is opening the main window page. You can use the window.postMessage to communicate between them. But like I said, the sidebar page has to open the main page because you need the window reference in order to post the message.

sidebar.js

var newwin = window.open('http://otherpage')
newwin.onload = function()
{
 newwin.postMessage('Hey newwin', 'http://sidebar');
};

mainpage.js
window.addEventListener('message',function(e)
{
 if(message.origin == 'http://sidebar')
  alert('message from sidebar');
},false);

Using this you still do not have access to the document, but can communicate between them and script out any changes you want to do.

EDIT: Putting some more thought into it, if you opened the window from the side bar, you would have the DOM for it. var newwin = window.open('blah'); newwin.document making the hole postMessage thing pretty pointless.

Morgan ARR Allen
+1  A: 

Are you trying to write in-page javascript that will allow communication between the sidebar page and the tab page? There are restrictions on which pages can see each other and communicate:

  • If the pages are not on the same domain, they aren't allowed to talk (same-domain restriction).
  • If one page did not open the other, there is no way for either page to acquire a reference to the other.

I'm not sure how a page can request the opening of a page in the sidebar, or vice versa. But if you can manage that, use var child = window.open(...) to get a reference one direction and window.opener to get a reference the other direction.

phyzome
A page cannot open a sidebar, but a sidebar page can open a new tab, or popup with window.open like you suggested.
Morgan ARR Allen