FireFox扩展:如何通过jQuery访问页面元素? [英] FireFox extension: How to access page element via jQuery?

查看:119
本文介绍了FireFox扩展:如何通过jQuery访问页面元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var myExtension = {
init:function(){
//事件可以是DOMContentLoaded,pageshow,pagehide,加载或卸载。
if(gBrowser)gBrowser.addEventListener(DOMContentLoaded,this.onPageLoad,false);
},
onPageLoad:function(aEvent){
var doc = aEvent.originalTarget; // doc是触发事件的文档
var win = doc.defaultView; // win是doc
// alert的窗口(page is loaded \\\
+ doc.location.href);
// alert(doc.location.href.indexOf(facebook.com));
if(doc.location.href.indexOf(facebook.com)== -1)
{
return;
}
alert(我们在这里);
alert($(#blueBar)。html());

$ b window.addEventListener(load,function load(event){
window.removeEventListener(load,load,false); //移除侦听器,不再需要
myExtension.init();
},false);

它一直给 undefined 错误

解决方案

$()默认使用当前窗口的文档。你的情况实际上是browser.xul。你需要通过 var doc = aEvent.originalTarget; 来获得子文档,所以这应该可以工作我认为(未经测试)

  $(doc).find(#blueBar)


I am using following code:

   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
        //alert("page is loaded \n" +doc.location.href);
       // alert(doc.location.href.indexOf("facebook.com"));
        if(doc.location.href.indexOf("facebook.com") == -1) 
        {
            return;
        }
        alert("we are here");
        alert($("#blueBar").html());
    }
}
window.addEventListener("load", function load(event){
    window.removeEventListener("load", load, false); //remove listener, no longer needed
    myExtension.init();  
},false);

It keeps giving undefined error

解决方案

$() will use the document of the current window by default. Which is actually browser.xul in your case. You need to operate on the sub-document, which you already got via var doc = aEvent.originalTarget;, so this should work I think (untested)

$(doc).find("#blueBar")

这篇关于FireFox扩展:如何通过jQuery访问页面元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆