How can two add-ons communicate if one is sdk and one is xul?

Before I ported my add-on from xul, I used this:

Components.utils.import("resource://searchdictcc-modules/overlay.jsm");

To access my add-on from the fire-gestures add-on.
But this does not work anymore. I get this error:

ReferenceError: require is not defined

But I guess it would not work with multiprocess firefox anyway, so is there any way to send a message from a xul add-on to an sdk add-on?

Control

Do you have control over each addon?

Then you can set up an observer, event listener, and maybe frame message listener. which one of these sound good to you? I can show you more.

Here are the relavent links:

These are built in observers - https://developer.mozilla.org/en-US/docs/Observer_Notifications#Developer_tools
How to create your own - https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIObserverService

Event listener with trusted argument (you most likely will need trusted it depends) - http://stackoverflow.com/questions/22121652/how-to-listen-to-custom-events-on-all-windows-bubbling-issue

Message manager - https://developer.mozilla.org/en-US/Firefox/Multiprocess_Firefox/The_message_manager#Frame_script_lifetime

Message manager is the e10s safe way and recommended way if you can do it. If not observers are e10s ok, it is usable in the parent process.

No Control

If you don’t have control you can tap into it with some not recommended methods, as your addon will likely be rejected. It uses some internal stuff so these things can change at any time. Here is how though:

http://stackoverflow.com/questions/22057888/controlling-a-firefox-extension-via-javascript/22099148#22099148

3 Likes

I’ll look at these when I get the time.
To clarify: I have access to my own add-on source and the other add-on allows me to execute javascript code, but I don’t have access to the add-on source itself.
So I’ll check out if it’s possible to use the message manager using old xul javascript.

I currently tap into my addons bootstrap scope with this code, but only for testing, they change the path to the XPIProvider.jsm all the time and mess around with it internally. I don’t think this method works on nightly. I use it on beta and release.

var getScope = function() { var XPIScope = Cu.import('resource://gre/modules/addons/XPIProvider.jsm'); return XPIScope.XPIProvider.bootstrapScopes['MouseControl@jetpack']; };

a = getScope();

Now I can do a.ANYTHING()

Yeah, nightly gives me this:

TypeError: XPIScope.XPIProvider.bootstrapScopes is undefined

I tried messagemanager, but aside from the fact that I’m confused which message manager to use, most of them don’t even work.
I’ve tried calling addMessageListener and after that send/broadcastAsyncMessage right after it for these:
browser.messageManager;
Cc["@mozilla.org/globalmessagemanager;1"].getService(Ci.nsIMessageListenerManager);
(window)messageManager
Cc["@mozilla.org/parentprocessmessagemanager;1"].getService(Ci.nsIMessageBroadcaster);
Cc["@mozilla.org/childprocessmessagemanager;1"].getService(Ci.nsISyncMessageSender);

Only the last two received the message.
But when I try to call these any of the above from the other add-on, I’m unsure how to catch them from my add-on.
Currently I do this:

pageMod.PageMod({
    include: "*",
    contentScriptWhen: 'start',
    contentScriptFile: self.data.url("pagemod.js"),
    attachTo: ["existing", "top", "frame"],
    onAttach: function (worker) {
        var lowLevelTab = core.viewFor(worker.tab);
        var browser = tabUtils.getBrowserForTab(lowLevelTab);
        browser.messageManager.addMessageListener("searchdictcc@roughael:handleMouseGesture", function (message) {
            console.log(message.data)
        });
    }
});

It doesn’t work for any of them.
I’ve tried registering with the CC message managers using require("chrome), but no success either.

I’ve tried using postMessage and Dom-Events as described here:


Which works for scripts running in the webpage I’m on, but for some reason not when I do it from the fire-gestures add-on.

Hey @Lusito I figured out how to do it in Firefox 47+ I updated the stackoverflow topic - http://stackoverflow.com/a/34983949/1828637

This is how to do it:

Update for Firefox 47 and up

Things changed drastically in Firefox 47. This is the new way to access it.

var XPIScope = Cu.import('resource://gre/modules/addons/XPIProvider.jsm');
var addonid = 'Profilist@jetpack';
var scope = XPIScope.XPIProvider.activeAddons.get(addonid).bootstrapScope
1 Like