BH_Lin
BH_Lin

Reputation: 11

Firefox Add-on - how to get post message on the page in add-on panel.html

I bump into trouble in firefox add-on development.

[symptom]: can not receive message from firefox add-on bar's panel page, the message is sent from iframe on the panel.html.

here is my code:

/* in file [popup.html] */ // in the body i add an iframe element.

   iframe src="http://localhost/hello.html"

/* in file [popup.js] */ // I add a listener

   window.addEventListener("message", 
    function(event) {
        console.log("popupJS Receive Event from WebPage(" + event.origin);      
        console.log(event);
        //alert(event);
    });

/* in the remote page hello.html */ // I send the message by clicking . emphasized text

   window.postMessage({ type: "FROM_PAGE", text: "Hello from the webpage!" }, "http://dicrectpass.com");

=====>> But, I still can not receive the message from the iframe !! Why?

Upvotes: 1

Views: 783

Answers (1)

ChrisM
ChrisM

Reputation: 21

I was having the same problem. Then I realized I was including my Javascript from within the HTML document I was loading instead of via the Panel constructor. Once I switch that out, it worked fine.

Panel reference

var pnl = panel.Panel({
    width: 300,
    height: 300,
    contentURL: self.data.url('popup.html'),
    contentScriptFile: [
        self.data.url('jquery.js'),
        self.data.url('popup.js')
    ],
    onMessage: function(message) {
        console.log(message);
    }
});

Upvotes: 1

Related Questions