|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Windows; |
| 4 | +using System.Windows.Input; |
| 5 | +using Microsoft.Web.WebView2.Core; |
| 6 | +using Microsoft.Web.WebView2.Wpf; |
| 7 | +using Microsoft.Web.WebView2.Core.DevToolsProtocolExtension; |
| 8 | +using System.Text; |
| 9 | +using System.Linq; |
| 10 | + |
| 11 | +namespace WV2CDPExtensionSample |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Interaction logic for MainWindow.xaml |
| 15 | + /// </summary> |
| 16 | + public partial class MainWindow : Window |
| 17 | + { |
| 18 | + bool _isNavigating = false; |
| 19 | + public static RoutedCommand CallCDPMethodCommand = new RoutedCommand(); |
| 20 | + DevToolsProtocolHelper _cdpHelper; |
| 21 | + DevToolsProtocolHelper cdpHelper |
| 22 | + { |
| 23 | + get |
| 24 | + { |
| 25 | + if (webView == null || webView.CoreWebView2 == null) |
| 26 | + { |
| 27 | + throw new Exception("Initialize WebView before using DevToolsProtocolHelper."); |
| 28 | + } |
| 29 | + |
| 30 | + if (_cdpHelper == null) |
| 31 | + { |
| 32 | + _cdpHelper = webView.CoreWebView2.GetDevToolsProtocolHelper(); |
| 33 | + } |
| 34 | + |
| 35 | + return _cdpHelper; |
| 36 | + } |
| 37 | + } |
| 38 | + public MainWindow() |
| 39 | + { |
| 40 | + InitializeComponent(); |
| 41 | + } |
| 42 | + |
| 43 | + void GoToPageCmdCanExecute(object sender, CanExecuteRoutedEventArgs e) |
| 44 | + { |
| 45 | + e.CanExecute = webView != null && !_isNavigating; |
| 46 | + } |
| 47 | + |
| 48 | + async void GoToPageCmdExecuted(object target, ExecutedRoutedEventArgs e) |
| 49 | + { |
| 50 | + await webView.EnsureCoreWebView2Async(); |
| 51 | + webView.CoreWebView2.Navigate((string)e.Parameter); |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + #region CDP_COMMANDS |
| 56 | + async void ShowFPSCounter(object sender, RoutedEventArgs e) |
| 57 | + { |
| 58 | + await cdpHelper.Overlay.SetShowFPSCounterAsync(true); |
| 59 | + } |
| 60 | + |
| 61 | + async void HideFPSCounter(object sender, RoutedEventArgs e) |
| 62 | + { |
| 63 | + await cdpHelper.Overlay.SetShowFPSCounterAsync(false); |
| 64 | + } |
| 65 | + |
| 66 | + async void SetPageScaleTo4(object sender, RoutedEventArgs e) |
| 67 | + { |
| 68 | + await cdpHelper.Emulation.SetPageScaleFactorAsync(4); |
| 69 | + } |
| 70 | + |
| 71 | + async void ResetPageScale(object sender, RoutedEventArgs e) |
| 72 | + { |
| 73 | + await cdpHelper.Emulation.SetPageScaleFactorAsync(1); |
| 74 | + } |
| 75 | + |
| 76 | + async void ReloadPage(object sender, RoutedEventArgs e) |
| 77 | + { |
| 78 | + await cdpHelper.Page.ReloadAsync(); |
| 79 | + } |
| 80 | + |
| 81 | + async void CaptureSnapshot(object sender, RoutedEventArgs e) |
| 82 | + { |
| 83 | + Trace.WriteLine(await cdpHelper.Page.CaptureSnapshotAsync()); |
| 84 | + } |
| 85 | + |
| 86 | + async void GetAllCookies(object sender, RoutedEventArgs e) |
| 87 | + { |
| 88 | + Network.Cookie[] cookies = await cdpHelper.Network.GetAllCookiesAsync(); |
| 89 | + StringBuilder cookieResult = new StringBuilder(cookies.Count() + " cookie(s) received\n"); |
| 90 | + foreach (var cookie in cookies) |
| 91 | + { |
| 92 | + cookieResult.Append($"\n{cookie.Name} {cookie.Value} {(cookie.Session ? "[session cookie]" : cookie.Expires.ToString("G"))}"); |
| 93 | + } |
| 94 | + MessageBox.Show(cookieResult.ToString(), "Cookies"); |
| 95 | + } |
| 96 | + |
| 97 | + async void AddOrUpdateCookie(object target, RoutedEventArgs e) |
| 98 | + { |
| 99 | + bool cookie = await cdpHelper.Network.SetCookieAsync("CookieName", "CookieValue", null, "https://www.bing.com/"); |
| 100 | + MessageBox.Show(cookie ? "Cookie is added/updated successfully" : "Error adding/updating cookie", "Cookies"); |
| 101 | + } |
| 102 | + |
| 103 | + async void ClearAllCookies(object sender, RoutedEventArgs e) |
| 104 | + { |
| 105 | + await cdpHelper.Network.ClearBrowserCookiesAsync(); |
| 106 | + MessageBox.Show("Browser cookies are deleted", "Cookies"); |
| 107 | + } |
| 108 | + |
| 109 | + async void SetGeolocation(object sender, RoutedEventArgs e) |
| 110 | + { |
| 111 | + double latitude = 36.553085; |
| 112 | + double longitude = 103.97543; |
| 113 | + double accuracy = 1; |
| 114 | + await cdpHelper.Emulation.SetGeolocationOverrideAsync(latitude, longitude, accuracy); |
| 115 | + MessageBox.Show("Overridden the Geolocation Position", "Geolocation"); |
| 116 | + } |
| 117 | + |
| 118 | + async void ClearGeolocation(object sender, RoutedEventArgs e) |
| 119 | + { |
| 120 | + await cdpHelper.Emulation.ClearGeolocationOverrideAsync(); |
| 121 | + MessageBox.Show("Cleared overridden Geolocation Position", "Geolocation"); |
| 122 | + } |
| 123 | + #endregion |
| 124 | + |
| 125 | + #region CDP_EVENTS |
| 126 | + async void SubscribeToDataReceived(object sender, RoutedEventArgs e) |
| 127 | + { |
| 128 | + await cdpHelper.Network.EnableAsync(); |
| 129 | + cdpHelper.Network.DataReceived += PrintDataReceived; |
| 130 | + MessageBox.Show("Subscribed to DataReceived Event!", "DataReceived"); |
| 131 | + } |
| 132 | + |
| 133 | + void UnsubscribeFromDataReceived(object sender, RoutedEventArgs e) |
| 134 | + { |
| 135 | + cdpHelper.Network.DataReceived -= PrintDataReceived; |
| 136 | + MessageBox.Show("Unsubscribed from DataReceived Event!", "DataReceived"); |
| 137 | + } |
| 138 | + |
| 139 | + void PrintDataReceived(object sender, Network.DataReceivedEventArgs args) |
| 140 | + { |
| 141 | + Trace.WriteLine(String.Format("DataReceived Event Args - Timestamp: {0} Request Id: {1} DataLength: {2}", args.Timestamp, args.RequestId, args.DataLength)); |
| 142 | + } |
| 143 | + |
| 144 | + async void SubscribeToAnimationCreated(object sender, RoutedEventArgs e) |
| 145 | + { |
| 146 | + await cdpHelper.Animation.EnableAsync(); |
| 147 | + cdpHelper.Animation.AnimationCreated += PrintAnimationCreated; |
| 148 | + MessageBox.Show("Subscribed to AnimationCreated Event!", "AnimationCreated"); |
| 149 | + } |
| 150 | + |
| 151 | + void UnsubscribeFromAnimationCreated(object sender, RoutedEventArgs e) |
| 152 | + { |
| 153 | + cdpHelper.Animation.AnimationCreated -= PrintAnimationCreated; |
| 154 | + MessageBox.Show("Unsubscribed from AnimationCreated Event!", "AnimationCreated"); |
| 155 | + } |
| 156 | + |
| 157 | + void PrintAnimationCreated(object sender, Animation.AnimationCreatedEventArgs args) |
| 158 | + { |
| 159 | + Trace.WriteLine(String.Format("AnimationCreated Event Args - Id: {0}", args.Id)); |
| 160 | + } |
| 161 | + |
| 162 | + async void SubscribeToDocumentUpdated(object sender, RoutedEventArgs e) |
| 163 | + { |
| 164 | + await cdpHelper.DOM.EnableAsync(); |
| 165 | + cdpHelper.DOM.DocumentUpdated += PrintDocumentUpdated; |
| 166 | + MessageBox.Show("Subscribed to DocumentUpdated Event!", "DocumentUpdated"); |
| 167 | + } |
| 168 | + |
| 169 | + void UnsubscribeFromDocumentUpdated(object sender, RoutedEventArgs e) |
| 170 | + { |
| 171 | + cdpHelper.DOM.DocumentUpdated -= PrintDocumentUpdated; |
| 172 | + MessageBox.Show("Unsubscribed from DocumentUpdated Event!", "DocumentUpdated"); |
| 173 | + } |
| 174 | + |
| 175 | + void PrintDocumentUpdated(object sender, DOM.DocumentUpdatedEventArgs args) |
| 176 | + { |
| 177 | + Trace.WriteLine("DocumentUpdated Event Args - No Parameters", "DocumentUpdated"); |
| 178 | + } |
| 179 | + |
| 180 | + async void SubscribeToDownloadWillBegin(object sender, RoutedEventArgs e) |
| 181 | + { |
| 182 | + await cdpHelper.Page.EnableAsync(); |
| 183 | + cdpHelper.Page.DownloadWillBegin += PrintDownloadWillBegin; |
| 184 | + MessageBox.Show("Subscribed to DownloadWillBegin Event!", "DownloadWillBegin"); |
| 185 | + } |
| 186 | + |
| 187 | + void UnsubscribeFromDownloadWillBegin(object sender, RoutedEventArgs e) |
| 188 | + { |
| 189 | + cdpHelper.Page.DownloadWillBegin -= PrintDownloadWillBegin; |
| 190 | + MessageBox.Show("Unsubscribed from DownloadWillBegin Event!", "DownloadWillBegin"); |
| 191 | + } |
| 192 | + |
| 193 | + void PrintDownloadWillBegin(object sender, Page.DownloadWillBeginEventArgs args) |
| 194 | + { |
| 195 | + Trace.WriteLine(String.Format("DownloadWillBegin Event Args - FrameId: {0} Guid: {1} URL: {2}", args.FrameId, args.Guid, args.Url)); |
| 196 | + } |
| 197 | + #endregion |
| 198 | + } |
| 199 | +} |
0 commit comments