title: Communication between Chrome-app and a homepage with PostMessage
body: <p>I need to be able to send postMessage from a Chrome App through a webview to the homepage and back.</p>
  
  <p>I have established PostMessage from the Chrome App to the homepage, and the PostMessage is also catched by the homepage and a new one is send back, but this PostMessage reply is not caught by the Chrome App. </p>
  
  <p>I can see that it is possible on the Chrome-App API.:</p>
  
  <p><a href="https://developer.chrome.com/apps/tags/webview#method-ContentWindow-postMessage">The guest will be able to send replies to the embedder by posting message to event.source on the message event it receives.</a></p>
  
  <p>So the problem is that i cannot get the Chrome App to catch the reply from the homepage, even though i am using event.source.postMessage('', event.origin) the send the reply with. Is the window.addEventListener('message', messageHandler, false); at the end of background.js wrong ?</p>
  
  <p>I have include the my code below.:</p>
  
  <p><strong>background.js</strong> (where the Chrome app is initialized).:</p>
  
  <pre><code>  var myAppWin = null;
    var webview = null;
  
  chrome.app.runtime.onLaunched.addListener(function() {
    // Center window on screen.
    var screenWidth = screen.availWidth/2;
    var screenHeight = screen.availHeight;
  
    var chromeWindow = chrome.app.window.create('webview-embed.html', {
      id: "helloWorldID",
      bounds: {
        width: screenWidth,
        height: screenHeight,
      }
    }, function(win) {
  
          myAppWin = win;
          myAppWin.contentWindow.addEventListener('DOMContentLoaded', function() {
  
            webview = myAppWin.contentWindow.document.getElementById('webview');
  
            try{
              webview.addEventListener("contentload", function () {
  
                console.log("webview content is now loaded");
  
                try{
                  console.log("Trying to post message");
                  webview.contentWindow.postMessage("Message from Chrome APP!", "*");
                }catch(error){
                  console.log("postMessage error: " + error);
                }
  
              });
            }
            catch(err) {
              console.log("Webview error: " + err);
            }
  
          });
    });
  
    //Event listnener for the PostMessage reply    
    var messageHandler = function(event) {
      console.log("got message from page back: " + event.data);
    };
    window.addEventListener('message', messageHandler, false);
  
  
  
  });
  </code></pre>
  
  <p><strong>webview-embed.html</strong> (html file with the webview tag).:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;head&gt;
  &lt;title&gt;webview&lt;/title&gt;
  &lt;/head&gt;
  &lt;body style='margin:0;padding:0;' &gt;
      &lt;webview src="http://localhost" style="width:100%;height:100%;" id="webview" &gt;&lt;/webview&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>index.html</strong> (the homepage on the web, that needs to catch the first PostMessage and sent a reply back to the Chrome-app).:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en" &gt;
      &lt;head&gt;
          &lt;meta charset="utf-8" /&gt;
          &lt;meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"&gt;
          &lt;title&gt;title&lt;/title&gt;
      &lt;/head&gt;
      &lt;body &gt;
      &lt;div id="wrapper" &gt;
          //body
      &lt;/div&gt;
  
      //Used to catch messages from the Chrome App and reply back 
      var messageHandler = function(event) {
        console.log('Message received fom APP!');
        try{
          console.log(event);
          event.source.postMessage("Message from page", event.origin);
          console.log("Sent massage back to APP");
        }catch(error){
          console.log("Error on postMessage back to APP" + error);
        }
  
      };
      window.addEventListener('message', messageHandler, false);
  
      &lt;/script&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>