gpt4 book ai didi

firefox - Firefox 插件的 nsIContentPolicy 示例?

转载 作者:行者123 更新时间:2023-12-02 15:39:23 25 4
gpt4 key购买 nike

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

最佳答案

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

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();

这来 self 用来查看内容策略实现问题的最小内容策略实现 - 除了将所有内容策略调用转储到控制台 ( window.dump documentation ) 之外,它不执行任何操作。显然,在实际实现中,字段 classDescriptionclassIDcontractID 应该更改为适当的内容。 onShutdown 属于我正在使用的私有(private)框架:此扩展无需重新启动,这就是为什么它需要“手动”注册组件,并且如果在浏览器期间关闭组件,还将运行此代码以将其删除 session 。

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

关于firefox - Firefox 插件的 nsIContentPolicy 示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10788489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com