使用从UIWebview检索HTML标记

时间:2013-04-18 12:23:12

标签: javascript ios uiwebview

我正在iOS上开发自定义webview,在网页上选择某些元素时应该为用户提供特殊选项,因此我正在扩展UIWebview并将我自己的按钮添加到sharedMenuController。因为显示的页面是使用xsl从xml构成的,所以在某些标签中有额外的数据,例如

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

选择图像时,会弹出sharedMenuController,如果我按下Action按钮,我希望收到包含img标签的标签。问题是使用window.getSelection()。innerHTML.toString()给我一个空字符串和window.getSelection()。getRangeAt(0).commonAncestorContainer.innerHTML.toString()什么应该是p-tag,给我整个HTML。

这是我的班级:

@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

4 个答案:

答案 0 :(得分:1)

试试这个:

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

找出问题所在。显然,当将单个对象放在p-tag中时,可以在选择对象时选择整个p-tag。因为p-tag是身体的孩子,所以返回完整的html是正确的。我现在在我的img周围使用一个简单的自定义标签,我正确地得到了值。

答案 3 :(得分:0)

根据Ajay的回答找到了我所寻找的真实答案:

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

指向非本机函数,因此没有它正在调用的javascript函数它是无用的。我在http://snipplr.com/view.php?codeview&id=10912找到了一个函数,似乎可以满足我的要求。因此,通过从我的xsl将这个JS注入我的页面,可以获得我获取数据所需的内容。

这是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 '';
            }
        };