How to rewrite XPCOM built-in component in Firefox addon?

The first time I raid the development of Firefox extensions, and so far it has been pretty comfortable, but I ran into a problem; one of the things I need to do is overwriting the built-in nsIPromptService and instead replacing it with something of my own.

I went through the basic tutorial on creating XPCOM components and got a welcome world that works:

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

And everything in this seems to work fine, but nothing that I managed to find or not research shows how I can rewrite the interface from javascript. I saw things in C ++ and Java that seem to be able to overwrite inline components, but I can't find anything about it from javascript, and just trying to change the contract id didn't work; when I try to get the service from the contract identifier (as shown below), it just returns the original embedded version of the component.

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

Is there something really obvious here that I'm missing? Isn't that the wrong way to remake components (I can't find anything anywhere, so I'm not sure what I should do.).

+4
source share
2 answers

Neil, thanks for the suggestion. This is what I thought I was doing (and I was), but if you really redefine the contract (instead of defining a new one), it looks like the answer is that you need to go to nsIComponentRegistrar and register your factory (instead of rely on chrome.manifest to handle this for you). An example of this might be:

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

Where were the constants:

 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"; 

If the identifiers CLASS_ID / CONTRACT_ID were identifiers for an existing service.

+3
source

You need to register your component using the service contract identifier of the service you want to override.

0
source

Source: https://habr.com/ru/post/1346831/


All Articles