private void webView2Control1_EnvironmentCreated(object sender, EnvironmentCreatedEventArgs e) { if (e.Result != 0) { if (e.Result == 2) { MessageBox.Show("Couldn't find Edge installation. Do you have a version installed that's compatible with this WebView2 SDK version?", "Error", MessageBoxButtons.OK); } else { CommonDialogs.ShowFailure(e.Result, "Failed to create webview environment"); } return; } _environment = e.WebViewEnvironment; _newVersionToken = _environment.RegisterNewVersionAvailable(OnNewVersionAvailable); }
/// <summary> /// Prompt the user for some script and then execute it. /// </summary> public void InjectScript() { TextInputDialog dialog = new TextInputDialog( "Inject Script", "Enter script code:", "Enter the JavaScript code to run in the webview.", "window.getComputedStyle(document.body).backgroundColor", false); if (dialog.ShowDialog() == DialogResult.OK) { _webView2.ExecuteScript(dialog.Input, (args) => { if (args.ErrorCode != 0) { CommonDialogs.ShowFailure(args.ErrorCode, "ExecuteScript failed"); } MessageBox.Show(args.ResultAsJson, "ExecuteScript Result", MessageBoxButtons.OK); }); } }