title: Fire Keydown event in Google Chrome extension
body: <p>I'm doing a Google Chrome extension that interacts with Gmail chat and can send the same message to all open chat, the functional part is done, but I can not send the <code>keydown</code> event to the textareas.</p>
  <p>What I am doing is using a <code>page_action</code> to show the extension icon when the user visit Gmail. When the user click the extension icon, it is going to prompt a popup with a textarea, when the user have opened chat and write something to the textarea and then press <kbd>Enter</kbd> key, the textarea of each opened chat are going to fill the same message and suppused to fire <code>keydown</code> event.</p>
  <p>Here is the essential code:</p>
  <p><strong>popup.js</strong></p>
  <pre><code>chrome.tabs.executeScript(null, {file: 'send.js'}, function () {
    // 'message' is the textarea inside the popup.html
    var message = document.getElementById('message');
  
    message.onkeydown = function (e) {
      if (13 == e.keyCode) {
        chrome.tabs.executeScript(null, {code: 'send(&quot;' + message.value + '&quot;);'}, function () {
          message.value = '';
        });
        return false;
      }
    };
  });
  </code></pre>
  <p><strong>send.js</strong></p>
  <pre><code>function send(message) {
    if (message) {
      for (var i = 0, textareas = document.getElementsByTagName(&quot;TEXTAREA&quot;), length = textareas.length; i &lt; length; ++i) {
        textarea = textareas[i];
        textarea.value = message;
  
        var evt = document.createEvent('KeyboardEvent');
  
        evt.initKeyboardEvent('keydown', true, true, null, false, false, false, false, 13, 13);
  
        Object.defineProperty(evt, 'keyCode', {
          get : function() {
              return 13;
          }
        });
  
        Object.defineProperty(evt, 'which', {
          get : function() {
              return 13;
          }
        });
  
        Object.defineProperty(evt, 'keyIdentifier', {
          get : function() {
              return 'Enter';
          }
        });
  
        Object.defineProperty(evt, 'shiftKey', {
          get : function() {
              return false;
          }
        });
  
        textarea.dispatchEvent(evt);
      }
    }
  }
  </code></pre>
  <p>The code did not have problem filling the textareas, so all textareas is reconized, but the <code>keydown</code> event did not fire after the script filled textareas. The strange part, is when I tried the <em>send.js</em> code into the Google Chrome console, the <code>keydown</code> event fire normally and the messages were send, but I can't do samething with the extension.</p>
  <p>So, how can I fire the <code>keydown</code> event into the extension?</p>