0

私はhttps://developer.mozilla.org/en/XUL_School/Intercepting_Page_Loadsを読んでハッキングしてきましたが、必要なことを実行しているように見えます。

私はChromelessに取り組んでおり、メインのxulbrowser要素が離れてナビゲートされないようにしようとしています。たとえば、リンクは機能しないはずwindow.location.href="http://www.example.com/"です。

経由browser.webProgress.addProgressListenerでこれを実行してから聞くことができると思いますが、リソース要求と場所の変更onProgressChangeを区別する方法がわかりません(ドキュメントがすでにアンロードされているため、遅すぎるようです)。browseronLocationChange

browser.webProgress.addProgressListener({
    onLocationChange: function(){},
    onStatusChange: function(){},
    onStateChange: function(){},
    onSecurityChange: function(){},
    onProgressChange: function(){
        aRequest.QueryInterface(Components.interfaces.nsIHttpChannel)
        if( /* need to check if the object triggering the event is the xulbrowser */ ){
            aRequest.cancel(Components.results.NS_BINDING_ABORTED);
        }
    },
    QueryInterface: xpcom.utils.generateQI([Ci.nsIWebProgressListener, Ci.nsISupportsWeakReference])
}, wo._browser.webProgress.NOTIFY_ALL);

有望に聞こえるもう1つのオプションはnsIContentPolicy.shouldLoad()メソッドですが、「nsIContentPolicyを拡張するXPCOMコンポーネントを作成し、nsICategoryManagerを使用して「content-policy」カテゴリに登録する」方法がわかりません。

何か案は?

4

1 に答える 1

0

これについては、mozilla の #xulrunner irc チャンネルから助けてもらいました。

結果のソリューションは次のとおりです。

注: これは Mozilla Chromeless で使用するためのモジュールです。通常の状況では、require("chrome")およびrequire("xpcom")ビットは使用できません。

const {Cc, Ci, Cu, Cm, Cr} = require("chrome");

const xpcom = require("xpcom");

/***********************************************************
class definition
***********************************************************/

var description = "Chromeless Policy XPCOM Component";
/* UID generated by http://www.famkruithof.net/uuid/uuidgen */
var classID = Components.ID("{2e946f14-72d5-42f3-95b7-4907c676cf2b}");
// I just made this up. Don't know if I'm supposed to do that.
var contractID = "@mozilla.org/chromeless-policy;1";

//class constructor
function ChromelessPolicy() {
    //this.wrappedJSObject = this;
}

// class definition
var ChromelessPolicy = {

  // properties required for XPCOM registration:
  classDescription: description,
  classID:          classID,
  contractID:       contractID,
  xpcom_categories: ["content-policy"],

  // QueryInterface implementation
  QueryInterface: xpcom.utils.generateQI([Ci.nsIContentPolicy,
    Ci.nsIFactory, Ci.nsISupportsWeakReference]),

  // ...component implementation...
  shouldLoad : function(aContentType, aContentLocation, aRequestOrigin, aContext, aMimeTypeGuess, aExtra) {
    let result = Ci.nsIContentPolicy.ACCEPT;
    // only filter DOCUMENTs (not SUB_DOCUMENTs, like iframes)
    if( aContentType === Ci.nsIContentPolicy["TYPE_DOCUMENT"]
        // block http(s) protocols...
        && /^http(s):/.test(aContentLocation.spec) ){
        // make sure we deny the request now
        result = Ci.nsIContentPolicy.REJECT_REQUEST;
    }
    // continue loading...
    return result;
  },
  createInstance: function(outer, iid) {
    if (outer)
        throw Cr.NS_ERROR_NO_AGGREGATION;
    return this.QueryInterface(iid);
  }
};

let registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
try
{
    Cm.nsIComponentRegistrar.registerFactory(classID, description, contractID, ChromelessPolicy);
}
catch (e) {
    // Don't stop on errors - the factory might already be registered
    Cu.reportError(e);
}

const categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
for each (let category in ChromelessPolicy.xpcom_categories) {
    categoryManager.addCategoryEntry(category, ChromelessPolicy.classDescription, ChromelessPolicy.contractID, false, true);
}

興味のある方は github のプル リクエスト: https://github.com/mozilla/chromeless/pull/114

于 2011-05-27T21:21:43.267 に答える