Firefox插件开发,打开一个隐藏的网页浏览器 [英] Firefox addon development, open a hidden web browser

查看:179
本文介绍了Firefox插件开发,打开一个隐藏的网页浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Firefox插件,如何打开一个隐藏在用户面前的web浏览器, >解决方案

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


$ b

XUL附加组件插入 XUL< iframe type =content> 在某处隐藏(例如 .style.display =none; )。此外,您可能要禁用< iframe> 中的图片/插件/脚本。



假设 window 是一个XUL窗口,比如 browser.xul ,这里是一个从隐藏的< code $< 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);
让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 {
//总是删除事件侦听器和框架本身。
//在这个例子中,我们不再需要这个框架,除此之外
//所以现在就删除它。
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);



$ b当然,只要你想要, - 根据需要多次使用,等等。但是,一旦不再需要,请务必将其删除,以节省资源(内存,CPU)。将它重置为 about:blank 同时不需要也是一个不错的选择。


I am developing a Firefox addon, how can I open a web browser which is hidden from the user but I can script it from Javascript within my addon code?

解决方案

SDK users should use the page-worker module.

XUL add-ons may insert a XUL <iframe type="content"> somewhere and make it hidden (e.g. .style.display = "none";). Also, you may want to disable images/plugins/script in that <iframe>.

Assuming window is a XUL window, such as browser.xul, here is an example reading the title of a website from a hidden <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);
}

Of course, you can keep around the frame as long as you want, re-use it as many times as you want, and so on. But make sure to remove it once you don't need it anymore, to conserve resources (memory, CPU). Resetting it to about:blank while not needed might also be a good option.

这篇关于Firefox插件开发,打开一个隐藏的网页浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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