title: Sending messages to page Javascript from a content script
body: <p>I have a method in the Javascript runtime for a tab that I want to fire from my extension's content script. At face value, this calls for message passing between contexts, but I cannot find any documentation of CS-to-tab messages, only tab-to-CS or CS-to-background. Essentially I want to flip the <a href="https://developer.chrome.com/extensions/content_scripts#host-page-communication" rel="nofollow noreferrer">Communication with the embedding page</a> section of the content scripts reference.</p>
  <p>So far, I am attaching an event listener to the tab's <code>window</code> by injecting a small Javascript payload (this will trigger the tab method on the correct type of message):</p>
  <pre><code>listenerScript = document.createElement('script');
  listenerScript.textContent = &quot;window.addEventListener('message',function(ev){console.log('New event: ' + ev);},false);console.log('installed');&quot;;
  (document.head||document.documentElement).appendChild(listenerScript);
  listenerScript.remove();
  </code></pre>
  <p>This listener is registered, however I can't figure out how to trigger it from my content script. <code>window.postMessage()</code> in the content script doesn't seem to do anything, and the documentation only talks about using <code>chrome.runtime</code> to create a port, which is for background scripts.</p>
  <p>How do I pass messages, in the simplest way possible, from content script to tab?</p>