문제

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