title: Inject content_script to new opened window
body: <p>I am developing an extension for personal use, which only automatically repeat some action on a website A.
  This is basically the flow I want to automate using Chrome extension:</p>
  
  <ol>
  <li>Trigger a button click event.</li>
  <li>A new popup window is opened (which is opened by website A).</li>
  <li>Fill form into the opened window.</li>
  <li>Click submit button.</li>
  </ol>
  
  <p>Currently, my extension is using Chrome's <code>pageAction</code> for the specific website I want to automate.</p>
  
  <p><strong><code>manifest.json</code></strong></p>
  
  <pre><code>{
    "manifest_version": 2,
  
    "name": "Automater",  
    "version": "0.0.1",
  
    "page_action": {
      "default_title": "Click here!",
      "default_icon": "icon.png"
    },
    "content_scripts": [
      {
        "matches": [
          "https://www.website.com/*"
        ],
        "js": ["jquery-3.2.1.min.js", "content.js"],
        "run_at": "document_end"
      }
    ],
    "background": {
      "scripts": ["background.js"],
      "persistent": false
    },
    "permissions": [
      "activeTab",
      "tabs",
      "declarativeContent",
      "storage"
    ]
  }
  </code></pre>
  
  <p><strong><code>background.js</code></strong></p>
  
  <pre><code>chrome.pageAction.onClicked.addListener(function(tab) {
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
      var activeTab = tabs[0];
      chrome.tabs.sendMessage(
        activeTab.id,
        { 'message': 'click_btn' }
      );
    });
  });
  
  chrome.runtime.onInstalled.addListener(function() {
    chrome.declarativeContent.onPageChanged.removeRules(
        undefined, function() {
      chrome.declarativeContent.onPageChanged.addRules([
        {
          conditions: [
            new chrome.declarativeContent.PageStateMatcher({
              pageUrl: {
                urlContains: 'www.website.com',
                schemes: ['https']
              },
            })
          ],
          actions: [ new chrome.declarativeContent.ShowPageAction() ]
        }
      ]);
    });
  });
  
  chrome.windows.onCreated.addListener(function(newWindow) {
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
      var activeTab = tabs[0];
      chrome.tabs.sendMessage(
        activeTab.id,
        { 'message': 'fill_form' }
      );
    });
  });
  
  chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
      if( request.message === 'btn_clicked' ) {
        citationsSize = request.citationsSize;
      }
    }
  );
  </code></pre>
  
  <p><strong><code>content.js</code></strong></p>
  
  <pre><code>chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
      switch(request.message) {
        case 'click_btn':
          $("#btn_id").click();
  
          chrome.runtime.sendMessage({
            "message": 'btn_clicked'          
          });
          break;
        case 'fill_form':
          console.log('start filling');        
      };
    }
  );
  </code></pre>
  
  <h2>My problem</h2>
  
  <p>I am on the website. After the extension icon is clicked, <code>$("#btn_id").click();</code> works and it opens a new window as a popup (this popup is opened by the website I'm currently on). I use <code>chrome.windows.onCreated</code> to catch the newly opened window, <strong>but, from here</strong>, I cannot send a message <code>fill_form</code> to the new window's content script.</p>
  
  <p>How can I execute the script in the newly opened popup window?</p>