|
| 1 | +// Copyright (c) Bili Copilot. All rights reserved. |
| 2 | + |
| 3 | +using Bili.Copilot.App.Controls.Base; |
| 4 | +using Bili.Copilot.Models.App.Args; |
| 5 | +using Microsoft.Web.WebView2.Core; |
| 6 | +using Windows.Storage; |
| 7 | + |
| 8 | +namespace Bili.Copilot.App.Pages; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// 网页播放器界面. |
| 12 | +/// </summary> |
| 13 | +public sealed partial class WebPlayerPage : PageBase |
| 14 | +{ |
| 15 | + private const string UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2048.1"; |
| 16 | + private string _url; |
| 17 | + private Window _attachedWindow; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Initializes a new instance of the <see cref="WebPlayerPage"/> class. |
| 21 | + /// </summary> |
| 22 | + public WebPlayerPage() |
| 23 | + { |
| 24 | + InitializeComponent(); |
| 25 | + } |
| 26 | + |
| 27 | + /// <inheritdoc/> |
| 28 | + protected override void OnNavigatedTo(NavigationEventArgs e) |
| 29 | + { |
| 30 | + if (e.Parameter is PlayerPageNavigateEventArgs args) |
| 31 | + { |
| 32 | + if (args.Snapshot != null) |
| 33 | + { |
| 34 | + _url = args.Snapshot.WebLink; |
| 35 | + _attachedWindow = args.AttachedWindow as Window; |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /// <inheritdoc/> |
| 41 | + protected override void OnNavigatedFrom(NavigationEventArgs e) |
| 42 | + { |
| 43 | + try |
| 44 | + { |
| 45 | + MainView.NavigateToString(string.Empty); |
| 46 | + } |
| 47 | + catch (Exception) |
| 48 | + { |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /// <inheritdoc/> |
| 53 | + protected override async void OnPageLoaded() |
| 54 | + { |
| 55 | + LoadingOverlay.IsOpen = true; |
| 56 | + |
| 57 | + // 取消自动静音限制. |
| 58 | + Environment.SetEnvironmentVariable("WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS", "--autoplay-policy=no-user-gesture-required --enable-features=PlatformHEVCEncoderSupport --enable-features=HardwareMediaDecoding --enable-features=PlatformEncryptedDolbyVision"); |
| 59 | + await MainView.EnsureCoreWebView2Async(); |
| 60 | + MainView.CoreWebView2.Settings.IsStatusBarEnabled = false; |
| 61 | + MainView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = true; |
| 62 | + MainView.CoreWebView2.Settings.AreDevToolsEnabled = true; |
| 63 | + MainView.CoreWebView2.Settings.UserAgent = UserAgent; |
| 64 | + MainView.CoreWebView2.ContainsFullScreenElementChanged += OnContainsFullScreenElementChanged; |
| 65 | + MainView.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All); |
| 66 | + MainView.CoreWebView2.WebResourceRequested += OnWebResourceRequested; |
| 67 | + MainView.CoreWebView2.NavigationStarting += OnNavigationStarting; |
| 68 | + if (!string.IsNullOrEmpty(_url)) |
| 69 | + { |
| 70 | + MainView.CoreWebView2.Navigate(_url); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private void OnNavigationStarting(CoreWebView2 sender, CoreWebView2NavigationStartingEventArgs args) |
| 75 | + => args.RequestHeaders.SetHeader("User-Agent", UserAgent); |
| 76 | + |
| 77 | + private void OnWebResourceRequested(CoreWebView2 sender, CoreWebView2WebResourceRequestedEventArgs args) |
| 78 | + => args.Request.Headers.SetHeader("User-Agent", UserAgent); |
| 79 | + |
| 80 | + private void OnContainsFullScreenElementChanged(CoreWebView2 sender, object args) |
| 81 | + { |
| 82 | + if (sender.ContainsFullScreenElement) |
| 83 | + { |
| 84 | + _attachedWindow.AppWindow.SetPresenter(Microsoft.UI.Windowing.AppWindowPresenterKind.FullScreen); |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + _attachedWindow.AppWindow.SetPresenter(Microsoft.UI.Windowing.AppWindowPresenterKind.Overlapped); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private async void OnNavigationCompletedAsync(WebView2 sender, CoreWebView2NavigationCompletedEventArgs args) |
| 93 | + { |
| 94 | + LoadingOverlay.IsOpen = false; |
| 95 | + |
| 96 | + // 注入 js. |
| 97 | + var cleanFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/WebPlayer.js")); |
| 98 | + var cleanContent = await FileIO.ReadTextAsync(cleanFile); |
| 99 | + await MainView.CoreWebView2.ExecuteScriptAsync(cleanContent); |
| 100 | + } |
| 101 | +} |
0 commit comments