- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
// 1. 获取iframe src,多个尝试
NSString *js = @"document.getElementsByTagName('iframe')[0].src";
//NSString *js = @"var iframe = document.querySelector('iframe');";
//NSString *js = @"var iframes = document.getElementsByTagName('iframe'); var src = iframes[0].src;";
//NSString *js = @"Array.from(document.getElementsByTagName('iframe')).map(frame => frame.src);" ;
[webView evaluateJavaScript:js completionHandler:^(id result, NSError *error) {
NSLog(@"===Error===:%@",error);
if (!error){
NSString *originSrc = result;
NSLog(@"===Result===:%@",originSrc);
// 2. 构造设置src代码
// NSString *js = [NSString stringWithFormat:@"iframes[0].src = \"%@\";", newSrc];
// NSString *modifiedIframeSrc = [iframeSrc stringByReplacingOccurrencesOfString:@"http://***" withString:@"https://***"]; // 替换成你的实际链接
// 3. 执行设置src操作
// [webView evaluateJavaScript:js completionHandler:nil];
}
}];
// 4. WKWebView会重新加载iframe新的页面
}
网友评论