ampproject / worker-dom

The same DOM API and Frameworks you know, but in a Web Worker.
Apache License 2.0
3.21k stars 152 forks source link

listen to message events from postMessage not supported (?) #747

Open massawho opened 4 years ago

massawho commented 4 years ago

Hi I am trying to listen to events from my iframe, but it just doesn't work. Even though addEventListener is supported, when I try to listen for message events, I don't get the event from my iframe. I am not sure if it's because it is running inside a worker and it has its own virtual DOM, but I don't receive the message sent by my iframe.

        window.addEventListener('message', function(e) {
          console.log("catching event: "+e.origin); //logs "catching event:  undefined"
        }, false);

The whole code from what I am trying to achieve is this:

      <amp-script script="navi_impressions" height="24.4rem" width="">
      <amp-ad
        height="24.4rem"
        data-multi-size="fluid"
        type="doubleclick"
        data-slot="my-ad-unit">
      </amp-ad>
      </amp-script>
      <script type="text/plain" target="amp-script" id="navi_impressions">
        window.addEventListener('message', function(e) {
          console.log("catching event: "+e.origin);
        }, false);
      </script>

I have an amp-ad that generates the iframe of my ad from google ad manager. From the creative, I sent a postMessage event; however, the same code works if I run in the parent document.

kristoferbaxter commented 4 years ago

I’ll create a minimal repro and take a look. Thanks for reporting the issue!

massawho commented 4 years ago

Thanks @kristoferbaxter ! Let me know if I can help in anything