0

onMessage()当我在面板的方法中发出消息时,我遇到了使用 Addon Builder 进行 Firefox 扩展开发的问题。错误信息是“ self is not defined”。我的代码是这样的:

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

js/savesubmitted.js

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

我编写的所有这些代码都是从 Chrome 扩展 API 转换而来的。Chrome 扩展中的代码是这样的:

在内容脚本中:

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

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

那么,怎么了?

4

1 回答 1

1

main.js中,使用savesubmittedPanel.port.emit代替self.port.emit

另请参阅:使用“端口”进行通信

于 2013-07-18T10:06:01.593 回答