title: How to get selected text in chrome extension development?
body: <p>I'm developing a chrome extension which involves getting selected text of the current tab. Here is the html file that I use:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
      &lt;head&gt;
          &lt;meta charset="UTF-8"&gt;
          &lt;title&gt;&lt;/title&gt;
          &lt;script&gt;
              chrome.tabs.executeScript( {
                  code: "window.getSelection().toString();"
              }, function(selection) {
                  document.getElementById("output").value = selection[0];
              });
          &lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;div id="output"&gt;&lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>It doesn't work, why? As followed is the error message from <strong>Chrome App &amp; Extension Developer Tool</strong>, some of these error messages are cut with ellipses, sorry I haven't figured out how to view the full error messages here, <strong>view details</strong> only gives me the stack track, not the full error message.</p>
  
  <p><a href="https://i.stack.imgur.com/dtk6T.png" rel="noreferrer"><img src="https://i.stack.imgur.com/dtk6T.png" alt="enter image description here"></a></p>