从选项卡加载事件 MDN 获取原始目标

Get originalTarget from tab load event MDN

本文关键字:获取 原始 目标 MDN 事件 选项 加载      更新时间:2023-09-26

我正在尝试按照此示例获取原始目标 DOM:

gBrowser.addEventListener("load", function(aEvent){ s.handleLoadBrowser(aEvent);},     true);
handleLoadBrowser : function (aEvent){
    var w = aEvent.originalTarget.defaultView;
}

我为选项卡加载创建了一个事件侦听器:

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

在这里我得到错误:"类型错误:win未定义"。

如何从选项卡事件加载中获取此 dom 对象?

关于你的第一个例子,它应该有效: 我做了类似的事: 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)