<!DOCTYPE html>
<title>Helper IFrame</title>
<script>
'use strict';

async function onLoad() {
  // Load the innermost child iframe and its content.
  const params = new URLSearchParams(self.location.search);
  const frame = document.createElement('iframe');
  frame.src = params.get('target');
  document.body.appendChild(frame);

  self.addEventListener('message', evt => {
    // Pass any operations request messages to the
    // innermost child iframe.
    if (evt.data.op){
      // Ensure that the iframe has loaded before passing
      // on the message.
      frame.addEventListener('load', function(){
        frame.contentWindow.postMessage(evt.data, '*');
      });
    }
    else {
      // All other messages, should be sent back to the
      // top-level site.
      if (self.opener)
        self.opener.postMessage(evt.data, '*');
      else
        self.top.postMessage(evt.data, '*');
    }
  });
}
self.addEventListener('load', onLoad);
</script>
