2012-05-28 34 views
3

我已閱讀NsIContentPolicy,並且已經搜索整個Stackoverflow以獲得實施NsIContentPolicy的正確教程,但都是徒勞的。我知道Adblock使用NsIContentPolicy作爲他們的主要武器。逆向工程Adblock並沒有幫助我理解如何實現NsIContentPolicy。有沒有簡單的插件使用NsIContentPolicy進行學習,或者有關NsIContentPolicy的優秀教程?Firefox插件的nsIContentPolicy示例?

回答

9

,我不知道有任何好的教程,但我可以給你一些小例子代碼:

Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 
Cu.import("resource://gre/modules/Services.jsm"); 

let policy = 
{ 
    classDescription: "Test content policy", 
    classID: Components.ID("{12345678-1234-1234-1234-123456789abc}"), 
    contractID: "@adblockplus.org/test-policy;1", 
    xpcom_categories: ["content-policy"], 

    init: function() 
    { 
    let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); 
    registrar.registerFactory(this.classID, this.classDescription, this.contractID, this); 

    let catMan = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager); 
    for each (let category in this.xpcom_categories) 
     catMan.addCategoryEntry(category, this.contractID, this.contractID, false, true); 

    onShutdown.add((function() 
    { 
     for each (let category in this.xpcom_categories) 
     catMan.deleteCategoryEntry(category, this.contractID, false); 

     // This needs to run asynchronously, see bug 753687 
     Services.tm.currentThread.dispatch(function() 
     { 
     registrar.unregisterFactory(this.classID, this); 
     }.bind(this), Ci.nsIEventTarget.DISPATCH_NORMAL); 
    }).bind(this)); 
    }, 

    // nsIContentPolicy interface implementation 
    shouldLoad: function(contentType, contentLocation, requestOrigin, node, mimeTypeGuess, extra) 
    { 
    dump("shouldLoad: " + contentType + " " + 
          (contentLocation ? contentLocation.spec : "null") + " " + 
          (requestOrigin ? requestOrigin.spec : "null") + " " + 
          node + " " + 
          mimeTypeGuess + "\n"); 
    return Ci.nsIContentPolicy.ACCEPT; 
    }, 

    shouldProcess: function(contentType, contentLocation, requestOrigin, node, mimeTypeGuess, extra) 
    { 
    dump("shouldProcess: " + contentType + " " + 
          (contentLocation ? contentLocation.spec : "null") + " " + 
          (requestOrigin ? requestOrigin.spec : "null") + " " + 
          node + " " + 
          mimeTypeGuess + "\n"); 
    return Ci.nsIContentPolicy.ACCEPT; 
    }, 

    // nsIFactory interface implementation 
    createInstance: function(outer, iid) 
    { 
    if (outer) 
     throw Cr.NS_ERROR_NO_AGGREGATION; 
    return this.QueryInterface(iid); 
    }, 

    // nsISupports interface implementation 
    QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPolicy, Ci.nsIFactory]) 
}; 

policy.init(); 

這來自我用一下與內容的政策執行問題的最低含量政策執行 - 它除了將所有內容策略調用轉儲到控制檯(window.dump documentation)外,不做任何其他操作。很明顯,在實際實施中,應將classDescription,classIDcontractID這些字段更改爲適當的。 onShutdown屬於我使用的私有框架:此擴展是無重啓的,這就是爲什麼它需要「手動」註冊組件,並且如果在瀏覽器會話期間關閉該組件,還將運行此代碼以將其刪除。

您還可以下載完整的擴展:testpolicy.xpi

+0

弗拉基米爾,那testpolicy.xpi正是我一直在尋找:)感謝很多:) –

+0

示例鏈接被破壞,進口需要更新,但仍然,這塊只有網絡內容,以及如何改變dom樹? – msangel

+0

@msangel:我更新了鏈接。這裏的代碼僅僅是一個簡單引導擴展的例子 - 如果你的擴展是基於SDK的,那麼你可以自由地調整它,以便它適用於你,但請不要編輯帖子。問題在於內容策略僅用於阻止網絡請求,DOM修改必須通過其他方式發生。 –