从tab load event MDN获取originalTarget

时间:2014-12-16 12:55:55

标签: javascript firefox web firefox-addon

我正在尝试使用此示例获取originalTarget DOM:

gBrowser.addEventListener("load", function(aEvent){ s.handleLoadBrowser(aEvent);},     true);

handleLoadBrowser : function (aEvent){
    var w = aEvent.originalTarget.defaultView;
}

我为tab load创建了一个事件监听器:

var tab = gBrowser.addTab("www.google.com");
tab.addEventListener("load", function(aEvent){ s.handleLoadTab(aEvent) }, true);

handleLoadTab : function (aEvent){
    var w = aEvent.originalTarget.defaultView;
} 

这里我收到错误:“TypeError:win is undefined”。

如何从标签事件加载中获取此dom对象?

1 个答案:

答案 0 :(得分:0)

关于你的第一个例子,它应该有效:我做了类似的事:https://gist.github.com/Noitidart/9287185#file-bootstrap-js-L98我唯一的区别就是我这样做:gBrowser.addEventListener("load", s.handleLoadBrowser, true);

关于第二个例子:var tab = gBrowser.addTab("www.google.com"); tab.addEventListener("load", function(aEvent){ s.handleLoadTab(aEvent) }, true);你不能这样做,你不要向标签添加加载事件,标签是你抓住并移动的xul元素。您可以执行tab.linkedBrowser.contentWindow.addEventListener('load'但在第一页加载后它将不再存在。

此代码适用于我:

var tab = gBrowser.addTab("data:text/html,<b>hi</b>");
tab.linkedBrowser.contentWindow.addEventListener("load", handleLoadTab, true);
//tab.linkedBrowser.contentWindow.addEventListener("DOMContentLoaded", handleLoadTab, true);

function handleLoadTab(aEvent){
    var w = aEvent.originalTarget.defaultView;
  w.alert('load done')
} 

此处还有另一个如何收听第一个加载一个标签的例子。我在此示例中使用gBrowser.loadOneTab,但可与addTab互换:https://gist.github.com/Noitidart/0f076070bc77abd5e406

var tab = gBrowser.loadOneTab('data:text/html,<span class="profilist-build-icon-1">backround of this span is of icon on desktop</span><input type="button" value="applyCss"><input type="button" value="removeCss"> Change File on Desktop to: <input type="button" value="Release Img"><input type="button" value="Beta Img"><input type="button" value="Aurora Img"><input type="button" value="Nightly Img">', {inBackground:false});
    var mobs = new window.MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            var attrVal = tab.getAttribute(mutation.attributeName);
            console.log(mutation.attributeName, attrVal);
            if (mutation.attributeName == 'progress' && attrVal == 'true') {
              //at this point can addEventListener for DOMConentLoaded and it will as document hasnt loaded yet
              tab.linkedBrowser.contentDocument.addEventListener('DOMContentLoaded', function() {
                tab.linkedBrowser.contentDocument.removeEventListener('DOMContentLoaded', arguments.callee, false);
                alert('loadOneTab finished loading', tab.linkedBrowser.contentDocument.body.innerHTML)
              }, false);
              mobs.disconnect();
            }
          /*
            if (mutation.attributeName == 'progress' && attrVal == '') {
             //cannot add addEventListener for DOMConentLoaded here as document is already loaded, it will never fire
              alert('tab done loading');
              mobs.disconnect();
            }
           */
        });
    });
    mobs.observe(tab, {
        attributes: true
    });

console.log(tab._fullyOpen)