|
|
@@ -132,13 +132,42 @@ extension LNWebViewController {
|
|
|
|
|
|
private func buildWebView() -> UIView {
|
|
|
let webConfig = WKWebViewConfiguration()
|
|
|
+ setupDefaultLocalStorage(webConfig)
|
|
|
|
|
|
- let tokenScript = WKUserScript(source: "window.localStorage.setItem('GAMI-web_auth_token', '\(LNAccountManager.shared.token)')",
|
|
|
- injectionTime: .atDocumentStart, forMainFrameOnly: true)
|
|
|
+ let webView = WKWebView(frame: .zero, configuration: webConfig)
|
|
|
+ webView.navigationDelegate = self
|
|
|
+#if DEBUG
|
|
|
+ if #available(iOS 16.4, *) {
|
|
|
+ webView.isInspectable = true
|
|
|
+ }
|
|
|
+#endif
|
|
|
+ webView.scrollView.contentInsetAdjustmentBehavior = .never
|
|
|
+ webView.scrollView.bounces = false
|
|
|
+
|
|
|
+ setupDefaultCookie(webView)
|
|
|
+
|
|
|
+ webView.publisher(for: \.title).removeDuplicates().sink
|
|
|
+ { [weak self] webTitle in
|
|
|
+ guard let self else { return }
|
|
|
+ guard config.customTitle.isEmpty else { return }
|
|
|
+
|
|
|
+ title = webTitle
|
|
|
+ }.store(in: &bag)
|
|
|
+
|
|
|
+ self.webView = webView
|
|
|
+
|
|
|
+ return webView
|
|
|
+ }
|
|
|
+
|
|
|
+ private func setupDefaultLocalStorage(_ webConfig: WKWebViewConfiguration) {
|
|
|
+ let tokenScript = WKUserScript(
|
|
|
+ source: "window.localStorage.setItem('GAMI-web_auth_token', '\(LNAccountManager.shared.token)')",
|
|
|
+ injectionTime: .atDocumentStart, forMainFrameOnly: true)
|
|
|
webConfig.userContentController.addUserScript(tokenScript)
|
|
|
|
|
|
- let languageScript = WKUserScript(source: "window.localStorage.setItem('GAMI-web_app_locale', '\(LNAppConfig.shared.curLang.bundleName)')",
|
|
|
- injectionTime: .atDocumentStart, forMainFrameOnly: true)
|
|
|
+ let languageScript = WKUserScript(
|
|
|
+ source: "window.localStorage.setItem('GAMI-web_app_locale', '\(LNAppConfig.shared.curLang.bundleName)')",
|
|
|
+ injectionTime: .atDocumentStart, forMainFrameOnly: true)
|
|
|
webConfig.userContentController.addUserScript(languageScript)
|
|
|
|
|
|
var header: [String: Any] = [:]
|
|
|
@@ -158,27 +187,29 @@ extension LNWebViewController {
|
|
|
injectionTime: .atDocumentStart, forMainFrameOnly: true)
|
|
|
webConfig.userContentController.addUserScript(headerScript)
|
|
|
}
|
|
|
-
|
|
|
- let webView = WKWebView(frame: .zero, configuration: webConfig)
|
|
|
- webView.navigationDelegate = self
|
|
|
-#if DEBUG
|
|
|
- if #available(iOS 16.4, *) {
|
|
|
- webView.isInspectable = true
|
|
|
+ }
|
|
|
+
|
|
|
+ private func setupDefaultCookie(_ webView: WKWebView) {
|
|
|
+ // 创建 Cookie
|
|
|
+ if let tokenCookie = HTTPCookie(properties: [
|
|
|
+ .domain: ".gami.vip",
|
|
|
+ .path: "/",
|
|
|
+ .name: "GAMI-cookie_auth_token",
|
|
|
+ .value: LNAccountManager.shared.token,
|
|
|
+ .secure: "TRUE",
|
|
|
+ .expires: NSDate(timeIntervalSinceNow: 3600)
|
|
|
+ ]) {
|
|
|
+ webView.configuration.websiteDataStore.httpCookieStore.setCookie(tokenCookie)
|
|
|
+ }
|
|
|
+ if let languageCookie = HTTPCookie(properties: [
|
|
|
+ .domain: ".gami.vip",
|
|
|
+ .path: "/",
|
|
|
+ .name: "GAMI-cookie_app_locale",
|
|
|
+ .value: LNAppConfig.shared.curLang.bundleName,
|
|
|
+ .secure: "TRUE",
|
|
|
+ .expires: NSDate(timeIntervalSinceNow: 3600)
|
|
|
+ ]) {
|
|
|
+ webView.configuration.websiteDataStore.httpCookieStore.setCookie(languageCookie)
|
|
|
}
|
|
|
-#endif
|
|
|
- webView.scrollView.contentInsetAdjustmentBehavior = .never
|
|
|
- webView.scrollView.bounces = false
|
|
|
-
|
|
|
- webView.publisher(for: \.title).removeDuplicates().sink
|
|
|
- { [weak self] webTitle in
|
|
|
- guard let self else { return }
|
|
|
- guard config.customTitle.isEmpty else { return }
|
|
|
-
|
|
|
- title = webTitle
|
|
|
- }.store(in: &bag)
|
|
|
-
|
|
|
- self.webView = webView
|
|
|
-
|
|
|
- return webView
|
|
|
}
|
|
|
}
|