我想添加特定的HTML来隐藏我的WKWebView上的一些内容。
我正在使用此代码:
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
NSString *cssString = @"#SITE_HEADER, #comp-imrw5tvg, #WRchTxt0-16wb, #comp-imrvq5jg, #comp-imrv0zgf, #SITE_FOOTERinlineContent, #SITE_FOOTER { display: none;}";
NSString *javascriptString = @"var style = document.createElement('style'); style.innerHTML = '%@'; document.head.appendChild(style)";
NSString *javascriptWithCSSString = [NSString stringWithFormat:javascriptString, cssString];
[webView evaluateJavaScript:javascriptWithCSSString completionHandler:^(id obj, NSError * _Nullable error) {
if (!webView.isLoading) {
[self.hud removeFromSuperview];
}
}];
}
也试过这段代码:
NSString *scriptString = @"var styleTag = document.createElement(\"style\"); styleTag.textContent = '#SITE_HEADER, #SITE_FOOTER {display:none;}'; document.documentElement.appendChild(styleTag);";
WKUserScript *script = [[WKUserScript alloc] initWithSource:scriptString injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
[userContentController addUserScript:script];
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.userContentController = userContentController;
self.wkWebView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
隐藏了正确的内容,但不幸的是,这留下了空白(但是,我正在使用display:none
}
如何实现此目的并删除此空白区域?