javascriptgoogle-chrome-extensiondom-eventsfirefox-addon-sdk

How can I emit message in panel's onMessage method?


I meet a problem with Firefox extension development using Addon Builder when I emit message in panel's onMessage() method. And the error info is "self is not defined". My code is like this:

in main.js

var savesubmittedPanel = require("sdk/panel").Panel({
  width: 322,
  height: 427,
  contentURL: data.url("savesubmitted.html"),
  include:["http://*/*","https://*/*"],
  contentScriptFile: [ data.url("js/savesubmitted.js")],
  onMessage: function(messagedata) {
      switch(messagedata.type) {
      case 'get.submittedid':
        self.port.emit("response.get.submittedid",submittedIdentity);
        break;
      case 'add.identity':
        addIdentityToList(messagedata.identity);
        notifyUpdatedIdentities();
        self.port.emit("response.add.identity")
        break;
      }
  }           
});

in js/savesubmitted.js

self.postMessage({type:"get.submittedid"});
    self.port.on("response.get.submittedid",function(response){
        //do something
    });

all these code I write is converting from Chrome extension API. The code in the Chrome extension is like that:

in the content script:

chrome.extension.sendMessage({type : "get.submittedid"}, function(response) {
        pageIdentitySave=response;
        showSaveIdentityTab();
    });

in the background.js:

chrome.extension.onMessage.addListener(function(message, sender, sendResponse){    
        console.log("Background.js: " + "Received message: "+message.type+" from tab: "+sender.tab.id);         
        if(message.type == "get.submittedid") {
            sendResponse(submittedIdentity);
        }

So, what's wrong?


Solution

  • In main.js, use savesubmittedPanel.port.emit instead of self.port.emit.

    See also: Communicating using "port".