简体   繁体   English

Android:window.getSelection()在Webview中不起作用

[英]Android: window.getSelection() does not work in webview

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. 您可能会认为操作系统将能够像iOS一样处理某些事情,但这是Android完全不足的众多地方之一。 I've probed this website and google and found nothing that could help me. 我已经调查过该网站和Google,但没有发现任何可以帮助我的信息。 My app, luckily has a javascript interface, to communicate with a script that we load into the webview. 幸运的是,我的应用程序具有JavaScript界面​​,可与我们加载到Web视图中的脚本进行通信。 I thought i could use javascript to select the text from the webview. 我以为我可以使用javascript从webview中选择文本。 In chrome i can do this with the following bit of javascript: 在chrome中,我可以使用以下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. 现在我有一个按钮,该按钮在我的js文件中调用一个函数,该函数将运行上述命令,并将其结果返回给我的javascript接口中的方法。 That message will toast the result of my javascript function. 该消息将敬酒我的javascript函数的结果。 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(); 当我删除'.toString(); part from my javascript command and select text and press a button i get teh following message 从我的javascript命令中选择并选择文本,然后按一下按钮,我收到以下消息

javascript returned: undefined

Why isn't this working? 为什么这不起作用?

Here's my code in context. 这是上下文中的代码。 This is hte javascript that gets called: 这是被称为的javascript:

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 这是它正在调用的Java函数

public void getSelectedText(String text)
        {
            String str = "function called.  it returned: " + text;
            Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
        }

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). 注意:不要在post或任何可运行的接口内部调用此方法,因为post或任何可运行的接口会延迟调用此方法(在释放浏览器选择之后发生方法调用)。 Just call this method like 就像这样调用这个方法

webView.loadUrl("javascript:getSelectedText()");

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 - 因为文本选择是在android上的单独类WebTextView上发生的,所以如果我们尝试使用javascript方法来获取所选单词,

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. 但是如果您希望所选的单词,此函数将不返回任何内容,如果您希望稍后再添加该代码,则可以对此使用反射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM