2

I have two content Scripts in main.js of Firefox Addon : contentScript A is inside 'panel' module (module A) contentScript B is inside 'page-mod' module (module B) How can they communicate or exchange messages ? I tried to do this by using the following steps : 1. sending message from contentScript A to AddonScript A 2. sending message from AddonScript A to AddonScript B by including module B in A 3. sending message from AddonScript B to contentScript B. However , it doesn't work (rather it does work intermittently , may be due to some errors in code). . Is this method ok ? Can any one please comment on any better method ? . Thanx

2
  • Yes, they cannot communicate with each other directly - they have to communicate through the extension. Jan 16, 2012 at 19:03
  • can you plz explain 'communication through the extension'. Plz elaborate in detail. Addon sdk support on the web is scarce.
    – GILL
    Jan 17, 2012 at 12:22

1 Answer 1

6

Due to how the SDK's security model, any communication between your panel and your page-mod need to be routed through the main add-on code itself. Here is an example that takes data from a form implemented in a Panel and sends it through the main script into a page-mod:

https://builder.addons.mozilla.org/addon/1035008/latest/

The key piece of code is this one:

var pagemod = require("page-mod").PageMod({
    include: [target],
    contentScriptFile: [data.url('jquery-1.7.1.min.js'), data.url('page-mod.js')],
    onAttach: function(worker) {
        // console.log('attached...');
        // when we get a panel-message event from the panel 
        panel.port.on('panel-message', function(data) {
            // we emit the same message through to the page-mod
            worker.port.emit('panel-message', data);
        });
    }
});

You'll notice that, when the page-mod is attached, I set up the panel instance to catch the 'panel-message' event and then emit it directly into the current page-mod worker.

6
  • Thats a good explainantion , however , what I am trying to do is : send a message from a panel to the web page opened in browser so that I can make changes in the page according to the inputs into the panel. This is opposite to the example you have suggested. Plz help.
    – GILL
    Jan 19, 2012 at 18:00
  • If I use 'onAttach' , the addon code is executed only once ,where as , I want a continuous communication after page load.
    – GILL
    Jan 19, 2012 at 18:32
  • Subsequent communication will be event-driven. For example, every time something happens in the page that needs to be communicated back to main.js, you would need to emit an event with the appropriate data using self.port.emit(). In main.js, you'll need to implement a handler 'worker.port.on("some-event", function() {})' to handle the event being sent. Jan 23, 2012 at 17:05
  • Thats a fair explanation. However, can you please explain how can I emit an event from pagemod add-on script to panel add-on script and vice versa (If I use OnAttach , it is executed only once and that too on the beginning. onAttach will probably not work for me). Please help.An example in this regard will be greatly helpful for beginners. Thanx
    – GILL
    Jan 24, 2012 at 18:17
  • I was assuming that the function associated with onAttach will be executed only once. Your answer seems to state that multiple emits are possible from onAttach script. I will try this.
    – GILL
    Feb 1, 2012 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.