gpt4 book ai didi

ios - Swift:向 WKWebView 注入(inject) JS 和 CSS 不起作用

转载 作者:可可西里 更新时间:2023-10-31 23:47:13 25 4
gpt4 key购买 nike

我想尝试将 js 脚本注入(inject) WKWebView,但它不起作用。所以它是一个网页,但是当它加载到 App 上时,我们应该隐藏页眉和页脚。我从配置中获取 CSS URL 并加载它,然后尝试将它与代码中编写的 JS 一起注入(inject)。但是,网络已加载,但预期的事情不起作用。怎么了?

这是代码(我省略了许多与问题无关的代码)

import UIKit
import WebKit

class CustomPageWebViewController: UIViewController, WKNavigationDelegate {

private var webView: WKWebView = WKWebView()


override func viewDidLoad() {
super.viewDidLoad()

webView.navigationDelegate = self
view.addSubview(webView)

// Request web page
requestWebPage()

}


// MARK: - Request Web Page

func requestWebPage() {
if let urlString = URL(string: determineURLBasedOnPaymentName()) {
webView.load(URLRequest(url: urlString))
}
}


//

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
removeHeaderAndFooter(webView: webView)
}

func removeHeaderAndFooter(webView: WKWebView) {
let cssURLString = ConfigManager.sharedInstance.cssURL
if cssURLString != "" {
let cssURL = URL(string: cssURLString)
if let cssURL = cssURL {
let sessionConfig = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil)
var request = URLRequest(url: cssURL)
request.httpMethod = "GET"

let task = session.dataTask(with: request, completionHandler: { (data: Data!, response: URLResponse!, error: Error!) -> Void in
if (error == nil) {
let result = String(data: data, encoding: String.Encoding.utf8)!

var js = "!function(){var e=document.createElement(\"style\");e.type=\"text/css\",e.innerHTML=window.atob(%@),document.getElementsByTagName(\"head\")[0].appendChild(e);var t=document.createElement(\"style\");t.type=\"text/css\",t.innerHTML=\"#tab-hotel,#tabs-2,.rail-order__flex{display:none;} .search__label--input{height:45px;} #tabs-2{display:block;}\",document.body.appendChild(t)}();"
// So after I get the CSS file from the link, I will inject it to the JS file here
js = String(format: js, result)

// And then inject the whole script to the webview
self.webView.evaluateJavaScript(js, completionHandler: { (any, error) in

})
}
})
task.resume()
}
}
}
}

最佳答案

WKWebview 中的 js 消息处理程序执行是使用 WKUserScript 处理的

你可以按照我在链接 WKUserScript not working 中的回答执行 JS

这是我们在WKWebview中执行JS的方式

[objectofWKWebView evaluateJavaScript:yourScript completionHandler:^(id result, NSError *error) {}];

您可以创建 WKWebView 的类别并添加类似的功能

- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script {
__block NSString *resultString = nil;
__block BOOL finished = NO;

[self evaluateJavaScript:script completionHandler:^(id result, NSError *error) {
if (error == nil) {
if (result != nil) {
resultString = [NSString stringWithFormat:@"%@", result];
}
} else {
NSLog(@"evaluateJavaScript error : %@", error.localizedDescription);
}
finished = YES;
}];

while (!finished)
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}

return resultString;
}

关于ios - Swift:向 WKWebView 注入(inject) JS 和 CSS 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44017501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com