将这 2 个 javascript 函数放在一个名为 JSTools.js 的文件中
function getHTMLElementsAtPoint(x,y) {
var tags = "";
var e;
var offset = 0;
while ((tags.search(",(A|IMG),") < 0) && (offset < 20)) {
tags = ",";
e = document.elementFromPoint(x,y+offset);
while (e) {
if (e.tagName) {
tags += e.tagName + ',';
}
e = e.parentNode;
}
if (tags.search(",(A|IMG),") < 0) {
e = document.elementFromPoint(x,y-offset);
while (e) {
if (e.tagName) {
tags += e.tagName + ',';
}
e = e.parentNode;
}
}
offset++;
}
return tags;}
function getLinkSRCAtPoint(x,y) {
var tags = "";
var e = "";
var offset = 0;
while ((tags.length == 0) && (offset < 20)) {
e = document.elementFromPoint(x,y+offset);
while (e) {
if (e.src) {
tags += e.src;
break;
}
e = e.parentNode;
}
if (tags.length == 0) {
e = document.elementFromPoint(x,y-offset);
while (e) {
if (e.src) {
tags += e.src;
break;
}
e = e.parentNode;
}
}
offset++;
}
return tags;}
然后在你的控制器中
- (void)longPressRecognized:(UILongPressGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
CGPoint point = [gestureRecognizer locationInView:webView];
longpressTouchedPoint = point;
// convert point from view to HTML coordinate system
CGSize viewSize = [webView frame].size;
CGSize windowSize = [webView windowSize];
CGFloat f = windowSize.width / viewSize.width;
if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 5.) {
point.x = point.x * f;
point.y = point.y * f;
} else {
// On iOS 4 and previous, document.elementFromPoint is not taking
// offset into account, we have to handle it
CGPoint offset = [webView scrollOffset];
point.x = point.x * f + offset.x;
point.y = point.y * f + offset.y;
}
// Load the JavaScript code from the Resources and inject it into the web page
NSString *path = [[NSBundle mainBundle] pathForResource:@"JSTools" ofType:@"js"];
NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[webView stringByEvaluatingJavaScriptFromString: jsCode];
// call js functions
NSString *tags = [webView stringByEvaluatingJavaScriptFromString:
[NSString stringWithFormat:@"getHTMLElementsAtPoint(%i,%i);",(NSInteger)point.x,(NSInteger)point.y]];
NSString *tagsSRC = [webView stringByEvaluatingJavaScriptFromString:
[NSString stringWithFormat:@"getLinkSRCAtPoint(%i,%i);",(NSInteger)point.x,(NSInteger)point.y]];
NSLog(@"src : %@",tags);
NSLog(@"src : %@",tagsSRC);
NSString *url = nil;
if ([tags rangeOfString:@",IMG,"].location != NSNotFound) {
url = tagsSRC; // Here is the image url!
}
来自多个来源的混合,归功于网络社区!
编辑
您还必须使用这两个功能在 uiwebview 上创建一个类别:
- (CGSize)windowSize {
CGSize size;
size.width = [[self stringByEvaluatingJavaScriptFromString:@"window.innerWidth"] integerValue];
size.height = [[self stringByEvaluatingJavaScriptFromString:@"window.innerHeight"] integerValue];
return size;}
- (CGPoint)scrollOffset {
CGPoint pt;
pt.x = [[self stringByEvaluatingJavaScriptFromString:@"window.pageXOffset"] integerValue];
pt.y = [[self stringByEvaluatingJavaScriptFromString:@"window.pageYOffset"] integerValue];
return pt;}