Firefox 侧边栏在主浏览器窗口中调用 javascript 函数
我正在尝试制作一个仅供内部使用的快速 Firefox 侧边栏。
我在理解侧边栏和主浏览器窗口如何通信方面有点困难。我想要做的就是调用仅驻留在主浏览器窗口中的现有 JavaScript 函数。
我的代码如下所示;
ff-sidebar.xul
<checkbox label="Button hover" checked="false" oncommand="add_enhance(this)"/>
ff-sidebar.js
function add_enhance(cb){
if (cb.checked) {
// this bit is wrong I know
window.content.document.NEWSTYLE.buttonHover();
}
}
所以问题是,如何调用主窗口中名为 NEWSTYLE.buttonHover()
的函数?
I'm trying to put together a quick Firefox sidebar for internal use only.
I'm struggling a bit in understanding how sidebar and main browser window communicate. What I want to do exactly is call existing javascript functions that reside in the main browser window only.
My code looks like this;
ff-sidebar.xul
<checkbox label="Button hover" checked="false" oncommand="add_enhance(this)"/>
ff-sidebar.js
function add_enhance(cb){
if (cb.checked) {
// this bit is wrong I know
window.content.document.NEWSTYLE.buttonHover();
}
}
So the question is, how do I call a function called NEWSTYLE.buttonHover()
that lives in the main window?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
理论上,这应该可行:
window.content 指向浏览器内容窗口,并且变量 NEWSTYLE 在此窗口上定义。实际上,由于安全机制,事情有点混乱 - 特权代码无法直接访问非特权代码的对象。相反,您可以通过 XPCNativeWrapper 访问它们(请参阅 https://developer.mozilla.org/en/XPCNativeWrapper)。 Firefox 4 中的技术细节有所改变,但本质上还是一样的。
在不引入安全问题的情况下执行您想要的操作的最简单方法是将内容窗口的位置更改为 javascript: URL。像这样:
您将无法获得此函数的结果,但看起来您不需要它。
Theoretically, this should work:
window.content points to the browser content window and the variable NEWSTYLE is defined on this window. In reality things are a bit more messy due to security mechanisms - privileged code cannot access objects of unprivileged code directly. Instead you get to access them through XPCNativeWrapper (see https://developer.mozilla.org/en/XPCNativeWrapper). Technical details changed somewhat in Firefox 4 but essentially it is still the same.
The easiest way to do what you want without introducing security issues is changing the location of the content window to a javascript: URL. Like this:
You won't be able to get the result of this function but it doesn't look like you need it.