Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从基于xul的firefox扩展在隐藏的iframe中加载多个页面_Iframe_Firefox Addon_Xul_Onload_Domcontentloaded - Fatal编程技术网

从基于xul的firefox扩展在隐藏的iframe中加载多个页面

从基于xul的firefox扩展在隐藏的iframe中加载多个页面,iframe,firefox-addon,xul,onload,domcontentloaded,Iframe,Firefox Addon,Xul,Onload,Domcontentloaded,从基于xul的firefox插件中,我需要: 以编程方式创建不可见的iframe(一次) 在加载项运行时重用它以加载多个URL 在加载每个URL后访问返回的HTML 问题:我只能为任何创建的iframe获取第一页加载,以触发“onload”或“DOMContentLoaded”事件。对于后续URL,不会触发任何事件 注意:如果可能的话,我也可以使用hiddenDOMWindow本身 代码: 有一些陷阱: 不同平台的hiddenWindow不同。它是Mac上的XUL和HTML 您应该使用.set

从基于xul的firefox插件中,我需要:

  • 以编程方式创建不可见的iframe(一次)
  • 在加载项运行时重用它以加载多个URL
  • 在加载每个URL后访问返回的HTML
  • 问题:我只能为任何创建的iframe获取第一页加载,以触发“onload”或“DOMContentLoaded”事件。对于后续URL,不会触发任何事件

    注意:如果可能的话,我也可以使用hiddenDOMWindow本身

    代码:

    有一些陷阱:

    • 不同平台的
      hiddenWindow
      不同。它是Mac上的XUL和HTML
    • 您应该使用
      .setAttribute(“src”,url)以可靠地导航
    以下内容适用于我(Mac、Win7):


    不要重新加载
    隐藏窗口
    本身,否则您将破坏许多其他代码。

    请发布您的代码。
    var urls = ['http://en.wikipedia.org/wiki/Internet', 'http://en.wikipedia.org/wiki/IPv4', 'http://en.wikipedia.org/wiki/Multicast' ];
    
    visitPage(urls.pop());
    
    function visitPage(url) {
    
        var XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
    
        var hiddenWindow = Components.classes["@mozilla.org/appshell/appShellService;1"].getService
            (Components.interfaces.nsIAppShellService).hiddenDOMWindow;
    
        var doc = hiddenWindow.document, iframe = doc.getElementById("my-iframe");
    
        if (!iframe) 
        {
            iframe = doc.createElement("iframe");
            //OR: iframe = doc.createElementNS(XUL_NS,"iframe");
    
            iframe.setAttribute("id", "my-iframe");
            iframe.setAttribute('style', 'display: none');
    
            iframe.addEventListener("DOMContentLoaded", function (e) { 
                dump('DOMContentLoaded: '+e.originalTarget.location.href);
                visitPage(urls.pop()); 
            });
    
            doc.documentElement.appendChild(iframe);
        }
    
        iframe.src = url;    
    }
    
    var urls = [
        'http://en.wikipedia.org/wiki/Internet',
        'http://en.wikipedia.org/wiki/IPv4',
        'http://en.wikipedia.org/wiki/Multicast'
    ];
    
    var hiddenWindow = Components.classes["@mozilla.org/appshell/appShellService;1"].
                       getService(Components.interfaces.nsIAppShellService).
                       hiddenDOMWindow;
    
    function visitPage(url) {
        var iframe = hiddenWindow.document.getElementById("my-iframe");
        if (!iframe) {
            // Always use html. The hidden window might be XUL (Mac)
            // or just html (other platforms).
            iframe = hiddenWindow.document.
                     createElementNS("http://www.w3.org/1999/xhtml", "iframe");
            iframe.setAttribute("id", "my-iframe");
            iframe.addEventListener("DOMContentLoaded", function (e) {
                console.log("DOMContentLoaded: " +
                            e.originalTarget.location);
                var u = urls.pop();
                // Make sure there actually was something left to load.
                if (u) {
                    visitPage(u);
                }
            });
            hiddenWindow.document.documentElement.appendChild(iframe);
        }
        // Use .setAttribute() to reliably navigate the iframe.
        iframe.setAttribute("src", url);
    }
    
    visitPage(urls.pop());