【问题标题】:Block JS with Firefox Addon使用 Firefox 插件阻止 JS
【发布时间】:2012-02-29 22:13:56
【问题描述】:

我正在使用 mozilla 提供的 addon-sdk 开发一个小的 firefox 插件。该插件只能在一个特定的网站上运行,并且需要阻止来自该网站的 js 文件。我正在寻找如何阻止此类请求的几个小时。

希望有人知道答案

【问题讨论】:

  • 阻止请求通常是通过创建一个实现nsIContentPolicy 的XPCOM 组件来完成的。然而,这非常重要,SDK 没有为此提供任何工具。

标签: firefox-addon firefox-addon-sdk


【解决方案1】:

是的,您必须大部分手动操作。 SDK 在这里对您没有太大帮助,但还是有可能的。

这符合您需要做的事情。请注意,这未经测试,不能开箱即用,只是为了让您了解所涉及的组件以及在哪里可以找到更多资源。

const { Cc, Ci, Cm, components } = require("chrome");
Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);
const CategoryManager = Cc["@mozilla.org/categorymanager;1"]
                                .getService(Ci.nsICategoryManager);

function PolicyComponent() { }  

PolicyComponent.prototype = {  
  desc:             "My nsIContentPolicy XPCOM Component",  
  classID:          components.ID("{3ffd2f60-3784-11e1-b86c-0800200c9a66}"),  
  contractID:       "@abc.def.com/policycomp;1",
  QueryInterface:   XPCOMUtils.generateQI([Ci.nsIContentPolicy]),

  shouldLoad: function(contentType, contentLocation, requestOrigin, aContext, mimeTypeGuess, extra) {
    if (contentLocation.spec != BLOCKED_JS) { return return Ci.nsIContentPolicy.ACCEPT; }
    else { return Ci.nsIContentPolicy.REJECT_REQUEST; }
  },
  shouldProcess: function() {
    return CI.nsIContentPolicy.ACCEPT;
  }
}

var pc = new PolicyComponent()

// Register the Interface
Cm.QueryInterface(Ci.nsIComponentRegistrar).registerFactory(pc.uuid, pc.desc, pc.contractID, pc);

// Add the content policy 
CategoryManager.addCategoryEntry("content-policy",pc.className,pc.contractID, true, true);  // not sure you should replace (last true statement)

查看这篇文章了解更多信息: What is missing in my nsIContentPolicy Firefox/IceWeasel extension XPCOMponent implementation for the shouldLoad to be called?

还可以查看这些文档:https://developer.mozilla.org/en/XUL_School/Intercepting_Page_Loads#Content_Policy

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多