UIWebview于JS交互

时间:2022-02-22 13:42:22

最近使用火车票网的限行网页,但是广告以及头部nav和地步footer都是我们现在所不需要的,所以决定使用js交互,下面所有代码都写在 webViewDidFinishLoad 里面

1.查看原网址的源代码:

NSString *js1 = @"document.documentElement.innerHTML";

NSString *pageSource = [webView stringByEvaluatingJavaScriptFromString:js1];

 NSLog(@"pagesource:%@", pageSource);

2.编写js代码

  NSMutableString *js = [NSMutableString string];

    // 删除NAV

    [js appendString:@"var header = document.getElementById('huoche_nav');"];//获取id为huoche_nav节点

    [js appendString:@"header.parentNode.removeChild(header);"];//把节点从父试图中删除

    // 删除TOPBAR

    [js appendString:@"var box = document.getElementById('huoche_topbar');"];

    [js appendString:@"box.parentNode.removeChild(box);"];

    // 删除footer

    [js appendString:@"var buyNow = document.getElementById('footer');"];

    [js appendString:@"buyNow.parentNode.removeChild(buyNow);"];

    [js appendString:@"var form1 = document.getElementById('form1');"];

    [js appendString:@"form1.parentNode.removeChild(form1);"];

   [js appendString:@"var righttbox = document.getElementsByClassName('righttbox')[0];"];

    [js appendString:@"righttbox.parentNode.removeChild(righttbox);"];

    [js appendString:@"var height8 = document.getElementsByClassName('center_xianlu height8')[0];"];

    [js appendString:@"height8.parentNode.removeChild(height8);"];

3.在webview的代理方法webViewDidFinishLoad中执行js代码

  [webView stringByEvaluatingJavaScriptFromString:js];

4.如果需要使用uiwebview双指变大变小,需要添加

   webview.scalesPageToFit=YES;

    webview.multipleTouchEnabled=YES;

    webview.userInteractionEnabled=YES;

总结:

1.

NSURL *url = [[NSBundle mainBundle] URLForResource:@"index"

withExtension:@"html"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];

2.

NSString *str1 = @"var word =

document.getElementById('word');";
NSString *str2 = @"word.remove();";
[webView stringByEvaluatingJavaScriptFromString:str1];
[webView stringByEvaluatingJavaScriptFromString:str2];

3.

NSString *str3 = @"var change =

document.getElementsByClassName('change')[0];" "change.innerHTML = '!';";

    [webView stringByEvaluatingJavaScriptFromString:str3];

4.

NSString *str4 =@"var img = document.createElement('img');"

                     "img.src = 'img_01.jpg';"
"img.width = '160';"
"img.height = '80';"
"document.body.appendChild(img);";
[webView stringByEvaluatingJavaScriptFromString:str4];

5.

NSString *str1 = @"var h1 =

document.getElementsByTagName('h1')[0];" "h1.innerHTML='';";

    [webView stringByEvaluatingJavaScriptFromString:str1];

6.

NSString *str2

=@"document.getElementById('footer').remove();";
[webView stringByEvaluatingJavaScriptFromString:str2];

7.

NSString *str3 = @"document.body.outerHTML"; NSString *html = [webView

stringByEvaluatingJavaScriptFromString:str3];
NSLog(@"%@", html);

在html中调用OC
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:
(NSURLRequest *)request navigationType:
(UIWebViewNavigationType)navigationType{
NSString *str = request.URL.absoluteString;
NSRange range = [str rangeOfString:@"ZJY://"];
if (range.location != NSNotFound) {
NSString *method = [str substringFromIndex:range.location
+ range.length];
SEL sel = NSSelectorFromString(method);
[self performSelector:sel];
}return YES; }