diff --git a/src/Snap.Hutao/Snap.Hutao/Core/ExceptionService/ExceptionRecorder.cs b/src/Snap.Hutao/Snap.Hutao/Core/ExceptionService/ExceptionRecorder.cs index 979b767b..358f83b8 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/ExceptionService/ExceptionRecorder.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/ExceptionService/ExceptionRecorder.cs @@ -14,9 +14,7 @@ namespace Snap.Hutao.Core.ExceptionService; internal sealed partial class ExceptionRecorder { private readonly ILogger logger; -#if RELEASE private readonly IServiceProvider serviceProvider; -#endif /// /// 记录应用程序异常 @@ -31,13 +29,11 @@ internal sealed partial class ExceptionRecorder private void OnAppUnhandledException(object? sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e) { -#if RELEASE serviceProvider .GetRequiredService() .UploadLogAsync(e.Exception) .GetAwaiter() .GetResult(); -#endif logger.LogError("未经处理的全局异常:\r\n{Detail}", ExceptionFormat.Format(e.Exception)); } diff --git a/src/Snap.Hutao/Snap.Hutao/View/Control/WebViewer.xaml.cs b/src/Snap.Hutao/Snap.Hutao/View/Control/WebViewer.xaml.cs index 480e2a44..e9326fa2 100644 --- a/src/Snap.Hutao/Snap.Hutao/View/Control/WebViewer.xaml.cs +++ b/src/Snap.Hutao/Snap.Hutao/View/Control/WebViewer.xaml.cs @@ -2,8 +2,10 @@ // Licensed under the MIT license. using CommunityToolkit.Mvvm.Messaging; +using Microsoft.UI.Content; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Hosting; using Microsoft.Web.WebView2.Core; using Snap.Hutao.Message; using Snap.Hutao.Service.Notification; @@ -20,7 +22,6 @@ internal partial class WebViewer : UserControl, IRecipient private readonly RoutedEventHandler loadEventHandler; private readonly RoutedEventHandler unloadEventHandler; - [SuppressMessage("", "IDE0052")] private MiHoYoJSInterface? jsInterface; public WebViewer() @@ -49,7 +50,6 @@ internal partial class WebViewer : UserControl, IRecipient private void OnUnloaded(object sender, RoutedEventArgs e) { - jsInterface = null; Loaded -= loadEventHandler; Unloaded -= unloadEventHandler; } @@ -86,13 +86,16 @@ internal partial class WebViewer : UserControl, IRecipient string source = SourceProvider.GetSource(userAndUid); if (!string.IsNullOrEmpty(source)) { - await coreWebView2.DeleteCookiesAsync(".mihoyo.com").ConfigureAwait(true); - coreWebView2.SetCookie(user.CookieToken, user.LToken, user.SToken); - _ = userAndUid.User.IsOversea ? coreWebView2.SetMobileOverseaUserAgent() : coreWebView2.SetMobileUserAgent(); - jsInterface = SourceProvider.CreateJsInterface(serviceProvider, coreWebView2, userAndUid); + await coreWebView2.Profile.ClearBrowsingDataAsync(); CoreWebView2Navigator navigator = new(coreWebView2); await navigator.NavigateAsync("about:blank").ConfigureAwait(true); + + coreWebView2.SetCookie(user.CookieToken, user.LToken, user.SToken); + _ = userAndUid.User.IsOversea ? coreWebView2.SetMobileOverseaUserAgent() : coreWebView2.SetMobileUserAgent(); + jsInterface?.Detach(); + jsInterface = SourceProvider.CreateJsInterface(serviceProvider, coreWebView2, userAndUid); + await navigator.NavigateAsync(source).ConfigureAwait(true); } } diff --git a/src/Snap.Hutao/Snap.Hutao/Web/Bridge/CoreWebView2Extension.cs b/src/Snap.Hutao/Snap.Hutao/Web/Bridge/CoreWebView2Extension.cs index afde86ed..fd763312 100644 --- a/src/Snap.Hutao/Snap.Hutao/Web/Bridge/CoreWebView2Extension.cs +++ b/src/Snap.Hutao/Snap.Hutao/Web/Bridge/CoreWebView2Extension.cs @@ -76,17 +76,24 @@ internal static class CoreWebView2Extension if (cookieToken is not null) { - cookieManager.AddMihoyoCookie(Cookie.ACCOUNT_ID, cookieToken, isOversea).AddMihoyoCookie(Cookie.COOKIE_TOKEN, cookieToken, isOversea); + cookieManager + .AddMihoyoCookie(Cookie.ACCOUNT_ID, cookieToken, isOversea) + .AddMihoyoCookie(Cookie.COOKIE_TOKEN, cookieToken, isOversea); } if (lToken is not null) { - cookieManager.AddMihoyoCookie(Cookie.LTUID, lToken, isOversea).AddMihoyoCookie(Cookie.LTOKEN, lToken, isOversea); + cookieManager + .AddMihoyoCookie(Cookie.LTUID, lToken, isOversea) + .AddMihoyoCookie(Cookie.LTOKEN, lToken, isOversea); } if (sToken is not null) { - cookieManager.AddMihoyoCookie(Cookie.STUID, sToken, isOversea).AddMihoyoCookie(Cookie.STOKEN, sToken, isOversea); + cookieManager + .AddMihoyoCookie(Cookie.MID, sToken, isOversea) + .AddMihoyoCookie(Cookie.STUID, sToken, isOversea) + .AddMihoyoCookie(Cookie.STOKEN, sToken, isOversea); } return webView; diff --git a/src/Snap.Hutao/Snap.Hutao/Web/Bridge/MiHoYoJSInterface.cs b/src/Snap.Hutao/Snap.Hutao/Web/Bridge/MiHoYoJSInterface.cs index 22dbc19d..fb4911ec 100644 --- a/src/Snap.Hutao/Snap.Hutao/Web/Bridge/MiHoYoJSInterface.cs +++ b/src/Snap.Hutao/Snap.Hutao/Web/Bridge/MiHoYoJSInterface.cs @@ -39,9 +39,10 @@ internal class MiHoYoJSInterface document.querySelector('body').appendChild(st); """; + private readonly Guid interfaceId = Guid.NewGuid(); private readonly IServiceProvider serviceProvider; - private readonly CoreWebView2 webView; private readonly UserAndUid userAndUid; + private CoreWebView2 webView; private readonly ITaskContext taskContext; private readonly ILogger logger; @@ -342,6 +343,14 @@ internal class MiHoYoJSInterface throw new NotImplementedException(); } + public void Detach() + { + webView.WebMessageReceived -= webMessageReceivedEventHandler; + webView.DOMContentLoaded -= domContentLoadedEventHandler; + webView.NavigationStarting -= navigationStartingEventHandler; + webView = default!; + } + private async ValueTask ExecuteCallbackScriptAsync(string callback, string? payload = null) { if (string.IsNullOrEmpty(callback)) @@ -360,7 +369,7 @@ internal class MiHoYoJSInterface .Append(')') .ToString(); - logger?.LogInformation("[ExecuteScript: {callback}]\n{payload}", callback, payload); + logger?.LogInformation("[{Id}][ExecuteScript: {callback}]\n{payload}", interfaceId, callback, payload); await taskContext.SwitchToMainThreadAsync(); try @@ -378,7 +387,7 @@ internal class MiHoYoJSInterface private async void OnWebMessageReceived(CoreWebView2 webView2, CoreWebView2WebMessageReceivedEventArgs args) { string message = args.TryGetWebMessageAsString(); - logger.LogInformation("[OnRawMessage]\n{message}", message); + logger.LogInformation("[{Id}][OnRawMessage]\n{message}", interfaceId, message); JsParam? param = JsonSerializer.Deserialize(message); ArgumentNullException.ThrowIfNull(param); @@ -445,7 +454,7 @@ internal class MiHoYoJSInterface if (uriHostSpan.EndsWith("mihoyo.com") || uriHostSpan.EndsWith("hoyolab.com")) { // Execute this solve issue: When open same site second time,there might be no bridge init. - coreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(InitializeJsInterfaceScript2).AsTask().SafeForget(logger); + // coreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(InitializeJsInterfaceScript2).AsTask().SafeForget(logger); coreWebView2.ExecuteScriptAsync(InitializeJsInterfaceScript2).AsTask().SafeForget(logger); } }