如何覆盖Firefox插件中的内置XPCOM组件? [英] How to overwrite built in XPCOM component in Firefox addon?

查看:103
本文介绍了如何覆盖Firefox插件中的内置XPCOM组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试了Firefox的扩展开发,到目前为止,这样做一直很顺利,但是我遇到了一个问题。我需要做的一件事是覆盖内置的nsIPromptService,并用我自己的东西代替它。

我在这里介绍了基本的XPCOM组件创建教程,并得到了hello world的一个工作:

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



在这一切似乎工作正常,但我一直无法找到或研究显示如何我可以覆盖从JavaScript接口。我已经看到了C ++和Java中似乎能够覆盖内置组件的东西,但我找不到任何关于从JavaScript做到这一点,只是试图改变合同ID不起作用;当我尝试从合同ID(如下)获得服务时,它只是返回原始的内置组件版本。

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

这里有什么明显的东西我错过了吗?这是错误的方式来重写组件(我似乎无法找到任何地方,所以我不知道我应该做什么..)。

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

常量是:

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

//描述
const CLASS_NAME =我的提示服务;

//文本唯一标识符
const CONTRACT_ID =@ mozilla.org / embedcomp / prompt-service; 1;

其中CLASS_ID / CONTRACT_ID是预先存在的服务的ID。


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; one of the things I need to do overwriting the built-in nsIPromptService and replacing it with something of my own instead.

I walked through the basic XPCOM component creation tutorial here and got the hello world one working:

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. 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; when I try to get the service from the contract ID (as below), it just returns the original, built-in component version.

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). 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.

这篇关于如何覆盖Firefox插件中的内置XPCOM组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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