Chrome / FF / Safari扩展:以隐身模式加载隐藏的网页 [英] Chrome/FF/Safari extension: Load hidden web page in incognito-like mode

查看:200
本文介绍了Chrome / FF / Safari扩展:以隐身模式加载隐藏的网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在浏览器扩展中建立一个隐身模式来加载后台网页吗? 非IE浏览器的跨浏览器扩展,代表用户定期检查网页。有两个要求:


  1. 页面检查在后台完成,尽可能不显眼。我相信这可以通过在新的未聚焦的浏览器选项卡中打开页面,或隐藏在扩展的后台页面中的沙盒iframe中来完成。

  2. 页面检查应以隐身模式 ,而不是使用/更新用户的Cookie,历史记录或本地存储。这是为了停止检查,尽可能多地污染用户的实际浏览行为。

有关如何执行此操作的任何想法'隐身模式'?



理想情况下,尽可能多的浏览器类型(不是IE)。

我现在的想法是:


  • 过滤掉与页面检查关联的传入/可以识别所有这些)(在Safari中不可能)

  • 在每次页面检查之后,从用户的历史记录中筛选页面。

    我发现的实用问题:


    确定源自hiddenDOMWindow h2_lin>解决方案

      var Cu = Components.utils; 
    Cu.import('resource://gre/modules/Services.jsm');
    Cu.import('resource://gre/modules/devtools/Console.jsm');
    var win = Services.appShell.hiddenDOMWindow
    var iframe = win.document.createElementNS('http://www.w3.org/1999/xhtml','iframe');
    iframe.addEventListener('DOMContentLoaded',function(e){
    var win = e.originalTarget.defaultView;
    console.log('done loaded',e.document.location);
    if(win.frameElement&&& win.frameElement!= iframe){
    //在载入
    的iframe中的一个框架}
    },false);

    win.document.documentElement.appendChild(iframe);

    必须对我们添加的iframe保留一个全局变量引用。
    ,那么你可以像这样改变iframe的位置,当它被加载的时候触发上面的事件监听器

      iframe。 contentContainer ='http://www.bing.com/'

    DOMContentLoaded标识所有东西加载在该iframe中。如果页面有帧,它也检测到。

    从历史记录中删除,进入DOMContentLoaded函数使用历史记录服务从历史记录中删除win.location:
    https://developer.mozilla.org/zh-CN/docs/Using_the_Places_history_service



    现在可以从该页面的请求中去除cookies,使用以下代码:

      const {类:Cc,构造函数:CC,接口:Ci,utils:Cu,结果:Cr,manager:Cm} =组件; 
    Cu.import('resource://gre/modules/Services.jsm');

    var myTabToSpoofIn = Services.wm.getMostRecentBrowser('navigator:browser')。gBrowser.tabContainer [0]; //会在浏览器的第一个选项卡中进行欺骗

    var httpRequestObserver = {
    观察:函数(主题,主题,数据){
    var httpChannel,requestURL;

    if(topic ==http-on-modify-request){
    httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
    var goodies = loadContextGoodies(httpChannel)
    if(goodies){
    if(goodies.contentWindow.top == iframe.contentWindow.top){
    httpChannel.setRequestHeader('Cookie ',',false);
    } else {
    //这个页面加载不在我们的iframe中,所以忽略它
    }
    }
    }
    }
    };

    Services.obs.addObserver(httpRequestObserver,http-on-modify-request,false);
    //Services.obs.removeObserver(httpRequestObserver,http-on-modify-request,false); //运行这个你的插件shudown,否则观察员雄鹿注册







    //这个函数从httpChannel的loadContext获取contentWindow和其他好东西
    函数loadContextGoodies(httpChannel){
    // httpChannel必须是http-on-modify-request的主题,像在线完成一样,返回到nsiHTTPChannel 8httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
    //启动loadContext的东西
    var loadContext;
    尝试{
    var interfaceRequestor = httpChannel.notificationCallbacks.QueryInterface(Ci.nsIInterfaceRequestor);
    // var DOMWindow = interfaceRequestor.getInterface(Components.interfaces.nsIDOMWindow); //不再做,因为:https://developer.mozilla.org/en-US/docs/Updating_extensions_for_Firefox_3.5#Getting_a_load_context_from_a_request //改为在
    下面加载loadContext试试{
    loadContext = interfaceRequestor.getInterface(Ci.nsILoadContext);
    } catch(ex){
    try {
    loadContext = subject.loadGroup.notificationCallbacks.getInterface(Ci.nsILoadContext); (!loadContext){
    // no load context(
    )catch(ex2){}
    }
    } catch(ex0){}

    所以不要做任何事情,尽管你可以运行这个,这是你的旧代码
    //这可能意味着它加载一个ajax调用或像谷歌广告的事情
    返回null;
    } else {
    var contentWindow = loadContext.associatedWindow;
    if(!contentWindow){
    //这个频道没有窗口,它可能会加载资源
    //这可能意味着它加载了一个ajax调用或者像一个谷歌广告
    返回null;
    } else {
    var aDOMWindow = contentWindow.top.QueryInterface(Ci.nsIInterfaceRequestor)
    .getInterface(Ci.nsIWebNavigation)
    .QueryInterface(Ci.nsIDocShellTreeItem)
    .rootTreeItem
    .QueryInterface(Ci.nsIInterfaceRequestor)
    .getInterface(Ci.nsIDOMWindow);
    var gBrowser = aDOMWindow.gBrowser;
    var aTab = gBrowser._getTabForContentWindow(contentWindow.top); //这是可点击的选项卡xul元素,在firefox窗口的标签栏中找到,aTab.linkedBrowser与浏览器var上面相同//可以风格化选项卡,如aTab.style.backgroundColor ='blue'; //可以将选项卡风格化为aTab.style.fontColor ='red';
    var browser = aTab.linkedBrowser; //这是选项卡中的浏览器//这是前一节中的示例结束的位置
    return {
    aDOMWindow:aDWindWindow,
    gBrowser:gBrowser,
    aTab:aTab ,
    browser:browser,
    contentWindow:contentWindow
    };


    // end loadContext


    $ p
    $ b

    }


    Is it possible to build an 'incognito mode' for loading background web-pages in a browser extension?

    I am writing a non-IE cross-browser extension that periodically checks web-pages on the user's behalf. There are two requirements:

    1. Page checks are done in the background, to be as unobtrusive as possible. I believe this could be done by opening the page in a new unfocussed browser tab, or hidden in a sandboxed iframe in the extension's background page.
    2. The page checks should operate in 'incognito mode', and not use/update the user's cookies, history, or local storage. This is to stop the checks polluting the user's actual browsing behavior as much as possible.

    Any thoughts on how to implement this 'incognito mode'?

    It would ideally work in as many browser types as possible (not IE).

    My current ideas are:

    • Filter out cookie headers from incoming/outgoing http requests associated with the page checks (if I can identify all of these) (not possible in Safari?)
    • After each page check, filter out the page from the user's history.

    Useful SO questions I've found:

    解决方案

    var Cu = Components.utils;
    Cu.import('resource://gre/modules/Services.jsm');
    Cu.import('resource://gre/modules/devtools/Console.jsm');
    var win = Services.appShell.hiddenDOMWindow
    var iframe = win.document.createElementNS('http://www.w3.org/1999/xhtml', 'iframe');
    iframe.addEventListener('DOMContentLoaded', function(e) {
        var win = e.originalTarget.defaultView;
        console.log('done loaded', e.document.location);
        if (win.frameElement && win.frameElement != iframe) {
                //its a frame in the in iframe that load
        }
    }, false);
    
    win.document.documentElement.appendChild(iframe);
    

    must keep a global var reference to iframe we added. then you can change the iframe location like this, and when its loaded it triggers the event listener above

    iframe.contentWindow.location = 'http://www.bing.com/'
    

    that DOMContentLoaded identifies all things loaded in that iframe. if the page has frames it detects that too.

    to remove from history, into the DOMContentLoaded function use the history service to remove win.location from history: https://developer.mozilla.org/en-US/docs/Using_the_Places_history_service

    now to strip the cookies from requests in that page use this code:

    const {classes: Cc, Constructor: CC, interfaces: Ci, utils: Cu, results: Cr, manager: Cm} = Components;
    Cu.import('resource://gre/modules/Services.jsm');
    
    var myTabToSpoofIn = Services.wm.getMostRecentBrowser('navigator:browser').gBrowser.tabContainer[0]; //will spoof in the first tab of your browser
    
    var httpRequestObserver = {
        observe: function (subject, topic, data) {
            var httpChannel, requestURL;
    
            if (topic == "http-on-modify-request") {
                httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
                var goodies = loadContextGoodies(httpChannel)
                if (goodies) {
                  if (goodies.contentWindow.top == iframe.contentWindow.top) {
                    httpChannel.setRequestHeader('Cookie', '', false);
                  } else {
                    //this page load isnt in our iframe so ignore it
                  }
                }
            }
        }
    };
    
    Services.obs.addObserver(httpRequestObserver, "http-on-modify-request", false);
    //Services.obs.removeObserver(httpRequestObserver, "http-on-modify-request", false); //run this on shudown of your addon otherwise the observer stags registerd
    
    
    
    
    
    
    
    //this function gets the contentWindow and other good stuff from loadContext of httpChannel
    function loadContextGoodies(httpChannel) {
        //httpChannel must be the subject of http-on-modify-request QI'ed to nsiHTTPChannel as is done on line 8 "httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);"
        //start loadContext stuff
        var loadContext;
        try {
            var interfaceRequestor = httpChannel.notificationCallbacks.QueryInterface(Ci.nsIInterfaceRequestor);
            //var DOMWindow = interfaceRequestor.getInterface(Components.interfaces.nsIDOMWindow); //not to be done anymore because: https://developer.mozilla.org/en-US/docs/Updating_extensions_for_Firefox_3.5#Getting_a_load_context_from_a_request //instead do the loadContext stuff below
            try {
                loadContext = interfaceRequestor.getInterface(Ci.nsILoadContext);
            } catch (ex) {
                try {
                    loadContext = subject.loadGroup.notificationCallbacks.getInterface(Ci.nsILoadContext);
                } catch (ex2) {}
            }
        } catch (ex0) {}
    
    if (!loadContext) {
        //no load context so dont do anything although you can run this, which is your old code
        //this probably means that its loading an ajax call or like a google ad thing
        return null;
    } else {
        var contentWindow = loadContext.associatedWindow;
        if (!contentWindow) {
            //this channel does not have a window, its probably loading a resource
            //this probably means that its loading an ajax call or like a google ad thing
            return null;
        } else {
            var aDOMWindow = contentWindow.top.QueryInterface(Ci.nsIInterfaceRequestor)
                .getInterface(Ci.nsIWebNavigation)
                .QueryInterface(Ci.nsIDocShellTreeItem)
                .rootTreeItem
                .QueryInterface(Ci.nsIInterfaceRequestor)
                .getInterface(Ci.nsIDOMWindow);
            var gBrowser = aDOMWindow.gBrowser;
            var aTab = gBrowser._getTabForContentWindow(contentWindow.top); //this is the clickable tab xul element, the one found in the tab strip of the firefox window, aTab.linkedBrowser is same as browser var above //can stylize tab like aTab.style.backgroundColor = 'blue'; //can stylize the tab like aTab.style.fontColor = 'red';
            var browser = aTab.linkedBrowser; //this is the browser within the tab //this is where the example in the previous section ends
            return {
                aDOMWindow: aDOMWindow,
                gBrowser: gBrowser,
                aTab: aTab,
                browser: browser,
                contentWindow: contentWindow
            };
        }
    }
    //end loadContext stuff
    

    }

    这篇关于Chrome / FF / Safari扩展:以隐身模式加载隐藏的网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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