简体   繁体   English

使用UIWebView,DOM和Javascript在HTML文档上指向Word

[英]Word on HTML document point, using UIWebView, DOM and Javascript

How can I figure out which word is at the point where the user tapped on a UIWebView ? 如何确定用户在UIWebView上点击的单词?

I am able to detect the CGPoint for the tap (subclassing UIWindow like this ), and I can actually get the DOM element on that point using javascript . 我能够检测到水龙头的CGPoint(像这样子类化UIWindow ),实际上我可以使用javascript获取该点的DOM元素。

But I know very little of javascript and DOM to figure out how can I actually get which word the user tapped on. 但我对javascript和DOM的了解很少,以弄清楚如何真正获得用户点击的单词。

Is that possible? 那可能吗? Here's what I have right now: 这是我现在所拥有的:

int scrollPosition = [[webView stringByEvaluatingJavaScriptFromString:@"window.pageYOffset"] intValue];
NSString *js = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).tagName", point.x, point.y+scrollPosition];
NSString *value = [webView stringByEvaluatingJavaScriptFromString:js];

NSLog(@"element: %@", value);

我不确定您将如何获得确切的单词,但是,我相信您可以在迭代它的子元素的元素中查找文本,并使用nodeType 3查找元素,即文本。

There was a method which I can't locate right now, which let you communicate info from UIWebView back "up" to the containing Cocoa app with some Javascript bridge. 有一种我现在无法找到的方法,它使您可以使用一些Javascript桥将UIWebView中的信息“向上”传递到包含Cocoa应用程序。 So I'd attach "click" handlers to all the elements of interest in the UIWebView, and fire some message to parent container with this magical bridge. 因此,我会将“ click”处理程序附加到UIWebView中所有感兴趣的元素上,并使用此神奇的桥将一些消息发送到父容器。

This assumes that you control the contents of UIWebView and can inject the needed Javascript. 假定您控制UIWebView的内容并且可以注入所需的Javascript。 If this needs to work with any webpage, it's harder. 如果这需要与任何网页一起使用,则难度会更大。

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

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