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);
}
那么,怎么了?