firefox插件的nsIContentPolicy示例? [英] An example of nsIContentPolicy for firefox addon?

查看:21
本文介绍了firefox插件的nsIContentPolicy示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读 NsIContentPolicy 并在整个 Stackoverflow 中搜索了实施 NsIContentPolicy 的正确教程,但是一切都是徒劳.我知道 Adblock 使用 NsIContentPolicy 作为他们的主要武器.逆向工程 Adblock 并没有帮助我理解如何实现 NsIContentPolicy.有没有使用 NsIContentPolicy 进行学习的简单插件,或者关于 NsIContentPolicy 的任何好的教程?

I have read NsIContentPolicy and have searched whole Stackoverflow for a proper tutorial for implementing NsIContentPolicy, but all in vain. I know that Adblock uses NsIContentPolicy as their main weapon. Reverse engineering Adblock didn't help me to understand how to implement NsIContentPolicy. Is there any simple addon using NsIContentPolicy for learning, or any good tutorial on NsIContentPolicy?

推荐答案

我不知道有什么好的教程,但我可以给你一些最小的示例代码:

I am not aware of any good tutorial but I can give you some minimal example code:

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 + "
");
    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 + "
");
    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 文档).显然,在实际实现中,字段 classDescriptionclassIDcontractID 应该更改为适当的内容.onShutdown 属于我正在使用的私有框架:这个扩展是无需重启的,这就是为什么它需要手动"注册组件并且如果它在浏览器期间关闭也会运行这个代码来删除它会话.

This comes from the minimal content policy implementation I use to look at issues with the content policies implementation - it doesn't do anything other than dumping all content policies calls to the console (window.dump documentation). Obviously, in a real implementation the fields classDescription , classID and contractID should be changed to something proper. onShutdown belongs to the private framework I am using: this extension is restartless which is why it needs to register the component "manually" and will also run this code to remove it if it is shut down during a browser session.

您还可以下载完整的扩展:testpolicy.xpi.

You can also download the complete extension: testpolicy.xpi.

这篇关于firefox插件的nsIContentPolicy示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆