From bcad3990efd3bd37a1ec48ec3cf3e6ad9c5e2def Mon Sep 17 00:00:00 2001 From: ywmoyue Date: Sat, 28 Oct 2023 07:28:07 +0800 Subject: [PATCH 1/4] =?UTF-8?q?WebPage=E6=94=B9=E7=94=A8WebView2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BiliLite.UWP/Models/Common/Constants.cs | 5 + src/BiliLite.UWP/Pages/WebPage.xaml | 4 +- src/BiliLite.UWP/Pages/WebPage.xaml.cs | 222 ++++++++++---------- 3 files changed, 123 insertions(+), 108 deletions(-) diff --git a/src/BiliLite.UWP/Models/Common/Constants.cs b/src/BiliLite.UWP/Models/Common/Constants.cs index ce83c901..0d23d4bf 100644 --- a/src/BiliLite.UWP/Models/Common/Constants.cs +++ b/src/BiliLite.UWP/Models/Common/Constants.cs @@ -12,6 +12,11 @@ public static class Constants /// public const string BILIBILI_DOMAIN = "https://www.bilibili.com"; + /// + /// b站Host + /// + public const string BILIBILI_HOST = ".bilibili.com"; + /// /// 评论中匹配特殊文本正则表达式 /// diff --git a/src/BiliLite.UWP/Pages/WebPage.xaml b/src/BiliLite.UWP/Pages/WebPage.xaml index 2e19d498..51c1c7ab 100644 --- a/src/BiliLite.UWP/Pages/WebPage.xaml +++ b/src/BiliLite.UWP/Pages/WebPage.xaml @@ -5,6 +5,7 @@ xmlns:local="using:BiliLite.Pages" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:controls="using:Microsoft.UI.Xaml.Controls" mc:Ignorable="d" @@ -49,6 +50,7 @@ - + + diff --git a/src/BiliLite.UWP/Pages/WebPage.xaml.cs b/src/BiliLite.UWP/Pages/WebPage.xaml.cs index 069d3849..b7d00490 100644 --- a/src/BiliLite.UWP/Pages/WebPage.xaml.cs +++ b/src/BiliLite.UWP/Pages/WebPage.xaml.cs @@ -3,11 +3,14 @@ using BiliLite.Services; using Microsoft.UI.Xaml.Controls; using System; -using System.Collections.Generic; +using System.Threading.Tasks; using Windows.UI.Popups; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; +using BiliLite.Models.Common; +using Microsoft.Web.WebView2.Core; +using Microsoft.Extensions.DependencyInjection; // https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板 @@ -18,59 +21,90 @@ namespace BiliLite.Pages /// public sealed partial class WebPage : BasePage { + private readonly CookieService m_cookieService; + private static readonly ILogger _logger = GlobalLogger.FromCurrentType(); + public WebPage() { + m_cookieService = App.ServiceProvider.GetRequiredService(); this.InitializeComponent(); Title = "网页浏览"; this.Loaded += WebPage_Loaded; } + + private async void CoreWebView2_NewWindowRequested(CoreWebView2 sender, CoreWebView2NewWindowRequestedEventArgs args) + { + args.Handled = true; + var handelUrlResult = await MessageCenter.HandelUrl(sender.Source); + if (handelUrlResult) return; + var md = new MessageDialog("是否使用外部浏览器打开此链接?"); + md.Commands.Add(new UICommand("确定", new UICommandInvokedHandler(async (e) => { await Windows.System.Launcher.LaunchUriAsync(new Uri(args.Uri)); }))); + md.Commands.Add(new UICommand("取消", new UICommandInvokedHandler((e) => { }))); + await md.ShowAsync(); + } + private void WebPage_Loaded(object sender, RoutedEventArgs e) { - if (this.Parent is MyFrame) - { - (this.Parent as MyFrame).ClosedPage -= WebPage_ClosedPage; - (this.Parent as MyFrame).ClosedPage += WebPage_ClosedPage; - } + if (!(this.Parent is MyFrame frame)) return; + frame.ClosedPage -= WebPage_ClosedPage; + frame.ClosedPage += WebPage_ClosedPage; } private void WebPage_ClosedPage(object sender, EventArgs e) { - webView.NavigateToString(""); - (this.Content as Grid).Children.Remove(webView); + CloseWebView(); + } + + private void CloseWebView() + { + webView.Close(); + //(this.Content as Grid).Children.Remove(webView); webView = null; - GC.Collect(); + //GC.Collect(); } - protected override void OnNavigatedTo(NavigationEventArgs e) + protected override async void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); - if (e.NavigationMode == NavigationMode.New) + await InitWebView2(); + if (e.NavigationMode != NavigationMode.New) return; + var uri = e.Parameter.ToString(); + if (uri.Contains("h5/vlog")) { - var uri = e.Parameter.ToString(); - if (uri.Contains("h5/vlog")) - { - webView.MaxWidth = 500; - } + webView.MaxWidth = 500; + } - if (uri.Contains("read/cv")) + if (uri.Contains("read/cv")) + { + //如果是专栏,内容加载完成再显示 + webView.Visibility = Visibility.Collapsed; + } + + var url = new Uri(uri); + if (url.Host.Contains(Constants.BILIBILI_HOST)) + { + foreach (var cookie in m_cookieService.Cookies) { - //如果是专栏,内容加载完成再显示 - webView.Visibility = Visibility.Collapsed; + var webCookie = webView.CoreWebView2.CookieManager.CreateCookie(cookie.Name, cookie.Value, Constants.BILIBILI_HOST, "/"); + webView.CoreWebView2.CookieManager.AddOrUpdateCookie(webCookie); } - webView.Navigate(new Uri(uri)); - } + webView.Source=new Uri(uri); + + } + private async Task InitWebView2() + { + await webView.EnsureCoreWebView2Async(); + webView.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested; } + protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) { if (e.NavigationMode == NavigationMode.Back || e.SourcePageType == typeof(BlankPage)) { NavigationCacheMode = NavigationCacheMode.Disabled; - webView.NavigateToString(""); - (this.Content as Grid).Children.Remove(webView); - webView = null; - GC.Collect(); + CloseWebView(); } base.OnNavigatingFrom(e); } @@ -86,7 +120,7 @@ private void btnForword_Click(object sender, RoutedEventArgs e) private void btnRefresh_Click(object sender, RoutedEventArgs e) { - webView.Refresh(); + webView.Reload(); } private void btnBack_Click(object sender, RoutedEventArgs e) @@ -97,30 +131,65 @@ private void btnBack_Click(object sender, RoutedEventArgs e) } } - private async void webView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args) + private void btnShare_Click(object sender, RoutedEventArgs e) + { + webView.Source.ToString().SetClipboard(); + } + + private async void btnOpenBrowser_Click(object sender, RoutedEventArgs e) + { + await Windows.System.Launcher.LaunchUriAsync(webView.Source); + } + + private async void webView_UnsupportedUriSchemeIdentified(WebView sender, WebViewUnsupportedUriSchemeIdentifiedEventArgs args) + { + if (args.Uri.AbsoluteUri.Contains("article")) + { + args.Handled = true; + return; + } + if (args.Uri.AbsoluteUri.Contains("bilibili://")) + { + args.Handled = true; + var re = await MessageCenter.HandelUrl(args.Uri.AbsoluteUri); + if (!re) + { + Notify.ShowMessageToast("不支持打开的链接" + args.Uri.AbsoluteUri); + } + } + } + + private void btnInfo_Click(object sender, RoutedEventArgs e) + { + Notify.ShowMessageToast("虽然看起来像个浏览器,但这完全这不是个浏览器啊! ╰(‵□′)╯"); + } + + private void WebView_OnNavigationStarting(WebView2 sender, CoreWebView2NavigationStartingEventArgs args) { - if (this.Parent != null) + } + + private async void WebView_OnNavigationCompleted(WebView2 sender, CoreWebView2NavigationCompletedEventArgs args) + { + if (Parent is Frame frame) { - if ((this.Parent as Frame).Parent is TabViewItem) + if (frame.Parent is TabViewItem tabViewItem && + !string.IsNullOrEmpty(webView.CoreWebView2.DocumentTitle)) { - if (!string.IsNullOrEmpty(webView.DocumentTitle)) - { - ((this.Parent as Frame).Parent as TabViewItem).Header = webView.DocumentTitle; - } + tabViewItem.Header = webView.CoreWebView2.DocumentTitle; } - else + else if (!(frame.Parent is TabViewItem)) { - MessageCenter.ChangeTitle(this, webView.DocumentTitle); + MessageCenter.ChangeTitle(this, webView.CoreWebView2.DocumentTitle); } } + try { - //专栏阅读设置 - if (args.Uri != null && args.Uri.AbsoluteUri.Contains("read/cv")) + if (sender.Source != null && sender.Source.AbsoluteUri.Contains("read/cv")) { - await webView?.InvokeScriptAsync("eval", new List() { - @"$('#internationalHeader').hide(); + await webView?.CoreWebView2?.ExecuteScriptAsync( + @"$('#internationalHeader').hide(); $('.unlogin-popover').hide(); $('.up-info-holder').hide(); $('.nav-tab-bar').hide(); @@ -129,90 +198,29 @@ private async void webView_NavigationCompleted(WebView sender, WebViewNavigation $('.no-login').hide(); $('.author-container').show(); $('.author-container').css('margin','12px 0px -12px 0px');" - }); + ); //将专栏图片替换成jpg - await webView?.InvokeScriptAsync("eval", new List() { + await webView?.CoreWebView2?.ExecuteScriptAsync( @"document.getElementsByClassName('img-box').forEach(element => { element.getElementsByTagName('img').forEach(image => { image.src=image.getAttribute('data-src')+'@progressive.jpg'; }); });" - }); + ); } - - - await webView?.InvokeScriptAsync("eval", new List() { + await webView?.CoreWebView2?.ExecuteScriptAsync( "$('.h5-download-bar').hide()" - }); - - - + ); } - catch (Exception) + catch (Exception ex) { - + _logger.Warn(ex.Message, ex); } finally { if (webView != null) webView.Visibility = Visibility.Visible; } } - - private void btnShare_Click(object sender, RoutedEventArgs e) - { - webView.Source.ToString().SetClipboard(); - } - - private async void webView_NewWindowRequested(WebView sender, WebViewNewWindowRequestedEventArgs args) - { - args.Handled = true; - var re = await MessageCenter.HandelUrl(args.Uri.AbsoluteUri); - if (!re) - { - var md = new MessageDialog("是否使用外部浏览器打开此链接?"); - md.Commands.Add(new UICommand("确定", new UICommandInvokedHandler(async (e) => { await Windows.System.Launcher.LaunchUriAsync(args.Uri); }))); - md.Commands.Add(new UICommand("取消", new UICommandInvokedHandler((e) => { }))); - await md.ShowAsync(); - } - } - - private async void btnOpenBrowser_Click(object sender, RoutedEventArgs e) - { - await Windows.System.Launcher.LaunchUriAsync(webView.Source); - } - - private void webView_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args) - { - if (args.Uri != null && args.Uri.AbsoluteUri.Contains("read/cv")) - { - // args.Cancel = true; - // return; - } - } - - private async void webView_UnsupportedUriSchemeIdentified(WebView sender, WebViewUnsupportedUriSchemeIdentifiedEventArgs args) - { - if (args.Uri.AbsoluteUri.Contains("article")) - { - args.Handled = true; - return; - } - if (args.Uri.AbsoluteUri.Contains("bilibili://")) - { - args.Handled = true; - var re = await MessageCenter.HandelUrl(args.Uri.AbsoluteUri); - if (!re) - { - Notify.ShowMessageToast("不支持打开的链接" + args.Uri.AbsoluteUri); - } - } - - } - - private void btnInfo_Click(object sender, RoutedEventArgs e) - { - Notify.ShowMessageToast("虽然看起来像个浏览器,但这完全这不是个浏览器啊! ╰(‵□′)╯"); - } } } From ef8fcf4ecf112d17dd3324ebcd15b229c35c89df Mon Sep 17 00:00:00 2001 From: GD-Slime <82302542+GD-Slime@users.noreply.github.com> Date: Sun, 29 Oct 2023 10:22:47 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=8C=E7=BB=B4?= =?UTF-8?q?=E7=A0=81=E7=99=BB=E5=BD=95=20(#367)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修复flv视频的无法播放的问题 * 修复一个类型转换的小问题 * 解决直播流模糊问题 * 修复二维码登录 * Revert "修复一个类型转换的小问题" This reverts commit 30f52c8bd7c7dc570ccb29991cb5173f13fa740c. * Revert "修复flv视频的无法播放的问题" This reverts commit 70deeba9a8ac184bd208ffa0f0961eb26bc93107. --- src/BiliLite.UWP/Models/Requests/Api/AccountApi.cs | 12 ++++++------ src/BiliLite.UWP/Modules/User/Account.cs | 12 ++++++------ src/BiliLite.UWP/Modules/User/LoginVM.cs | 6 +++--- src/BiliLite.UWP/Services/ApiHelper.cs | 8 +++++++- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/BiliLite.UWP/Models/Requests/Api/AccountApi.cs b/src/BiliLite.UWP/Models/Requests/Api/AccountApi.cs index 4a7ae153..38dbd5b8 100644 --- a/src/BiliLite.UWP/Models/Requests/Api/AccountApi.cs +++ b/src/BiliLite.UWP/Models/Requests/Api/AccountApi.cs @@ -324,15 +324,15 @@ public ApiModel DelHistory(string id) /// /// /// - public ApiModel QRLoginAuthCodeTV(string local_id) + public ApiModel QRLoginAuthCodeTV(string local_id, ApiKeyInfo appkey) { ApiModel api = new ApiModel() { method = RestSharp.Method.Post, baseUrl = "https://passport.bilibili.com/x/passport-tv-login/qrcode/auth_code", - body = ApiHelper.MustParameter(ApiHelper.AndroidTVKey, false) + $"&local_id={local_id}", + body = ApiHelper.MustParameter(appkey, false) + $"&local_id={local_id}", }; - api.body += ApiHelper.GetSign(api.body, ApiHelper.AndroidTVKey); + api.body += ApiHelper.GetSign(api.body, appkey); return api; } @@ -341,15 +341,15 @@ public ApiModel QRLoginAuthCodeTV(string local_id) /// /// /// - public ApiModel QRLoginPollTV(string auth_code, string local_id) + public ApiModel QRLoginPollTV(string auth_code, string local_id, ApiKeyInfo appkey) { ApiModel api = new ApiModel() { method = RestSharp.Method.Post, baseUrl = "https://passport.bilibili.com/x/passport-tv-login/qrcode/poll", - body = ApiHelper.MustParameter(ApiHelper.AndroidTVKey, false) + $"&auth_code={auth_code}&guid={Guid.NewGuid().ToString()}&local_id={local_id}", + body = ApiHelper.MustParameter(appkey, false) + $"&auth_code={auth_code}&guid={Guid.NewGuid().ToString()}&local_id={local_id}", }; - api.body += ApiHelper.GetSign(api.body, ApiHelper.AndroidTVKey); + api.body += ApiHelper.GetSign(api.body, appkey); return api; } diff --git a/src/BiliLite.UWP/Modules/User/Account.cs b/src/BiliLite.UWP/Modules/User/Account.cs index 3bd5f07a..9a82bbe2 100644 --- a/src/BiliLite.UWP/Modules/User/Account.cs +++ b/src/BiliLite.UWP/Modules/User/Account.cs @@ -329,14 +329,14 @@ public async Task GetAccessKey(string url) } /// - /// 获取二维码登录信息 + /// 获取tv版二维码登录信息 /// /// - public async Task> GetQRAuthInfoTV() + public async Task> GetQRAuthInfoTV(ApiKeyInfo appkey) { try { - var result = await accountApi.QRLoginAuthCodeTV(guid).Request(); + var result = await accountApi.QRLoginAuthCodeTV(guid, appkey).Request(); if (result.status) { var data = await result.GetData(); @@ -377,14 +377,14 @@ public async Task> GetQRAuthInfoTV() } } /// - /// 轮询二维码扫描信息 + /// 轮询tv版二维码扫描信息 /// /// - public async Task PollQRAuthInfoTV(string auth_code) + public async Task PollQRAuthInfoTV(string auth_code, ApiKeyInfo appkey) { try { - var result = await accountApi.QRLoginPollTV(auth_code, guid).Request(); + var result = await accountApi.QRLoginPollTV(auth_code, guid, appkey).Request(); if (result.status) { var data = await result.GetData(); diff --git a/src/BiliLite.UWP/Modules/User/LoginVM.cs b/src/BiliLite.UWP/Modules/User/LoginVM.cs index ee0a5dbc..9e590f22 100644 --- a/src/BiliLite.UWP/Modules/User/LoginVM.cs +++ b/src/BiliLite.UWP/Modules/User/LoginVM.cs @@ -576,7 +576,7 @@ public Windows.UI.Xaml.Media.ImageSource QRImageSource } - QRAuthInfoWeb qrAuthInfo; + QRAuthInfo qrAuthInfo; private async void GetQRAuthInfo() { try @@ -587,7 +587,7 @@ private async void GetQRAuthInfo() qrTimer.Stop(); qrTimer.Dispose(); } - var result = await account.GetQRAuthInfo(); + var result = await account.GetQRAuthInfoTV(ApiHelper.iPadOSKey); if (result.success) { qrAuthInfo = result.data; @@ -636,7 +636,7 @@ private async void QrTimer_Elapsed(object sender, ElapsedEventArgs e) { await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => { - var result = await account.PollQRAuthInfo(qrAuthInfo.qrcode_key); + var result = await account.PollQRAuthInfoTV(qrAuthInfo.auth_code, ApiHelper.iPadOSKey); if (result.status == LoginStatus.Success) { qrTimer.Stop(); diff --git a/src/BiliLite.UWP/Services/ApiHelper.cs b/src/BiliLite.UWP/Services/ApiHelper.cs index f143145c..a20c23fd 100644 --- a/src/BiliLite.UWP/Services/ApiHelper.cs +++ b/src/BiliLite.UWP/Services/ApiHelper.cs @@ -30,11 +30,17 @@ public static class ApiHelper //漫游默认的服务器 public const string ROMAING_PROXY_URL = "https://b.chuchai.vip"; - public static ApiKeyInfo AndroidKey = new ApiKeyInfo("1d8b6e7d45233436", "560c52ccd288fed045859ed18bffd973"); + // 抓取ipad端appkey + public static ApiKeyInfo iPadOSKey = new ApiKeyInfo("27eb53fc9058f8c3", "c2ed53a74eeefe3cf99fbd01d8c9c375"); + public static ApiKeyInfo AndroidVideoKey = new ApiKeyInfo("iVGUTjsxvpLeuDCf", "aHRmhWMLkdeMuILqORnYZocwMBpMEOdt"); public static ApiKeyInfo WebVideoKey = new ApiKeyInfo("84956560bc028eb7", "94aba54af9065f71de72f5508f1cd42e"); public static ApiKeyInfo AndroidTVKey = new ApiKeyInfo("4409e2ce8ffd12b8", "59b43e04ad6965f34319062b478f83dd"); public static ApiKeyInfo LoginKey = new ApiKeyInfo("783bbb7264451d82", "2653583c8873dea268ab9386918b1d65"); + + //public static ApiKeyInfo AndroidKey = new ApiKeyInfo("1d8b6e7d45233436", "560c52ccd288fed045859ed18bffd973"); + public static ApiKeyInfo AndroidKey = iPadOSKey; + private const string build = "6235200"; private const string _mobi_app = "android"; private const string _platform = "android"; From e592833a80948dca3cb9fff9713234122c8ba131 Mon Sep 17 00:00:00 2001 From: ywmoyue Date: Sun, 29 Oct 2023 15:29:44 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E8=B0=83=E6=95=B4=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E5=8F=8A=E4=BD=BF=E7=94=A8AppKey=E7=9A=84=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BiliLite.UWP/BiliLite.UWP.csproj | 13 +- src/BiliLite.UWP/Dialogs/LoginDialog.xaml.cs | 3 +- .../Models/Common/SettingConstants.cs | 11 ++ .../Models/Requests/Api/AccountApi.cs | 147 +++++++++--------- .../Models/Requests/Api/BaseApi.cs | 10 ++ .../Models/Requests/Api/CommentApi.cs | 28 ++-- .../Models/Requests/Api/EmoteApi.cs | 9 +- .../Models/Requests/Api/GitApi.cs | 4 +- .../Models/Requests/Api/Home/AnimeAPI.cs | 14 +- .../Models/Requests/Api/Home/CinemaAPI.cs | 4 +- .../Models/Requests/Api/Home/HotApi.cs | 8 +- .../Models/Requests/Api/Home/LiveAPI.cs | 13 +- .../Models/Requests/Api/Home/RecommendAPI.cs | 21 ++- .../Models/Requests/Api/Live/LiveAreaAPI.cs | 15 +- .../Models/Requests/Api/Live/LiveCenterAPI.cs | 37 +++-- .../Requests/Api/Live/LiveRecommendAPI.cs | 8 +- .../Models/Requests/Api/Live/LiveRoomAPI.cs | 115 +++++++------- .../Models/Requests/Api/PlayerAPI.cs | 56 +++---- .../Models/Requests/Api/RankAPI.cs | 9 +- .../Models/Requests/Api/RegionAPI.cs | 38 ++--- .../Models/Requests/Api/SearchAPI.cs | 22 +-- .../Models/Requests/Api/SeasonApi.cs | 34 ++-- .../Models/Requests/Api/SeasonIndexAPI.cs | 14 +- .../Models/Requests/Api/User/AtAPI.cs | 14 +- .../Models/Requests/Api/User/DynamicAPI.cs | 65 ++++---- .../Models/Requests/Api/User/FavoriteAPI.cs | 89 +++++------ .../Models/Requests/Api/User/FollowAPI.cs | 40 ++--- .../Models/Requests/Api/User/UserDetailAPI.cs | 32 ++-- .../Models/Requests/Api/User/WatchLaterAPI.cs | 32 ++-- .../Models/Requests/Api/VideoAPI.cs | 48 +++--- src/BiliLite.UWP/Modules/User/Account.cs | 48 +++--- src/BiliLite.UWP/Modules/User/LoginVM.cs | 37 +++-- src/BiliLite.UWP/Services/ApiHelper.cs | 7 +- src/BiliLite.UWP/Services/SettingService.cs | 47 +++--- 34 files changed, 557 insertions(+), 535 deletions(-) create mode 100644 src/BiliLite.UWP/Models/Requests/Api/BaseApi.cs diff --git a/src/BiliLite.UWP/BiliLite.UWP.csproj b/src/BiliLite.UWP/BiliLite.UWP.csproj index 888f047f..60997a7d 100644 --- a/src/BiliLite.UWP/BiliLite.UWP.csproj +++ b/src/BiliLite.UWP/BiliLite.UWP.csproj @@ -1,8 +1,6 @@  - - + + Debug x86 @@ -178,6 +176,7 @@ + @@ -1252,12 +1251,10 @@ Windows Desktop Extensions for the UWP - + 14.0 - +