Aaronius / penpal

A promise-based library for securely communicating with iframes via postMessage.
MIT License
381 stars 56 forks source link

SRC-less iframes report ERR_NOT_IN_IFRAME #44

Closed jameswragg closed 4 years ago

jameswragg commented 4 years ago

I'm creating an iframe with JS, building my own document (with Penpal in head) and writing it in, the iframe loads but Penpal fails with: connectToParent() must be called within an iframe when it actually is and the parent has Penpal running and listening.

The check that's failing is here: https://github.com/Aaronius/penpal/blob/master/src/connectToParent.js#L32

I can only assume window does equal window.top when the document has been built from the parent.

Here's a poor test-case on jsbin: https://jsbin.com/huhocig/5/edit?html,js,output

Poor because jsbin uses iframes, so the condition is met but hopefully you'll get the gist of how I'm building my iframe.

Is there a way around this, or can the check be disabled? If I manually comment out the check - everything works as expected.

jameswragg commented 4 years ago

I gave the iframe a generated name & it seemed to fix it!

Aaronius commented 4 years ago

Thanks for the information @jameswragg. I'm glad you got it working. I tried to reproduce your example because I was curious about it, but things seem to be working for me. Here's my setup: http://dtm.aaronhardy.com/penpal/

Did I miss something?

jameswragg commented 4 years ago

Thanks for coming back to me even though its working :)

The only difference is I'm creating the iframe & appending to the body, writing the contents in the onload callback but thats minimal difference & can only assume I was doing something silly as its working correctly now.

Using Penpal because I can apply it to any iframe, my goto for postmesaging (postmate) has to create the iframe itself. Glad I was forced to make the switch, really enjoying using it!

Aaronius commented 4 years ago

Thanks @jameswragg! I appreciate the kind words.