我正在使用Addon SDK(V1.9)开发Firefox扩展。
目标
我需要在加载之前(在发送请求之前)检查特定内容类型的HTTP请求。
问题
通过'observer-service'模块添加观察者时,我使用“http-on-modify-request”来检查所有HTTP请求。这提供了标题。但是,在检查内容类型时,永远不会根据请求进行设置。
确实在响应时设置了内容类型,我使用“http-on-examine-response”进行检查。但这意味着请求已经加载,从而破坏了我的扩展目的。
修改
@ Wladimir Palant 感谢您指出在发送请求标头时未设置内容类型。
我正在尝试在一堆与我相关的正则表达式上测试HTTP请求的URL并阻止它们。但是,我希望能够跳过某些内容类型(例如图像),因为它们与我的应用程序无关。
答案 0 :(得分:0)
所以,总结一下这个问题。完全控制加载到浏览器中的资源的最佳方法似乎是使用nsIContentPolicy。
@WladimirPalant写了一个很好的例子here。这就是他的代码看起来如何适应Addon SDK(v1.9):
const { Cc, Ci, Cu, Cm, components } = require('chrome');
const { XPCOMUtils } = Cu.import("resource://gre/modules/XPCOMUtils.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 = Cm.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);
},
// 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();