javascriptgoogle-chrome-extensiondom-eventsmessagefirefox-addon-sdk

Firefox addon: Why the self.port in the panel's contentscript is undefined?


I meet a strange thing with Firefox addon development: I write code like this (I convert the commented code from Chrome extension to Firefox addon code):

function sendStoreIdentitiesMessage(identities, remember){
    console.log("Storing identities: "+identities.length);
    if(remember==null){
        remember=false;
    }
    //extension.sendMessage({type:"save.identities", identities:identities, remember:remember}, function(response) {
    //  identitiesNum=identities.length;
    //  identitiesDate=new Date();
    //  sendGetPageIdsMessage(function(){showCodeImageSuccess();});
    //});
    
    self.postMessage({type:"save.identities", identities:identities, remember:remember});
    self.port.on("response.save.identities",function(response){
        identitiesNum=identities.length;
        identitiesDate=new Date();
        sendGetPageIdsMessage(function(){showCodeImageSuccess();});
    });
}

When I compile it with Addon Builder,it gets the error:

"Error: NS_ERROR_XPC_NOT_ENOUGH_ARGS: Not enough arguments [nsIDOMWindow.postMessage]"

Then I change the code like this:

function sendStoreIdentitiesMessage(identities, remember){
    console.log("Storing identities: "+identities.length);
    if(remember==null){
        remember=false;
    }
    //extension.sendMessage({type:"save.identities", identities:identities, remember:remember}, function(response) {
    //  identitiesNum=identities.length;
    //  identitiesDate=new Date();
    //  sendGetPageIdsMessage(function(){showCodeImageSuccess();});
    //});
    
    self.port.emit("message",{type:"save.identities", identities:identities, remember:remember});
    self.port.on("response.save.identities",function(response){
        identitiesNum=identities.length;
        identitiesDate=new Date();
        sendGetPageIdsMessage(function(){showCodeImageSuccess();});
    });
}

I got another error:

TypeError: self.port is undefined.

So, what's wrong?


Solution

  • change the self to addon.Detailed document is here.