Firefox扩展:如何从加载覆盖中运行脚本

时间:2014-04-09 01:22:42

标签: javascript firefox-addon

我已经加载了扩展程序并且能够在底部显示状态。我试图将以下代码添加到chrome.manifest文件中:

chrome.manifest:

content     sample    chrome/content/
overlay chrome://browser/content/browser.xul chrome://sample/content/sample.xul

然后在sample.xul中,我有以下内容:

sample.xul:
<?xml version="1.0"?>
<overlay id="sample" 
         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
 <statusbar id="status-bar" hidden="false">
  <statusbarpanel id="my-panel" label="Hello, World v2"  />
 </statusbar>

<menubar id="main-menubar">
  <menu label="Jeff" insertbefore="tools-menu">
    <menupopup>
      <menuitem label="About"/>
    </menupopup>
  </menu>
</menubar>


<script src="onloadoverlay.js"/> 

</overlay>

我添加了菜单栏和状态栏,以确保叠加层正常工作(到目前为止)。所以从理论上讲,下面的代码应该从&#34; onloadoverlay.js&#34;当浏览器启动时,因此在页面加载时应继续加载:

onloadoverlay.js:

var myExtension = {
    init: function() {
        // The event can be DOMContentLoaded, pageshow, pagehide, load or unload.
        if(gBrowser) gBrowser.addEventListener("DOMContentLoaded", this.onPageLoad, false);
    },
    onPageLoad: function(aEvent) {
        var doc = aEvent.originalTarget; // doc is document that triggered the event
        var win = doc.defaultView; // win is the window for the doc
        // test desired conditions and do something
        // if (doc.nodeName != "#document") return; // only documents
        // if (win != win.top) return; //only top window.
        // if (win.frameElement) return; // skip iframes/frames
        alert("page is loaded \n" +doc.location.href);
    }
}
window.addEventListener("load", function load(event){
    window.removeEventListener("load", load, false); //remove listener, no longer needed
    myExtension.init();  
},false);

如果我完全错误或者我遗漏了一些简单的话,请告诉我。我已经通过谷歌和几个扩展创建演练和其他信息达到了这一点。到目前为止,我希望代码能够在页面加载时弹出一个窗口。

非常感谢帮助,

-Jeff

1 个答案:

答案 0 :(得分:1)

将脚本代码的src属性更改为chrome://sample/content/onloadoverlay.js

相关问题