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

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

<p>@PConory:</p>

<p>I like your answer, but when I try it there is an error:</p>

<blockquote>
  <p>Error: Permission denied to create wrapper for object of class
  UnnamedClass.</p>
</blockquote>

<p>Thanks.</p>

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);
```
```