1

I am developing an extension where I need to get notified whenever an iframe is loaded and ready. I used page-mod but I don't get the expected output. This is my code:

var data = require("sdk/self").data;
var pageMod = require("sdk/page-mod");

pageMod.PageMod({
 include: ['*'],
 contentScriptFile: data.url("pageNavData.js"),
 contentScriptWhen: "ready",
 attachTo: ["frame"],
 onAttach: function(worker) {
  worker.port.on("gotElement", function(elementContent) {
    console.log(elementContent);
  });
 }
});

And pageNavData.js is:

self.port.emit("gotElement", document.location.href);

Can anybody see what is wrong with this?

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
Alagiri
  • 23
  • 1
  • 10
  • The code looks fine. What output are you seeing? [This answer](http://stackoverflow.com/a/1245166/1720014) might be helpful. – willlma May 01 '14 at 07:44

1 Answers1

0

The problem here is that the "gotElement" message is emitted before the listener is attached.

You can fix it with:

setTimeout(_ => self.port.emit("gotElement", document.location.href));

Afaict you don't need the contentScript, just do what you wanted to do in the onAttach handler.

erikvold
  • 15,988
  • 11
  • 54
  • 98