Android WebView Javascript getSelection

前端 未结 3 1744
后悔当初
后悔当初 2020-12-05 05:07

I am having some trouble getting the selection from a WebView in Android.

I can get the WebView to go into selection mode. I can even get it to copy the text to the

相关标签:
3条回答
  • 2020-12-05 05:15

    Finally, in Android 4.4 KitKat, the WebView is now based on Chromium.

    Therefore we have access to window.getSelection()!!

    wv.evaluateJavascript("console.log(window.getSelection().baseNode.nodeValue);", null);
    

    Tested on Nexus 5 & Nexus 7.

    0 讨论(0)
  • 2020-12-05 05:25

    When the WebView goes into "Selection Mode", the WebView is not actually being used for selection... It is being pushed under a "WebTextView" (private class in Android's arsenal) which mimics the text position, yet allows images to show through, and allows you to "select" the text which appears in the actual HTML. The problem comes when you try to interact with the WebView after "selecting" the text. The highlight and cursor handles are in the right position, but they are actually in the special WebTextView I mentioned, therefore you do not actually have a selection to get via JavaScript's getSelection, or any other means in JavaScript. I am working on making the ACTION_DOWN (of the LongPress) which triggers selection and the drag and ACTION_UP of the release from drag work for me via JavaScript, but it is very hairy, and not at all user friendly at this point...

    http://www.java2s.com/Open-Source/Android/android-core/platform-frameworks-base/android/webkit/WebTextView.java.htm

    check the source(that's a lot of work to mimic text selection instead of provide it) It is sad, and currently very painful for a project our team has undertaken -- especially after doing the same app for iPad...

    0 讨论(0)
  • 2020-12-05 05:37

    You should try rangy - A cross-browser JavaScript range and selection library.

    0 讨论(0)
提交回复
热议问题