javascript - How can I emit message in panel's onMessage method? -
i meet problem firefox extension development using addon builder when emit message in panel's onmessage()
method. , error info "self not defined
".my code 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 });
all these code write coverting chrome extension api. code in chrome extension that:
in content script:
chrome.extension.sendmessage({type : "get.submittedid"}, function(response) { pageidentitysave=response; showsaveidentitytab(); });
in background.js
:
chrome.extension.onmessage.addlistener(function(message, sender, sendresponse){ console.log("background.js: " + "received message: "+message.type+" tab: "+sender.tab.id); if(message.type == "get.submittedid") { sendresponse(submittedidentity); }
so,what's wrong? thanks.
in main.js
, use savesubmittedpanel.port.emit
instead of self.port.emit
.
see also: communicating using "port".
Comments
Post a Comment