title: Chrome Extension: Replace selected text in textarea with clipboard data
body: <p>I'm having a hell of a time trying to insert text into an HTML textarea, or grab data from the clipboard with a Chrome extension (using a context menu).</p>
  
  <p>I currently have these permissions in my manifest.json file:</p>
  
  <pre><code>"permissions": [
    "contextMenus",
    "clipboardRead",
    "clipboardWrite"],
  </code></pre>
  
  <p>My javascript skills are still pretty elementary, but despite spending quite a while on SO I can't seem to overcome security issues with document.execCode('paste') or the specific syntax I require to insert text in the text box.</p>
  
  <p>Programatically this is what I'd imagine the function would do:</p>
  
  <ol>
  <li>Get the selected string in the current textarea and copy to a
  variable "orig_str". </li>
  <li>If a string, get the current clipboard and
  store it in a variable "clip_str". If not, set variable to nothing.</li>
  <li>Overwrite the original selected string in the current textarea with
  "orig_str + clip_str".</li>
  </ol>
  
  <p>So if the textarea contained:</p>
  
  <pre><code>I eat peas
  </code></pre>
  
  <p>and the clipboard contained:</p>
  
  <pre><code> and cook
  </code></pre>
  
  <p>and the user selected "eat", then the function was activated, the string in the textarea would be changed to read:</p>
  
  <pre><code>I eat and cook peas
  </code></pre>
  
  <p>Any help down the right path would be appreciated, preferably with straight JS rather than JQuery.</p>