0👍
✅
You are sending a message to your iframe’s context, so from this iframe you don’t want to listen for top
‘s messages, but only for this iframe’s window
ones.
Change
window.top.addEventListener('message',...
to
window.addEventListener('message',...
If you want to understand better how messaging works, I invite you to read this answer of mine.
Baiscally with an iframe, one port is in the main context, under HTMLIframeElement.contentWindow
, and the other one is in the iframe’s context, under window
. To communicate between both contexts, each context should listen to and talk in their own port.
When this is fixed you will face a new issue that the location
object can’t be cloned. Since apparently you only want to send the current URL of the main page, then only send this:
var data = {title: document.title, url: window.location.href };
Source:stackexchange.com