Firefox 侧边栏和 DOM 的 Document 对象

发布于 2024-07-06 11:28:19 字数 404 浏览 5 评论 0原文

Firefox 侧边栏中加载了一个网页,主文档中加载了另一个网页。 现在,我如何通过 Firefox 侧边栏访问主文档对象? 通过 Firefox 侧边栏文档中的 Javascript 代码访问主文档来执行此操作的示例会很有帮助。

感谢您的回答。 不过,我必须完善我的问题。 主窗口已加载一些网页,侧边栏也有一个网页。 我希望侧边栏窗口知道当单击侧边栏窗口上的链接时用户在主窗口上选择了哪些文本。 我知道如何从窗口中获取选定的文本。 只是侧边栏元素增加了我无法超越的问题的复杂性。

@PConory:

我喜欢你的答案,但是当我尝试时出现错误:

错误:为类对象创建包装器的权限被拒绝 未命名类。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满地尘埃落定 2024-07-13 11:28:19

据我所知,您实际上是在侧边栏中加载一个网站(选中“在侧边栏中加载此书签”)。 如果是这种情况,并且侧边栏正在打开主窗口页面。 您可以使用 window.postMessage 在它们之间进行通信。 但就像我说的,侧边栏页面必须打开主页,因为您需要窗口引用才能发布消息。

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

使用此功能,您仍然无法访问该文档,但可以在它们之间进行通信并编写您想要执行的任何更改的脚本。

编辑:再考虑一下,如果您从侧边栏打开窗口,您将拥有它的 DOM。 var newwin = window.open('blah'); newwin.document 使得 postMessage 的漏洞变得毫无意义。

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.

只是一片海 2024-07-13 11:28:19

从侧边栏访问主窗口比以其他方式返回要棘手得多。

您需要遍历的 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); 

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); 
只是一片海 2024-07-13 11:28:19

您是否正在尝试编写页内 JavaScript 以允许侧边栏页面和选项卡页面之间进行通信? 哪些页面可以互相看到并进行通信是有限制的:

  • 如果页面不在同一域中,则不允许它们进行通信(同域限制)。
  • 如果一个页面没有打开另一个页面,则任一页面都无法获取对另一个页面的引用。

我不确定页面如何请求在侧边栏中打开页面,反之亦然。 但是如果您可以做到这一点,请使用var child = window.open(...)来获取一个方向的参考和window.opener以获得另一个方向的参考。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文