title: Chrome extension: Open new tab and click on button
body: <p>i have created an chrome extension which logs the user into his account.
  On the popup.html you can choose the login url (We have 2 portals EU and US) and type in your login credentials. After that you click the button "Login". Then a new tab with the url will open, and the extension will fill the login form with username and password. Then it should click the button on the new loginform, this wont work.</p>
  
  <p>manifest.json</p>
  
  <pre><code>{
    "manifest_version": 2,
  
    "name": "mbCONNECT24 Login",
    "short_name": "MBCL_Login",
    "description": "This extension log you into your mbCONNECT24 account",
    "version": "1.7",
    "options_page": "options.html",
    "icons": { "16": "icon16.png",
               "48": "icon48.png",
              "128": "icon128.png" },
    "browser_action": {
      "default_icon": "icon.png",
      "default_popup": "popup.html"
    },
  
    "author": "Johannes Regner &lt;johannes.regner@mbconnectline.de&gt;",
    "permissions": [
      "activeTab",
      "&lt;all_urls&gt;","*://*/*",
      "storage"
  
    ]
  }
  </code></pre>
  
  <p>Popup.html</p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;html&gt;
    &lt;head&gt;
      &lt;title&gt;mbCONNECT24 Login&lt;/title&gt;
      &lt;script src="popup.js"&gt;&lt;/script&gt;
      &lt;script src="options.js"&gt;&lt;/script&gt;
      &lt;link href="bootstrap.min.css" rel="stylesheet"&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;div style="padding:20px;"&gt;
        &lt;img src="mbconnect24.png" /&gt;
      &lt;form&gt;
  
        &lt;div class="form-group"&gt;
          &lt;label for="portals"&gt;Server&lt;/label&gt;
          &lt;select id="portals" class="form-control"&gt;
            &lt;option value="https://rsp.mbconnect24.net/portal/"&gt;mbCONNECT24 RSP EU&lt;/option&gt;
            &lt;option value="https://rsp.mbconnect24.us/portal/"&gt;mbCONNECT24 RSP US&lt;/option&gt;
          &lt;/select&gt;
        &lt;/div&gt;
        &lt;div class="form-group"&gt;
          &lt;label for="username"&gt;Username&lt;/label&gt;
        &lt;input type="text" class="form-control" id="username" placeholder="Username"&gt;
        &lt;/div&gt;
        &lt;div class="form-group"&gt;
          &lt;label for="password"&gt;Password&lt;/label&gt;
          &lt;input type="password" class="form-control" id="password" placeholder="Password"&gt;
        &lt;/div&gt;
        &lt;button id="gotoLogin" class="btn btn-success"&gt;Login&lt;/button&gt;
      &lt;/form&gt;
  
  &lt;/div&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>My code in popup.js is the following:</p>
  
  <pre><code>window.addEventListener("load", function()
  {
    document.getElementById("gotoLogin")
            .addEventListener("click", gotoLogin, false);
  }, false);
  
  
  function gotoLogin() {
    var selectlist = document.getElementById("portals");
    var url = selectlist.value;
  
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;
  
  
    var logincode = 'var loginField = document.getElementById("modlgn_username");'+
               'var passwordField = document.getElementById("modlgn_passwd");'+
               'loginField.value = "'+username+'";passwordField.value ="'+password+'";'+
               'document.getElementById("loginBtn").click();';
  
    var tabId = null;
  
    //var tabs = chrome.extension.getViews();
    //console.log(tabs);
    chrome.tabs.create({"url": url}, function(tab){
      tabId = tab.id;
  
      //chrome.tabs.executeScript(tab.id, {file: 'content.js', runAt: 'document_end',code: 'var myUsername = "'+username+'";'});
    });
  
     chrome.tabs.update(tabId, {url: url});
     chrome.tabs.executeScript(tabId, {code: logincode, runAt:"document_end",allFrames:true}, function(result){
     });
  
  }
  </code></pre>
  
  <p>Maybe its the wrong way? Now it opens the new tab and fill the form, but don't click the button. If i type the command 'document.getElementById("loginBtn").click();' in console, the button will clicked.</p>
  
  <p>Thank you!</p>