<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Cross Document Messaging</title>
  <style>
    section {
      width: 400px;
      padding: 20px;
      margin-bottom: 10px;
      border: 1px solid #999999;
    }
  </style>
  <script>
    function initiate() {
      const button = document.getElementById("open");
      button.addEventListener("click", openPopup);
      window.addEventListener("message", receiver);
    }
    function openPopup() {
      window.open("https://www.domain2.com/popup.html", "popup", "width=400,height=200");
    }
    function receiver(event) {
      const databox = document.getElementById("databox");
      if (event.origin == "https://www.domain2.com") {
        databox.innerHTML = "Valid message: " + event.data;
      } else {
        databox.innerHTML = "Invalid origin";
      }
    }
    window.addEventListener("DOMContentLoaded", initiate);
  </script>
</head>
<body>
  <section>
    <button type="button" id="open">Open Popup</button>
  </section>
  <section id="databox"></section>
</body>
</html>