title: Android WebView Javascript getSelection
body: <p>I am having some trouble getting the selection from a WebView in Android.</p>
  
  <p>I can get the WebView to go into selection mode. I can even get it to copy the text to the ClipBoard. But what I really want to do is highlight the selection permanently. </p>
  
  <p>So the idea is put the WebView in select mode. Let the user select the text, and then fire something to highlight that text. I can get it to work by getting the selected text from the clipboard, and then search for it in Javascript and highlight it. The problem occurs when the user selects a real common word. I have to either highlight them all or somehow figure out where the selection is to get the right one. </p>
  
  <p>I have tried this JavaScript which works on the iPhone. Bu getSelection() does not seem to work on the Android. </p>
  
  <pre><code>function highlight(colour) {
      var range, sel;
      if (window.getSelection) {
              // Non-IE case
          sel = window.getSelection();
          if (sel.getRangeAt) {
              range = sel.getRangeAt(0);
          }
          document.designMode = "on";
          if (range) {
              sel.removeAllRanges();
              sel.addRange(range);
          }
              // Use HiliteColor since some browsers apply BackColor to the whole block
          if ( !document.execCommand("HiliteColor", false, colour) ) {
              document.execCommand("BackColor", false, colour);
          }
          document.designMode = "off";
      } else if (document.selection &amp;&amp; document.selection.createRange) {
              // IE case
          range = document.selection.createRange();
          range.execCommand("BackColor", false, colour);
      }
  }
  </code></pre>
  
  <p>Any suggestions?</p>