This commit is contained in:
DismissedLight
2023-10-11 22:12:07 +08:00
parent e7233fbf2a
commit 61dd098d95
4 changed files with 32 additions and 17 deletions

View File

@@ -14,9 +14,7 @@ namespace Snap.Hutao.Core.ExceptionService;
internal sealed partial class ExceptionRecorder
{
private readonly ILogger<ExceptionRecorder> logger;
#if RELEASE
private readonly IServiceProvider serviceProvider;
#endif
/// <summary>
/// 记录应用程序异常
@@ -31,13 +29,11 @@ internal sealed partial class ExceptionRecorder
private void OnAppUnhandledException(object? sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
{
#if RELEASE
serviceProvider
.GetRequiredService<Web.Hutao.Log.HomaLogUploadClient>()
.UploadLogAsync(e.Exception)
.GetAwaiter()
.GetResult();
#endif
logger.LogError("未经处理的全局异常:\r\n{Detail}", ExceptionFormat.Format(e.Exception));
}

View File

@@ -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<UserChangedMessage>
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<UserChangedMessage>
private void OnUnloaded(object sender, RoutedEventArgs e)
{
jsInterface = null;
Loaded -= loadEventHandler;
Unloaded -= unloadEventHandler;
}
@@ -86,13 +86,16 @@ internal partial class WebViewer : UserControl, IRecipient<UserChangedMessage>
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);
}
}

View File

@@ -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;

View File

@@ -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<MiHoYoJSInterface> 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<string> 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<JsParam>(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);
}
}