简体   繁体   English

如何在Firefox插件中覆盖内置的XPCOM组件?

[英]How to overwrite built in XPCOM component in Firefox addon?

I'm taking a foray into Firefox extension development for the first time, and so far it's been pretty comfortable going, but I'm running into a problem; 我是第一次涉足Firefox扩展开发,到目前为止它一直很舒服,但我遇到了一个问题; one of the things I need to do overwriting the built-in nsIPromptService and replacing it with something of my own instead. 我需要做的一件事就是覆盖内置的nsIPromptService并将其替换为我自己的东西。

I walked through the basic XPCOM component creation tutorial here and got the hello world one working: 我在这里浏览了基本的XPCOM组件创建教程,并让hello world工作了:

https://developer.mozilla.org/en/creating_xpcom_components https://developer.mozilla.org/en/creating_xpcom_components

And everything in that seems to work fine, but nothing I've been able to find or research shows how I can overwrite an interface from javascript. 并且其中的所有内容似乎都运行良好,但我找不到或研究的任何内容都显示我如何从javascript覆盖界面。 I've seen things in C++ and Java that seem to be able to overwrite the built-in components, but I can't find anything about doing this from javascript, and just trying to change the contract ID didn't work; 我已经看到C ++和Java中的东西似乎能够覆盖内置组件,但是我无法从javascript中找到任何关于这样做的事情,只是尝试更改合同ID不起作用; when I try to get the service from the contract ID (as below), it just returns the original, built-in component version. 当我尝试从合同ID(如下所示)获取服务时,它只返回原始的内置组件版本。

var myComponent = Components.classes['@mozilla.org/embedcomp/prompt-service;1']
                                               .getService(Components.interfaces.nsIPromptService);

Is there something really obvious here that I'm missing? 这里有什么明显的东西让我失踪吗? Is this the wrong way to go about overriding components (I can't seem to find anything anywhere, so I'm not really sure what I should be doing..). 这是否是重写组件的错误方法(我似乎无法在任何地方找到任何东西,所以我不确定我应该做什么......)。

Neil, thanks for the suggestion. 尼尔,谢谢你的建议。 That's what I thought I was doing (and I was), but if you're actually overriding a contract (instead of defining a new one), it looks like the answer is that you have to go to the nsIComponentRegistrar and actually register your factory (rather than relying on the chrome.manifest to handle it for you). 这就是我以为我在做什么(我是),但是如果你实际上覆盖了一份合同(而不是定义一个新合同),那么看起来答案是你必须去nsIComponentRegistrar并实际注册你的工厂(而不是依靠chrome.manifest来为你处理它)。 An example of this would be: 一个例子是:

Components.manager.nsIComponentRegistrar.registerFactory(CLASS_ID, CLASS_NAME, CONTRACT_ID, MyPromptServiceFactory);

Where the constans were: 常数是:

const CLASS_ID = Components.ID("{a2112d6a-0e28-421f-b46a-25c0b308cbd0}");

// description
const CLASS_NAME = "My Prompt Service";

// textual unique identifier
const CONTRACT_ID = "@mozilla.org/embedcomp/prompt-service;1";

Where the CLASS_ID/CONTRACT_ID were the IDs for the pre-existing service. 其中CLASS_ID / CONTRACT_ID是预先存在的服务的ID。

您需要使用要覆盖的服务的合同ID注册组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM