penkzhou
penkzhou

Reputation: 1210

How to use port to transfer message between main.js and the panel's content script?

I encounter a problem with my firefox addon.I write code in the main.js like this:

var mypanel = require("sdk/panel").Panel({
  width: 322,
  height: 427,
  contentURL: data.url("main.html"),
  include:["http://*/*","https://*/*"],
  contentScriptFile: [data.url("js/content.js")]  
});


mypanel.on('message', function(message) {
    switch(message.type) {
      case 'type1':
        console.log(message.content);
        break;
      case 'type2':
        console.log(message.content);
        break;
     }
});

the code in the content.js:

self.port.emit("message",{type:"type1",content:"content1"});

When I run the code, the Mozilla Addon-Builder remind me that "TypeError: self.port is undefined",so what's wrong with my code?Or the way I write about the Firefox port messaging is wrong? How should I write the code? Thank you.

Upvotes: 0

Views: 210

Answers (1)

paa
paa

Reputation: 5054

See Scripting Trusted Panel Content

Upvotes: 1

Related Questions