Firefox Sidebar and DOM Document Object

The firefox sidebar is loaded with a webpage and another webpage loaded into the main document. Now, how do I access the main document object through the Firefox sidebar? An example of this is the Javascript code in a firefox sidebar document to access the main document.

Thanks for answers. However, I have to clarify my question. A web page is loaded in the main window, and there is a web page in the sidebar. I want the sidebar window to know what text the user has selected in the main window when a link is clicked in the sidebar window. I know how to get selected text from a window. Only that sidebar element adds complexity to a problem that I cannot surpass.

@PConory:

I like your answer, but when I try, an error occurs:

Error: Permission denied to create a wrapper for an object of class UnnamedClass.

Thank.

+3
source share
3 answers

As far as I can tell, you are actually loading the website in the sidebar (check "Download this bookmark in the sidebar"). If so, and if the sidebar opens the page of the main window. You can use window.postMessage to communicate between them. But, as I said, the sidebar page should open the main page, because you need a link to the window to post a 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 you can link between them and the script with any changes you want to make.

: , , DOM. var newwin = window.open('blah'); newwin.document postMessage .

+2

, .

DOM , Mozilla , :

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

mainWindow:

var mWin = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
               .getInterface(Components.interfaces.nsIWebNavigation)
               .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
               .rootTreeItem
               .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
               .getInterface(Components.interfaces.nsIDOMWindow); 
+1

javascript, ? , :

  • , ( ).
  • , .

I'm not sure how a page can request a page to open in the sidebar or vice versa. But , if you can do this, use var child = window.open(...)to get the link in one direction and window.openerto get the link in the other direction.

+1
source

Source: https://habr.com/ru/post/1697623/


All Articles