Firefox插件:为什么面板内容中的self.port未定义?

时间:2013-07-24 05:26:48

标签: javascript-events google-chrome-extension message firefox-addon-sdk

我在Firefox插件开发中遇到一个奇怪的事情:我编写这样的代码(我将注释代码从chrome扩展转换为Firefox插件代码):

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();});
    });
}

当我使用Addon Builder编译它时,它会收到“Error: NS_ERROR_XPC_NOT_ENOUGH_ARGS: Not enough arguments [nsIDOMWindow.postMessage]”错误。然后我改变代码:

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();});
    });
}

我收到了另一个错误:TypeError: self.port is undefined。那么,怎么了? 感谢。

1 个答案:

答案 0 :(得分:2)

self更改为addon。详细文档为here