From 008c92e9c1d177471df32f8f6154a30e151a1987 Mon Sep 17 00:00:00 2001 From: yicheng <11733500+yichengchen@users.noreply.github.com> Date: Thu, 15 Jun 2023 14:08:45 +0800 Subject: [PATCH] fix: delete webview cache when version update, remove git dir in dashboard folder, add more log in dashboard window --- ClashX.xcodeproj/project.pbxproj | 8 +-- ClashX/General/Utils/AppVersionUtil.swift | 1 + .../ClashWebViewContoller.swift | 57 +++++++++++++------ install_dependency.sh | 2 + 4 files changed, 47 insertions(+), 21 deletions(-) diff --git a/ClashX.xcodeproj/project.pbxproj b/ClashX.xcodeproj/project.pbxproj index 5b74837..68cf9ff 100644 --- a/ClashX.xcodeproj/project.pbxproj +++ b/ClashX.xcodeproj/project.pbxproj @@ -1008,7 +1008,7 @@ CODE_SIGN_STYLE = Manual; COMBINE_HIDPI_IMAGES = YES; COMPRESS_PNG_FILES = YES; - CURRENT_PROJECT_VERSION = 1.97.0; + CURRENT_PROJECT_VERSION = 1.116.2; DEVELOPMENT_TEAM = MEWHFZ92DY; ENABLE_HARDENED_RUNTIME = YES; FRAMEWORK_SEARCH_PATHS = ( @@ -1030,7 +1030,7 @@ "$(PROJECT_DIR)/ClashX/goClash", ); MACOSX_DEPLOYMENT_TARGET = 10.14; - MARKETING_VERSION = 1.97.0; + MARKETING_VERSION = 1.116.2; PRODUCT_BUNDLE_IDENTIFIER = com.west2online.ClashX; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; @@ -1055,7 +1055,7 @@ COMBINE_HIDPI_IMAGES = YES; COMPRESS_PNG_FILES = YES; COPY_PHASE_STRIP = YES; - CURRENT_PROJECT_VERSION = 1.97.0; + CURRENT_PROJECT_VERSION = 1.116.2; DEPLOYMENT_POSTPROCESSING = YES; DEVELOPMENT_TEAM = MEWHFZ92DY; ENABLE_HARDENED_RUNTIME = YES; @@ -1078,7 +1078,7 @@ "$(PROJECT_DIR)/ClashX/goClash", ); MACOSX_DEPLOYMENT_TARGET = 10.14; - MARKETING_VERSION = 1.97.0; + MARKETING_VERSION = 1.116.2; OTHER_CODE_SIGN_FLAGS = "--timestamp"; PRODUCT_BUNDLE_IDENTIFIER = com.west2online.ClashX; PRODUCT_NAME = "$(TARGET_NAME)"; diff --git a/ClashX/General/Utils/AppVersionUtil.swift b/ClashX/General/Utils/AppVersionUtil.swift index 9ef4ff4..dcb9600 100644 --- a/ClashX/General/Utils/AppVersionUtil.swift +++ b/ClashX/General/Utils/AppVersionUtil.swift @@ -44,6 +44,7 @@ class AppVersionUtil: NSObject { extension AppVersionUtil { static func showUpgradeAlert() { if let lastVersion = shared.lastVersionNumber, hasVersionChanged { + WebCacheCleaner.clean() guard lastVersion.compare("1.30.0", options: .numeric) == .orderedAscending else { return } let alert = NSAlert() alert.messageText = NSLocalizedString("This version of ClashX contains a break change due to clash core 1.0 released. Check if your config is not working properly.", comment: "") diff --git a/ClashX/ViewControllers/ClashWebViewContoller.swift b/ClashX/ViewControllers/ClashWebViewContoller.swift index 90642d1..0d89f1f 100644 --- a/ClashX/ViewControllers/ClashWebViewContoller.swift +++ b/ClashX/ViewControllers/ClashWebViewContoller.swift @@ -12,6 +12,19 @@ import RxSwift import WebKit import WebViewJavascriptBridge +enum WebCacheCleaner { + static func clean() { + HTTPCookieStorage.shared.removeCookies(since: Date.distantPast) + Logger.log("[WebCacheCleaner] All cookies deleted") + WKWebsiteDataStore.default().fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in + records.forEach { record in + WKWebsiteDataStore.default().removeData(ofTypes: record.dataTypes, for: [record], completionHandler: {}) + Logger.log("[WebCacheCleaner] Record \(record) deleted") + } + } + } +} + class ClashWebViewWindowController: NSWindowController { var onWindowClose: (() -> Void)? @@ -83,12 +96,13 @@ class ClashWebViewContoller: NSViewController { webview.navigationDelegate = self webview.customUserAgent = "ClashX Runtime" - - if NSAppKitVersion.current.rawValue > 1500 { - webview.setValue(false, forKey: "drawsBackground") - } else { - webview.setValue(true, forKey: "drawsTransparentBackground") + if #available(macOS 13.3, *) { + webview.isInspectable = true } + webview.setValue(false, forKey: "drawsBackground") + let script = WKUserScript(source: "console.log(\"dashboard loaded\")", injectionTime: .atDocumentStart, forMainFrameOnly: false) + + webview.configuration.userContentController.addUserScript(script) bridge = JsBridgeUtil.initJSbridge(webview: webview, delegate: self) registerExtenalJSBridgeFunction() @@ -119,12 +133,10 @@ class ClashWebViewContoller: NSViewController { view.window?.styleMask.insert(.closable) view.window?.styleMask.insert(.resizable) view.window?.styleMask.insert(.miniaturizable) - if #available(OSX 10.13, *) { - view.window?.toolbar = NSToolbar() - view.window?.toolbar?.showsBaselineSeparator = false - view.wantsLayer = true - view.layer?.cornerRadius = 10 - } + view.window?.toolbar = NSToolbar() + view.window?.toolbar?.showsBaselineSeparator = false + view.wantsLayer = true + view.layer?.cornerRadius = 10 view.window?.minSize = minSize if let lastSize = lastSize, lastSize != .zero { @@ -148,12 +160,20 @@ class ClashWebViewContoller: NSViewController { } func loadWebRecourses() { + WKWebsiteDataStore.default().removeData(ofTypes: [WKWebsiteDataTypeOfflineWebApplicationCache, WKWebsiteDataTypeMemoryCache], modifiedSince: Date(timeIntervalSince1970: 0), completionHandler: { }) // defaults write com.west2online.ClashX webviewUrl "your url" - let defaultUrl = "http://127.0.0.1:\(ConfigManager.shared.apiPort)/ui/" - let url = UserDefaults.standard.string(forKey: "webviewUrl") ?? defaultUrl - if let url = URL(string: url) { + if let userDefineUrl = UserDefaults.standard.string(forKey: "webviewUrl"), let url = URL(string: userDefineUrl) { + Logger.log("get user define url: \(url)") webview.load(URLRequest(url: url, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 0)) + return } + let defaultUrl = "http://127.0.0.1:\(ConfigManager.shared.apiPort)/ui/" + if let url = URL(string: defaultUrl) { + Logger.log("dashboard url:\(defaultUrl)") + webview.load(URLRequest(url: url, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 0)) + return + } + Logger.log("load dashboard url fail", level: .error) } deinit { @@ -174,20 +194,23 @@ extension ClashWebViewContoller { } extension ClashWebViewContoller: WKUIDelegate, WKNavigationDelegate { - func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {} + func webViewWebContentProcessDidTerminate(_ webView: WKWebView) { + Logger.log("[dashboard] webview crashed", level: .error) + } func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {} func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { + Logger.log("[dashboard] load request \(String(describing: navigationAction.request.url?.absoluteString))", level: .debug) decisionHandler(.allow) } func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { - Logger.log("\(String(describing: navigation))", level: .debug) + Logger.log("[dashboard] didFinish \(String(describing: navigation))", level: .info) } func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) { - Logger.log("\(error)", level: .debug) + Logger.log("[dashboard] \(String(describing: navigation)) error: \(error)", level: .error) } func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? { diff --git a/install_dependency.sh b/install_dependency.sh index a1d0fa9..e2c37f3 100755 --- a/install_dependency.sh +++ b/install_dependency.sh @@ -20,3 +20,5 @@ mv Country.mmdb.gz ./ClashX/Resources/Country.mmdb.gz echo "install dashboard" cd ClashX/Resources git clone -b gh-pages https://github.com/Dreamacro/clash-dashboard.git dashboard +cd dashboard +rm -rf *.webmanifest *.js CNAME .git