chromium/chrome/test/data/companion_iframe.html

<html>
<body>
  <a id="some_link" href="google.html" target="_blank">Click Here</a>
</body>
<script>
  // The proto received on last update postmessage. Reset on getLastReceivedCompanionProto().
  lastReceivedCompanionProto = null;
  // The url received on last NotifyLinkOpen postmessage.
  lastReceivedLinkOpenedUrl = null;
  // The metadata for the last URL received on the NotifyLinkOpen postmessage.
  lastReceivedLinkOpenedMetadata = null;
  // The page title received on last UpdatePageTitle postmessage.
  lastReceivedPageTitle = null;
  // The innerHtml received on last UpdateInnerHtml postmessage.
  lastReceivedInnerHtml = null;

  // Promise to be returned to for browser tests when the test needs to wait
  // for a postmessage call from the browser to be received by the companion
  // page.
  var promiseResolve, promiseReject;
  var promise = new Promise(function (resolve, reject) {
    promiseResolve = resolve;
    promiseReject = reject;
  });

  // Postmessage from browser.
  function onBrowserMessage(event) {
    if (event.data['type'] == 31) { // `MethodType.kUpdateCompanionPage`
      window.lastReceivedCompanionProto = event.data['companionUpdateParams'];
      promiseResolve(event.data['companionUpdateParams']);
    } else if (event.data['type'] == 32) { // `MethodType.kOnCqFindTextResultsAvailable`
      promiseResolve(event.data['cqTextFindResults']);
    } else if (event.data['type'] == 33) { // `MethodType.kOnDeviceVisualClassificationResult`
      promiseResolve(event.data['visualSearchParams']);
    } else if (event.data['type'] == 34) { // `MethodType.kNotifyLinkOpen`
      window.lastReceivedLinkOpenedUrl = event.data['openedUrl'];
      window.lastReceivedLinkOpenedMetadata = event.data['openMetadata'];
      promiseResolve(event.data['openedUrl']);
    } else if (event.data['type'] == 35) { // `MethodType.kUpdatePageContent`
      window.lastReceivedPageTitle = event.data['pageTitle'];
      window.lastReceivedInnerHtml = event.data['innerHtml'];
      promiseResolve();
    }
  }

  function getLastReceivedCompanionProto() {
    protoCopy = window.lastReceivedCompanionProto;
    window.lastReceivedCompanionProto = null;
    return protoCopy;
  }

  function getLastReceivedLinkOpenedUrl() {
    return window.lastReceivedLinkOpenedUrl;
  }

  function getLastReceivedLinkOpenedMetadata() {
    return JSON.stringify(window.lastReceivedLinkOpenedMetadata);
  }

  function getLastReceivedPageTitle() {
    return window.lastReceivedPageTitle;
  }

  function getLastReceivedInnerHtml() {
    return window.lastReceivedInnerHtml;
  }

  function waitForMessage() {
    return promise;
  }

  window.addEventListener('message', onBrowserMessage, false);

</script>
</html>