title: Communication from chrome extension to web page
body: <p>I know it's possible to postMessage from a web page to a chrome extension and get an ok response back to the web page, <strong>but is it possible the other way around ?</strong></p>
  
  <p><strong><em>My question</em></strong> "I need to be able to sent a request into a webpage and wait for a response that I can use in the extension. Maybe not with postMessage ?"</p>
  
  <p>I know the docs says "Only the web page can initiate a connection." (<a href="https://developer.chrome.com/extensions/messaging#external-webpage" rel="nofollow noreferrer">https://developer.chrome.com/extensions/messaging#external-webpage</a>) </p>
  
  <p>This is what I have tried so far.</p>
  
  <p><em>background.js</em></p>
  
  <pre><code>chrome.extension.sendMessage("Hello from extension to webpage");
  window.postMessage("Hello from extension to webpage");
  chrome.tabs.sendMessage(TAB_ID, "Hello from extension to webpage");
  </code></pre>
  
  <p>webpage.js (not part of the extension)</p>
  
  <pre><code>window.onmessage = (event) =&gt; {
    // Waiting for that message.
    console.info("Event received ", event);
  }
  window.chrome.runtime.connect(CHROME_EXTENSION_APP_ID).onMessage.addListener(function (){
    function(request, sender, sendResponse) {
       console.info("Event received from the extension", event);
    }
  })  
  </code></pre>
  
  <p><strong>Any idea will be appreciated</strong> :)</p>