Firefox插件开发,打开隐藏的Web浏览器

时间:2013-11-22 11:36:16

标签: javascript firefox firefox-addon

我正在开发一个Firefox插件,如何打开一个对用户隐藏的Web浏览器,但我可以在我的插件代码中用Javascript编写脚本?

1 个答案:

答案 0 :(得分:2)

SDK用户应使用page-worker模块。

XUL加载项可能会在某处插入XUL <iframe type="content">并隐藏它(例如.style.display = "none";)。此外,您可能希望禁用<iframe>中的图像/插件/脚本。

假设window是一个XUL窗口,例如browser.xul,这里是一个从隐藏的<iframe>读取网站标题的示例:

function readTitleFromPage(uri, callback) {
    callback = callback || function() {};

    let frame = document.createElement("iframe");
    frame.setAttribute("type", "content");
    frame.style.display = "none";
    document.documentElement.appendChild(frame);
    let docShell = frame.contentWindow.
                   QueryInterface(Ci.nsIInterfaceRequestor).
                   getInterface(Ci.nsIWebNavigation).
                   QueryInterface(Ci.nsIDocShell);
    docShell.allowImages = false;
    docShell.allowPlugins = false;
    frame.setAttribute("src", uri);
    let load = function load(e) {
        try {
            if (e.type == "load") {
                callback(frame.contentDocument.title);
            }
            else {
                callback(null);
            }
        }
        finally {
            // Always remove event listeners and the frame itself, at some point.
            // In this example, we don't need the frame anymore, beyond this point,
            // so remove it now.
            frame.removeEventListener("load", load, false);
            frame.removeEventListener("error", load, false);
            frame.removeEventListener("abort", load, false);
            frame.parentElement.removeChild(frame);
        }
    };
    frame.addEventListener("load", load, false);
    frame.addEventListener("error", load, false);
    frame.addEventListener("abort", load, false);
}

当然,您可以根据需要随时保留框架,可以根据需要多次重复使用,依此类推。但是一旦不再需要它,请务必将其删除,以节省资源(内存,CPU)。在不需要的情况下将其重置为about:blank也可能是一个不错的选择。