intercept-postmessage.html 1.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
<html>
  <head>
    <script>
      function write(str) {
        pre = document.getElementById('console');
        text = document.createTextNode(str + '\n');
        pre.appendChild(text);
      }

      function recvMsg1(e) {
        write("RECV1");
        window.removeEventListener("message", recvMsg1, false);

        if (window.layoutTestController)
          layoutTestController.interceptPostMessage = true;

        window.addEventListener("message", recvMsg2, false);
        window.postMessage("Message 2", "*");
      
        // Ensure that we're intercepting postMessages before the origin check
        window.postMessage("Message 3", "http://example.org"); 

        // We need to call setTimeout here because we intercept the next event.
        // If this test becomes flaky, you may need to increase this timeout.
        window.setTimeout("done()", 50);
      }

      function recvMsg2(e) {
        write("RECV2");
      }

      function done() {
        if (window.layoutTestController)
          window.layoutTestController.notifyDone();
      }

      window.addEventListener("message", recvMsg1, false);

      if (window.layoutTestController) {
        window.layoutTestController.dumpAsText();
        window.layoutTestController.waitUntilDone();
      }

      window.postMessage("Message 1", "*");
    </script>
  </head>
  <body><pre id="console"></pre></body>
</html>