質問

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?

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top