Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get word on indicated location on WKWebView?

When you long press a word on a WKWebView webpage, you will select it, and system will popup a menu to let you share/copy/lookup it so on. This is the iOS default behavior.

So my question is, how do you programmatically get a word from indicated point (CGPoint) on the WKWebView webpage?

Or, how do you programmatically select a word on WKWebView webpage without long press?

Any advice appreciated!

like image 683
Jonny Avatar asked Nov 08 '22 05:11

Jonny


1 Answers

First you must convert the point to the scrollview's coordinate system using convertPoint:toView:. You can use this point in a Javascript (with jQuery) function which will search the DOM node at this point:

function(inX, inY) {
    var theWindow = $(window);
    var theElement = document.elementFromPoint(
        inX - theWindow.scrollLeft(), inY - theWindow.scrollTop());

    return theElement == null ? null : theElement.textContent;
}
like image 121
clemens Avatar answered Nov 14 '22 22:11

clemens