打开 xul 文件以响应工具栏扩展按钮单击
我目前正在构建我的第一个 Firefox 扩展,并且在其中一个功能上遇到了一些困难。 我想打开一个新的浏览器选项卡来响应工具栏上的按钮单击。 新选项卡应包含网页的内容以及一些额外的按钮。
目前,我已经为新选项卡的内容创建了一个单独的 xul 文件:
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<window id="myapp-report-window"
title="Example 4.5.1"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/x-javascript"
src="chrome://myapp/content/main.js" />
<toolbox>
<toolbar id="nav-toolbar">
<toolbarbutton label="This-is-going-to-do-some-stuff"/>
</toolbar>
</toolbox>
<iframe id="myapp-report-frame" flex="1"/>
<script type="text/javascript">
function loadPage(url){
document.getElementById('myapp-report-frame').setAttribute('src',url);
}
</script>
</window>
这个 xul 文件是通过此 javascript 启动的,从主 myapptoolbar.xul 引用:
gBrowser.selectedTab = gBrowser.addTab('chrome://myapp/content/report.xul');
var newTabBrowser = gBrowser.getBrowserForTab(gBrowser.selectedTab);
newTabBrowser.addEventListener("load",
function(){
loadPage('http://www.somedynamicallysetwebsite.com');
}, true);
我遇到的问题是 loadPage 函数不是被发现,所以 iframe 的 src 属性永远不会被设置。 我确信这是一些愚蠢的范围问题,但我对 Firefox 扩展非常陌生(第 2 天!),所以任何帮助将不胜感激。
I'm currently building my first Firefox extension, and am having a little difficulty with one piece of functionality. I'd like to open a new browser tab in response to a button click on the toolbar. The new tab should contain the contents of a webpage, together with some extra buttons.
At the moment I've created a separate xul file for the contents of the new tab:
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<window id="myapp-report-window"
title="Example 4.5.1"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/x-javascript"
src="chrome://myapp/content/main.js" />
<toolbox>
<toolbar id="nav-toolbar">
<toolbarbutton label="This-is-going-to-do-some-stuff"/>
</toolbar>
</toolbox>
<iframe id="myapp-report-frame" flex="1"/>
<script type="text/javascript">
function loadPage(url){
document.getElementById('myapp-report-frame').setAttribute('src',url);
}
</script>
</window>
This xul file is launched via this javascript, referenced from the main myapptoolbar.xul:
gBrowser.selectedTab = gBrowser.addTab('chrome://myapp/content/report.xul');
var newTabBrowser = gBrowser.getBrowserForTab(gBrowser.selectedTab);
newTabBrowser.addEventListener("load",
function(){
loadPage('http://www.somedynamicallysetwebsite.com');
}, true);
The problem that I'm having is that the loadPage function is not being found, so the src attribute of the iframe is never set. I'm sure it's some silly scoping problem, but I'm very new to firefox extensions (day 2!) so any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我自己对此很陌生。 实际上,我偶然回到这里,因为我想问另一个有关 Firefox 扩展的问题,但我想我也许可以提供帮助。
嗯,您的 loadpage 函数是在 newtab.xul 中声明的,因此无法从 myapptoolbar.xul 调用该函数。 我认为 XUL 文件是具有增强权限的 HTML 文件,因此具有相似的属性。 您可以在 app.js 中声明该函数,然后让它们都包含 app.js 文件,如下所示:-
将其放在顶部的某个位置,紧接在there.is.only.xul 行之后会很好。 只需执行通常对 HTML 页面执行的操作即可。
我希望这有帮助。 =) 祝您延期顺利!
I am new to this myself. Actually stumbled back here because I was trying to ask another question about firefox extension but I think I might be able to help.
Um, your loadpage function is declared within newtab.xul and therefore, the function can't be called from myapptoolbar.xul. I think XUL files are HTML files with enhanced privilege and hence share similar properties. You can declare the function within an app.js and then have the both of them include the app.js file like this:-
Place it somewhere at the top, right after the there.is.only.xul line would be nice. Just do things like what you'll normally do to a HTML page.
I hope this helps. =) Good luck on your extension!
我最终解决了这个问题——这是一个范围问题。 我必须通过 GBrowser 属性访问 iframe。
I fixed this in the end - it was a scope issue. I had to access the iframe via the GBrowser property.