1

I have an app with a webview and I have been trying to get user-selected text from the body of the webview. You would think that the operating system would be able to handle something like the way iOS does, but this one of the many places where Android is utterly inadequate. I've probed this website and google and found nothing that could help me. My app, luckily has a javascript interface, to communicate with a script that we load into the webview. I thought i could use javascript to select the text from the webview. In chrome i can do this with the following bit of javascript:

window.getSelection.toString();

Right now i have a button which calls a function in my js file that will run the above command and return it's results to a method in my javascript interface. That message will toast the result of my javascript function. So i select text and then press this button. The only problem is that my toast message returns the following message:

javascript returned:  

when it should return

javascript returned: <my selected text>

When i remove the '.toString(); part from my javascript command and select text and press a button i get teh following message

javascript returned: undefined

Why isn't this working?

Here's my code in context. This is hte javascript that gets called:

myNameSpace.getTextSelection = function()
        {
            var str;
            if (window.getSelection){
                str = window.getSelection().toString();
            } else {
                str = 'does not';
            }
            window.myJSHandler.getSelectedText(str);
        };

and here's the java function it is calling

public void getSelectedText(String text)
        {
            String str = "function called.  it returned: " + text;
            Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
        }
aamiri
  • 2,420
  • 4
  • 38
  • 58

2 Answers2

1

Got solution which works for me

//I am using below line of code which works in both android and web browsers.

function getSelectedText() {
    var selection = null;

    if (window.getSelection) {
        selection = window.getSelection();
    } else if (typeof document.selection != "undefined") {
        selection = document.selection;
    }

    var selectedRange = selection.getRangeAt(0);

    console.log(selectedRange.toString());
}

NOTE : Don't call this method in post or inside any runnable interface as post or any runnable interface make delay in calling this method(Method call happens after browser selection release). Just call this method like

webView.loadUrl("javascript:getSelectedText()");
Abhishek
  • 3,348
  • 3
  • 15
  • 34
0

Because the text selection is happens in android on a separate class WebTextView so if we tryies to get the selected word using javascript method -

var range = window.getSelection ();  

but this functions returns nothing if you want the selected word you can use reflection for that in case if you want that i will add that code later.

Ravi
  • 2,277
  • 3
  • 22
  • 37