简体   繁体   English

使用UIWebview检索HTML标签

[英]Retrieving HTML-tags from UIWebview using

i'm developing a custom webview on iOS which should give users special options when selecting certain elements on a webpage, therefor I'm extending the UIWebview and am adding my own buttons to the sharedMenuController. 我正在开发iOS上的自定义Web视图,在选择网页上的某些元素时,应该为用户提供特殊的选择,因此,我正在扩展UIWebview并将自己的按钮添加到sharedMenuController。 Because the page shown is made up from xml using xsl to style it, there is extra data in certain tags, eg 由于显示的页面是使用xsl对其样式进行设置的,它是由xml组成的,因此某些标记中会包含额外的数据,例如

<p data-type="MC"><img src="annotation.png"></p>

When selecting the image, the sharedMenuController pops up, and if i press the Action button, i would like to receive the tag containing the img tag. 选择图像时,会弹出sharedMenuController,如果我按“操作”按钮,我想接收包含img标签的标签。 The problem is that using window.getSelection().innerHTML.toString() gives me an empty string and window.getSelection().getRangeAt(0).commonAncestorContainer.innerHTML.toString() what should be the p-tag, gives me the entire html. 问题是使用window.getSelection()。innerHTML.toString()给我一个空字符串,而window.getSelection()。getRangeAt(0).commonAncestorContainer.innerHTML.toString()应该是p标记,这给了我整个html。

This is my class: 这是我的课:

@implementation UICustomWebView

+ (void)initialize
{
    [super initialize];
    UIMenuItem *itemA = [[UIMenuItem alloc] initWithTitle:@"Action" action:@selector(a:)];
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:itemA, nil]];    
}

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{    
    if (action == @selector(defineSelection:))
    {
        return YES;
    }
    else if (action == @selector(translateSelection:))
    {
        return YES;
    }
    else if (action == @selector(copy:))
    {
        return NO;
    }
    else if ( action == @selector( a: ) )
    {
        return YES;
    }

    return [super canPerformAction:action withSender:sender];
}

-(void) a:(id)sender
{
    NSLog(@"a %@", [self stringByEvaluatingJavaScriptFromString:@"window.getSelection().getRangeAt(0).commonAncestorContainer.innerHTML.toString()"]);
}
@end

尝试这个:

 NSString *htmlString=[[webView stringByEvaluatingJavaScriptFromString:@"getSelectionHtml()"]mutableCopy];
  NSString *htmlString = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];

Found out what the problem was. 找出问题所在。 Apparently when putting a single object in a p-tag makes you select the entire p-tag when selecting the object. 显然,将单个对象放在p标签中时,可以在选择对象时选择整个p标签。 Therefor it was correct that the full html was returned since the p-tag was a child of the body. 因此,正确的是返回完整的html,因为p标签是人体的孩子。 I'm using a simple custom tag around my img now and i get the values correctly. 我现在在img周围使用一个简单的自定义标签,并且可以正确获取值。

Found the real answer of what i was looking for based upon the answer of Ajay: 根据Ajay的答案找到了我要找的真正答案:

NSString *htmlString=[[webView stringByEvaluatingJavaScriptFromString:@"getSelectionHtml()"]mutableCopy]; NSString * htmlString = [[webView stringByEvaluatingJavaScriptFromString:@“ getSelectionHtml()”] mutableCopy];

points to a non-native function, therefor it is useless without the javascript function that it's calling. 指向非本机函数,因此没有它正在调用的javascript函数就没有用。 I found a function at http://snipplr.com/view.php?codeview&id=10912 which seems to do what i require. 我在http://snipplr.com/view.php?codeview&id=10912找到了一个功能,它似乎可以满足我的要求。 So by injecting this JS into my page from my xsl gives me what i require to get my data. 因此,通过将xs从我的xsl注入到我的页面中,我得到了获取数据的条件。

This is the JavaScript function: 这是JavaScript函数:

function getSelectionHTML()
        {
            var userSelection;
            if (window.getSelection) 
            {
                // W3C Ranges
                userSelection = window.getSelection ();
                // Get the range:
                if (userSelection.getRangeAt)
                    var range = userSelection.getRangeAt (0);
                else 
                {
                    var range = document.createRange ();
                    range.setStart (userSelection.anchorNode, userSelection.anchorOffset);
                    range.setEnd (userSelection.focusNode, userSelection.focusOffset);
                }
                // And the HTML:
                var clonedSelection = range.cloneContents ();
                var div = document.createElement ('div');
                div.appendChild (clonedSelection);
                return div.innerHTML;
            } 
            else if (document.selection)
            {
                // Explorer selection, return the HTML
                userSelection = document.selection.createRange ();
                return userSelection.htmlText;
            } 
            else 
            {
                return '';
            }
        };

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

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