Reyraa
Reyraa

Reputation: 4274

how to create event listener to a custom message from page script

Assume a panel injected:

   //main.js
    var ext = require("sdk/panel").Panel({
      width: 300,
      height: 500,
      contentScriptFile: [data.url("js/angular/angular.min.js"),
                          data.url("js/angular/angular-route.js"),
                          data.url("js/app.js"),
                          data.url("js/utilities.js"),
                          data.url("js/services.js"),
                          data.url("js/directives.js"),
                          data.url("js/controllers.js"),
                          data.url("js/popup.js")],
      contentScriptWhen: "ready"
    });

I can attach a listener to DOM of the active tab to detect some events:

if(window.customObj != undefined)
{
  window.postMessage(window.customObj, window.location.href);
}

how to get the object I'm sending by this custom message inside popup.js or main.js?

Upvotes: 1

Views: 176

Answers (1)

willlma
willlma

Reputation: 7533

Your main script can only communicate with content scripts, including those injected into tabs and those injected into panels. Your content scripts can communicate with your main scripts and with page scripts. So, in order to get something from a page script to main.js, your path needs to be:

page script/DOM --> content script

content script --> main script

And, optionally, main script --> panel script

Upvotes: 2

Related Questions