Pergunta

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?

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top