<!DOCTYPE html>
<body>
  <button id="one">one</button>
  <button id="two" autofocus>two</button>
  <button id="three">three</button>
  <script>
    function messageHandler(e) {
      if (e.data === 'check-active-element') {
        window.requestAnimationFrame(() => {
          let autofocusedButton = document.querySelector('#two');
          e.source.postMessage(document.activeElement === autofocusedButton);
        });
      }
    }

    window.portalHost.onmessage = messageHandler;
    window.onportalactivate = e => {
      let portal = e.adoptPredecessor();
      portal.onmessage = messageHandler;
      document.body.appendChild(portal);
    }
  </script>
</body>
