0

ブラウザ拡張機能でバックグラウンド Web ページを読み込むための「シークレット モード」を構築することは可能ですか?

ユーザーに代わって Web ページを定期的にチェックする非 IE クロスブラウザー拡張機能を作成しています。次の 2 つの要件があります。

  1. ページ チェックはバックグラウンドで行われ、できるだけ目立たないようにします。これは、フォーカスされていない新しいブラウザー タブでページを開くか、拡張機能のバックグラウンド ページのサンドボックス化された iframe に非表示にすることで実行できると思います。
  2. ページ チェックは「シークレット モード」で動作する必要があり、ユーザーの Cookie、履歴、またはローカル ストレージを使用または更新しないでください。これは、ユーザーの実際のブラウジング動作を可能な限り汚染するチェックを停止するためです。

この「シークレットモード」を実装する方法について何か考えはありますか?

可能な限り多くの種類のブラウザー (IE 以外) で動作することが理想的です。

私の現在のアイデアは次のとおりです。

  • ページ チェックに関連する着信/発信 http 要求から Cookie ヘッダーを除外します (これらすべてを特定できる場合) (Safari では不可能ですか?)
  • 各ページをチェックした後、ユーザーの履歴からそのページを除外します。

私が見つけた有用なSOの質問:

4

1 に答える 1

1
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);

追加した iframe へのグローバル var 参照を保持する必要があります。次に、このようにiframeの場所を変更できます。ロードされると、上記のイベントリスナーがトリガーされます

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

その DOMContentLoaded は、その iframe にロードされたすべてのものを識別します。ページにフレームがある場合は、それも検出されます。

履歴から削除するには、DOMContentLoaded 関数に履歴サービスを使用して履歴から win.location を削除します: https://developer.mozilla.org/en-US/docs/Using_the_Places_history_service

そのページのリクエストから Cookie を削除するには、次のコードを使用します。

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

}

于 2014-03-15T20:40:01.307 に答える