我已閱讀NsIContentPolicy,並且已經搜索整個Stackoverflow以獲得實施NsIContentPolicy的正確教程,但都是徒勞的。我知道Adblock使用NsIContentPolicy作爲他們的主要武器。逆向工程Adblock並沒有幫助我理解如何實現NsIContentPolicy。有沒有簡單的插件使用NsIContentPolicy進行學習,或者有關NsIContentPolicy的優秀教程?Firefox插件的nsIContentPolicy示例?
3
A
回答
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
,classID
和contractID
這些字段更改爲適當的。 onShutdown
屬於我使用的私有框架:此擴展是無重啓的,這就是爲什麼它需要「手動」註冊組件,並且如果在瀏覽器會話期間關閉該組件,還將運行此代碼以將其刪除。
您還可以下載完整的擴展:testpolicy.xpi。
相關問題
- 1. 示例mac Firefox插件?
- 2. 如何在自舉的Firefox擴展中實現XPCOM組件(nsIContentPolicy)
- 3. 插件示例
- 4. Firefox插件顯示iFrame
- 5. Firefox - 圖像顯示插件
- 6. 是否有用C++編寫的Firefox 5插件示例?
- 7. Firefox插件:'promptService'不適用於Mozilla自己的示例代碼
- 8. Drupal插件示例
- 9. Eclipse插件示例
- 10. GStreamer - 示例插件
- 11. Jquery插件拖放示例
- 12. Firefox/Flash插件
- 13. Firefox插件 - 圖標不顯示
- 14. Firefox插件:contentaccessible =是
- 15. Firefox擴展插件?
- 16. Firefox/Chrome插件socketio
- 17. Firefox插件SDK - getElementsByTagName
- 18. Firefox:功能插件
- 19. 創建FireFox插件
- 20. 開發Firefox插件?
- 21. Firefox字典插件
- 22. AjaxUpload插件在Firefox
- 23. GWT插件Firefox 12
- 24. 分析firefox插件
- 25. 檢測Firefox插件?
- 26. Firefox插件更新
- 27. Firefox插件開發
- 28. 在Firefox插件的控件附加事件顯示面板
- 29. Firefox插件開發,如何在插件生成器中使用MDN中的示例代碼
- 30. NPAPI插件的簡單示例
弗拉基米爾,那testpolicy.xpi正是我一直在尋找:)感謝很多:) –
示例鏈接被破壞,
進口需要更新,但仍然,這塊只有網絡內容,以及如何改變dom樹? – msangel@msangel:我更新了鏈接。這裏的代碼僅僅是一個簡單引導擴展的例子 - 如果你的擴展是基於SDK的,那麼你可以自由地調整它,以便它適用於你,但請不要編輯帖子。問題在於內容策略僅用於阻止網絡請求,DOM修改必須通過其他方式發生。 –